Can you correct this Charge account modification program for me? It is suppose t
ID: 3579530 • 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.