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

Can some one help me out to figure out that message. Even if I put the file name

ID: 3879224 • Letter: C

Question

Can some one help me out to figure out that message.

Even if I put the file name it’s 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<numOfDigit;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.

ct2.java ile Messages jGRASP Messages Run LO Interactions Valid value: 3 Enter input fileName: "C:UserslutheDocumentsics section 2 projectseile.txtYP2Input.txt unable to find the input file inc:lUserslutheDocumentslcs section 2 projects. Exiting. ar Ip GRASP: operation complete. Line:54 Col:101 Code:0 Top:106 1:45 PM /26/2018

Explanation / Answer

In the first picture, you are providing the wrong file path. Look carefully !.The files.txt is not a directory.Provide the correct path and it works.I compiled it and it works like a charm.

Enter C:UserslutheCS Section 2 projectsP2input.txt

In the second case, it has no way to know where the file is since you don't have an absolute path of the file.

Alternatively,

I suggest You get the file path in the program itself if the file is in the working directory. So that you don't need to provide the full path.Then All you need to do is provide the file name itself.Sometimes we have the issue of file separator '/' or '' so it is better to get it from the system to avoid errors.

Note: The file should be present in the same directory for below to work.

To do that add these line to your code and only provide the file name as input

// add these lines to your main function

IMP: please check the file path carefully where is the file located checking its properties. Also, you can print the working directory of your project using System.out.println(absoluteFilePath); in your code to give you an idea where your input file should be

Read the file using the absolute file path in your code as shown below.

Example:-  reader = new BufferedReader(new FileReader(absoluteFilePath ));

Also the input file is where the output file is created.

Hope it helps. If any difficulties feel free to ask.

BELOW IS THE MODIFIED CODE AS REQESTED.

import java.io.BufferedReader;

import java.io.BufferedWriter;
import java.io.File;

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

String workingDirectory = System.getProperty("user.dir");
String absoluteFilePath = "";  
absoluteFilePath = workingDirectory + File.separator + fileName;


BufferedReader reader = null;

BufferedWriter writer = null;

try {

reader = new BufferedReader(new FileReader(absoluteFilePath));

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.