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
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.
import java.util.*; // ArrayList, Random import java.util.Scanner; public class Binary { private Scanner keyboard; Boolean Validate; public Binary () { //Instantiating Scanner keyboard = new Scanner(System.in); } public static void PrintInfo(){ System.out.println("hi"); } public void processUsernumbers(){ System.out.println("First enter the 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; while(iout.println("enter string:"); String number=keyboard.next(); boolean isValid=Validatenumber(number); if(isValid) { System.out.println("status:Valid"); int intNum=Evaluatenumber(number); System.out.println("Decimal Value: "+intNum); userInput[i]=number; numValues[i]=intNum; i++; } else //To print invalid status { System.out.println("status:inValid"); } } } public boolean Validatenumber(String number) { if (number.length() == 8 && number.matches("[0-1]+")) return true; else return false; } public int Evaluatenumber(String number){ int num=0; int p=2; int c=0; for(int i=number.length()-1;i>=0;i--) { int x=(int)number.charAt(i)-48; num =num+x*(int)Math.pow(p,c); c++; } return num; } public static void main(String[] args) { Binary.PrintInfo(); /*Binary test = new Binary(); test.processUsernumbers();*/ } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started