subject

The provided data file contains entries in the form

ABCDE BB

That is - a value ABCDE, followed by the base BB.

Process this file, convert each to decimal (base 10) and determine the sum of the decimal (base 10) values. Reject any entry that is invalid.

Report the sum of the values of the valid entries and total number of invalid entries

//data. txt file contains following as input

//First word is number and second is base, desired base is always 10;

// for example

thVkFu6 32

thVkFu6 is the number that should be converted into base 10 and 32 is the current base of thVkFu6.

thVkFu6 32
6b416C1bD23A 14
Ie7E0CHjgfej 22
DXBV8bON6L3KMoRM 34
2KiomlR4t0T7IO6 30
712d6Ab8 14
8231489531b759 12
5813045482d0b1 10
lJ3939oG06 25
6EhE85I73Da52fb 19
aE1GfIi42D1ck1GgC9H 21
4Lg9e34b9j 22
ebA 15
64KJdan8n05 27
25F48ccC4bCI2Jg91D48 20
64dCf5 18
101110100 2
113 5
aD5X57721 14
a4 15
1110 5
F5V 32
31LpjOb7 34
87 21
660174741073 8

//This is my program
import java. math. BigInteger;
import java. util. ArrayList;
import java. util. List;
import java. util. Scanner;

public class BaseConversion {

public static boolean isValidInteger(String number, int base) {
List < Character > occc = new ArrayList < Character > ();

for (int i = 0; i < 10 && i < base; i++) {
occc. add((char)('0' + i));
}

if (base >= 10) {
for (int i = 0; i <= base - 10; i++) {
occc. add((char)('A' + i));
}
}

for (char c: number. toCharArray()) {
if (!occc. contains(c)) {
return false;
}
}

return true;
}

private static int charValueInDecimal(char c) {
if (c <= '9' && c >= '0') {
return c - '0';
}
return c - 'A' + 10;
}

private static char digitInChar(int c) {
if (c <= 9 && c >= 0) {
return (char)(c + '0');
}
return (char)((c - 10) + 'A');
}

public static String convertInteger(String firstValue, int firstBase, int finalBase) {
BigInteger num = BigInteger. ZERO;

for (char c: firstValue. toCharArray()) {
BigInteger x = num. multiply(new BigInteger(String. valueOf(firstBase)));
num = x. add(new BigInteger(String. valueOf(charValueInDecimal(c;
}

String result = "";

BigInteger desired = new BigInteger(String. valueOf(finalBase));

while (!num. equals(BigInteger. ZERO)) {
BigInteger remain = num. mod(desired);
result = digitInChar(remain. intValue()) + result;
num = num. divide(desired);
}

return result;
}

public static void main(String[] args) {
Scanner in = new Scanner(System. in);

System. out. println("Welcome to Base Conversion program!");
System. out. println("");
System. out. println("Enter the value to be converted: ");
String number = in .nextLine().toUpperCase();

System. out. println("Enter the base of the entered value: ");
int base = Integer. parseInt( in .nextLine());

int desiredBase = 10; // Desired base is always 10

if (!isValidInteger(number, base)) {
System. out. println("Invalid number entered for base " + base);

}
if (base < 2 || base > 36) {
System. out. println("Base value " + base + " Must be in [2,36]");
System. exit(0);
}

}

System. out. println("Converting " + number + " from base " + base + " to base " + desiredBase + "...");
System. out. println("");
System. out. println(convertInteger(number, base, desiredBase));

in .close();
}

}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 01:00
Which option marks all modification made within a document? review comment track changes balloons
Answers: 1
question
Computers and Technology, 23.06.2019 13:30
Spoons are designed to be used for: spring hammering. applying body filler. identifying high and low spots. sanding highly formed areas.
Answers: 3
question
Computers and Technology, 23.06.2019 18:30
List 3 items that were on kens resume that should have been excluded
Answers: 1
question
Computers and Technology, 23.06.2019 20:00
Match the file formats with the types of multimedia they can store
Answers: 2
You know the right answer?
The provided data file contains entries in the form

ABCDE BB

That is - a v...
Questions
question
English, 22.03.2020 23:59
question
Mathematics, 22.03.2020 23:59
question
Spanish, 22.03.2020 23:59
question
Mathematics, 22.03.2020 23:59
question
Mathematics, 22.03.2020 23:59
Questions on the website: 13722359