Question

Java programming How do i change this program to scan data from file and find the...

Java programming

How do i change this program to scan data from file and find the sum of the valid entry and count vaild and invalid entries

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();
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
*
* @author baderddine
*/
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) throws IOException {
System.out.println("Welcome to Base Conversion program!");
System.out.println("");

// Set you path
File file = new File("/home/baderddine/Bureau/data.txt");

BufferedReader br = new BufferedReader(new FileReader(file));

String st;
while ((st = br.readLine()) != null)
{
      String[] array = st.split(" ");
      String number = array[0];
      int base = Integer.parseInt(array[1]);
      int desiredBase = 10; // Desired base is always 10// Desired base is always 10
      System.out.println("************ Converting " + number + " from base " + base + " to base " + desiredBase + "... ************ ");
    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("");
    System.out.println(convertInteger(number, base, desiredBase));
}
}
}

//Screenshot

Comment down if you have any queries related to this answer.

Please give a thumbs up.

Add a comment
Know the answer?
Add Answer to:
Java programming How do i change this program to scan data from file and find the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

  • Look for some finshing touches java help with this program. I just two more things added...

    Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT