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

Can you correct this Charge account modification program for me? It is suppose t

ID: 3577530 • Letter: C

Question

Can you correct this Charge account modification program for me? It is suppose to read from a text file and I am getting this error: [line 8] Error ";"

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Validator
{
public static void main(String[] args)

  
public boolean isValid(int number)
{
boolean found = false; // Flag
//give full path of the file
String fileName = "C:\AccountNumbers.txt";

try{
String line = null;
FileReader fileReader =
new FileReader(fileName);


BufferedReader br =
new BufferedReader(fileReader);

while((line = br.readLine()) != null) {
if(line.trim().equals(number+"")){
found=true;
break;
}
}   


br.close();   
}
catch(FileNotFoundException ex) {
ex.printStackTrace();
}
catch(IOException ex) {
ex.printStackTrace();
}
return found;
}
}

Explanation / Answer

Here goes the working code!

Code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Validator
{
public static void main(String[] args){
      System.out.println(isValid(1));
}
public static boolean isValid(int number)
{
boolean found = false; // Flag
//give full path of the file
String fileName = "C:\AccountNumbers.txt";

try{
String line = null;
FileReader fileReader =
new FileReader(fileName);


    BufferedReader br =
    new BufferedReader(fileReader);

      while((line = br.readLine()) != null) {
      if(line.trim().equals(number+"")){
      found=true;
      break;
      }
      }


    br.close();
}
catch(FileNotFoundException ex) {
ex.printStackTrace();
}
catch(IOException ex) {
ex.printStackTrace();
}
return found;
}
}

Explanation:

*The main method should be properly close.

*I wrote a System.out.println for example purpose.

*Since main method is a static method, all the functions thus being called from main should also be declared static.

*make sure you save your file with class name like "Validator.java"

*You can call isValid method by passing an integer variable from the main method.

Eg:

isValid(2)

isValid(3)

*this will return a boolean value

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