Question
Can some one help me out to figure out that message. Here I used the file path but its printing the message Even if I
Can some one help me out to figure out that message.
Here I used the file path but its printing the message
Even if I put the file name its still printing the same message
The part I really need help with is on how to read the file and do the conversion, The program is supposed to ask the user to type in the file name and then perform the convert the input from the file to decimal. You can also create your own random file test case that has both binary number and non binary number.
Thank You!
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner; // User input
/* Program name: Binary Number
Program filename : Project2.java
Author:
Date:
Version: 1.01
Description: This is a program that evaluate the binary number into decimal.
Input: user from the command line.
Output: screen (console)
*/
public class Project2{
private String input; // From the user.
private int decimal; // Decimal Value.
private int numOfDigit; // Number of Iterations from the user.
private String userName; // Name of the user
private Scanner scan; // Console
public Project2(){
scan = new Scanner(System.in);
input = null;
userName = null;
numOfDigit = 0;
decimal = 0;
}// end of constructor
/*
Method printInfo the infomations about the program and what needs to be done.
Parameters: none
Return: none
*/
public void printInfo(){
System.out.println("Hello user, please can you type in your name... ");
userName = scan.nextLine();
System.out.print(userName + " this program is going to ask you to enter an exact 8 digit binary number.");
System.out.print("It'll calculate the decimal equaverlant binary number and give you the status and decimal number. ");
System.out.println("Anything other than than that is going to be considered as invalid entry. ");
}// end printInfo
/*
Method processUserNumber ask the user number of operations ant to enter the info.
Parameters: none
Return: none
*/
public void processUserNumber(){
int index = 0;
int decimalVar = 0;
System.out.println("Please enter the value of operation you want to perform... ");
numOfDigit = scan.nextInt();// number of the operations
System.out.println("Please enter the 8 digit number that only includes only 1's and 0's. ");
for (int i =0; i input = scan.next(); // binary number from the user. if (validateNumber()== false){ decimalVar = evaluatesNumber(); // stores the return value fro the evaluation method. System.out.println("String: " + input); System.out.println("Status: Invalid "); } else if (validateNumber() == true){ decimalVar = evaluatesNumber(); // Stores the return value. System.out.println("String: " + input); System.out.println("Status: Valid"); System.out.println("Valid value: " + decimalVar + " "); } } } // end of processUserNumber /* Method validateNumber validate the operations entered by the user. Parameters: none Return: boolean */ public boolean validateNumber(){ boolean found = true; int checkVar = Integer.parseInt(input); // converts the input(String) from user to int value if((checkVar != '1') && (checkVar != '0') && (input.length() != 8)) { // checks the characters found = false; } else{ found = true; } return found; }// end of validateNumber /* Method evaluatesNumber does the calculations from the operations entered by the user. Parameters: none Return: int */ public int evaluatesNumber(){ int power = 0; int decimal = 0; int binaryNumber = Integer.parseInt(input); // changes the string passed by the user to int. while (binaryNumber != 0){ // calculate the number to decimal. decimal += (binaryNumber%10)* Math.pow(2, power); binaryNumber /= 10; power++; } return decimal; // returns the var as a decimal }// end of evaluateNumber /* Method processFileNumber does the calculations from the file. Parameters: inputFromFile Return: boolean */ public boolean processFileNumber(String inputFromFile) { if((inputFromFile == null) || (inputFromFile.length() != 8)) { throw new IllegalArgumentException("String: " + inputFromFile + " Status: Invalid."); } for(char c: inputFromFile.toCharArray()) { if((c != '1') && (c != '0')) { throw new IllegalArgumentException("String: " + inputFromFile + " Status: Invalid."); } } return true; } // end of processFileNumber // Main public static void main (String [] args)throws IOException{ Project2 decimalVar = new Project2(); decimalVar.printInfo(); decimalVar.processUserNumber(); Scanner in = new Scanner(System.in); System.out.print("Enter input fileName: "); String fileName = in.nextLine(); BufferedReader reader = null; BufferedWriter writer = null; try { reader = new BufferedReader(new FileReader(fileName)); writer = new BufferedWriter(new FileWriter("P2Output.txt")); } catch (FileNotFoundException e) { System.out.println("unable to find the input file in" + System.getProperty("user.dir") +". Exiting."); return; } catch (IOException e) { System.out.println("Unable to write into output file. Exiting"); return; } String line; try { while((line = reader.readLine()) != null) { try { decimalVar.processFileNumber(line); writer.append(line + ": Valid Binary String "); } catch (Exception e) { writer.append(line + ": " + e.getMessage() + " "); } } } catch (IOException e) { e.printStackTrace(); } System.out.println("Output has been written to " + System.getProperty("user.dir") + "\\output.txt"); if(reader != null) { reader.close(); } if(writer != null) { writer.close(); } } // end of main. } // end of class.
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