Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is in Java. The program I am working on here is intended to take binary num

ID: 3881459 • Letter: T

Question

This is in Java. The program I am working on here is intended to take binary numbers and output their decimal value. I have already completed most of the program however I need one method added to it involving output to a file but i'm not sure how this works. Add a method called "processFileNumbers" to the program this is similar to processUsernumbers but reads from an input file and outputs to a report file. The input file is of unknown size, but contains at least one string. One string appears per line. File name is input by the user and output is in the following format:


string: 00110000
status: valid
decimal value: 48

string: 1111
status: invalid

string: 12341234
status: invalid

string: 999
status: invalid

This new method should not cause change to processUsernumbers as only one of these should be called at a time, so I just commented out the current call in the main method. To clarify the new method should have its own call and processUsernumbers should not be drastically altered. Validatenumber and Evaluatenumber should be called by the new method processFileNumbers. I will post the code below. Please explain how you did this and how this method of input/output works thank you.

Explanation / Answer

/*I am just adding new method called processFileNumbers() to your existing code

* To do file operations we need to add handle exception so please copy this whole code

* To do this you just read data from file and processing them and writing back to output file

* Steps to solution:

* First program will request user to input full path of input file

* Read file data from file using BufferedReader

* process the string and

* Write output to file using BufferedWriter

*

* I added you new method processFileNumbers() separately

*  

* */

import java.util.*; // ArrayList, Random

import java.io.*;

public class Binary {

//To store instance to Scanner

private Scanner keyboard;

Boolean Validate;

public Binary () {

//Instantiating Scanner

keyboard = new Scanner(System.in);

}

public static void PrintInfo(){

System.out.println("hi");

}

public void processFileNumbers() throws IOException{

//Instantiating BufferedReader first to read file path

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter complete path of input file(like C:\data\input.txt) : ");

String inputFile=br.readLine();

/*System.out.println("Enter output file path(path\out.txt: ");

String outputFile=br.readLine();*/

//Now instantiating BufferedReader to read file

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

//Instantiating BufferedWriter to write to file

BufferedWriter bw=new BufferedWriter(new FileWriter("output.txt")); //Please change output file path or ask user for file path uncomment

String line=""; //uncomment output file path code, by default it will store in your word space

while((line=br.readLine())!=null)

{

boolean isValid=Validatenumber(line);

//If string is valid then only going to do calculation

if(isValid)

{

String status="status:valid";

//Calling method to find decimal value of string

int intNum=Evaluatenumber(line);

//Creating string for intNum

String decimalVal="decimal value:"+String.valueOf(intNum);

//now adding all string to file

// just to add new line to file

line ="string: "+line;

bw.append(line);

bw.append(" ");

bw.append(status);

bw.append(" ");

bw.append(decimalVal);

bw.append(" ");

}

else //To print invalid status

{

line ="string: "+line;

String status="status:invalid";

bw.append(line);

bw.append(" ");

bw.append(status);

bw.append(" ");

}

}

bw.close();

}

public void processUsernumbers(){

System.out.println("First enter the amount of numbers you plan to test.(consider array length)");

int n=keyboard.nextInt();

String[] userInput = new String[n];

int[] numValues=new int[n];

int i=0;

//It will iterate until you enters n valid strings

// you can see the value of i in Increamenting only when you enters valid stirng

while(i<n)

{

System.out.println("enter string:");

String number=keyboard.next();

boolean isValid=Validatenumber(number);

//If string is valid then only going to do calculation

if(isValid)

{

System.out.println("status:Valid");

//Calling method to find decimal value of string

int intNum=Evaluatenumber(number);

//printing the values

System.out.println("Decimal Value: "+intNum);

//Storing string to array

userInput[i]=number;

//Storing numerical value to the array

numValues[i]=intNum;

i++;

}

else //To print invalid status

{

System.out.println("status:inValid");

}

}

//Printing final arrays

/*

for(i=0;i<n;i++)

{

System.out.print(userInput[i]+" "+numValues[i]);

}*/

}

public boolean Validatenumber(String number) {

//Checking if length of string is 8 and string contains only 0 and 1

if (number.length() == 8 && number.matches("[0-1]+"))

return true;

else

return false;

}

public int Evaluatenumber(String number){

/*To convert binary to decimal:

* Traverse string from back and

* multiply current bit with 2 to the power c as c is 0 in starting and increament one-by-one

* For more understanding please see how to convert binary to decimal

* ex:

* 1 0 1 0

* 2^3 2^2 2^1 2^0

* 1*8 + 0*4+ 1*2 +0*1

* 10

* */

  

int num=0;

int p=2;

int c=0;

for(int i=number.length()-1;i>=0;i--)

{

//Converting character to decimal as ascii value of '0' is 48 so subtracting 48

int x=(int)number.charAt(i)-48;

num =num+x*(int)Math.pow(p,c);

c++;

}

return num;

}

public static void main(String[] args) throws IOException {

Binary.PrintInfo();

Binary test = new Binary();

/* test.processUsernumbers();*/

test.processFileNumbers();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote