I have a filereader problem where I want to return null if the value is not found. But the method is a Float method.
public float getBalance(int accountNo) throws FileNotFoundException {
Scanner reader = new Scanner(new FileReader(bankfile));
String currentline = "";
try {
while((currentline = reader.nextLine())!= null) {
String line[] = currentline.split(",");
System.out.println(currentline);
System.out.println(line[0]);
if(Integer.parseInt(line[0]) == accountNo) {
this.accountBalance = Float.parseFloat(line[1]);
}
}
}
catch(NoSuchElementException e) {
System.out.println("The account number is not found");
// I want the method to end here if the account number is not found or just return null.
}
return this.accountBalance;
}
This is the file.
File:
2,2.0,Active
1,1.0,Active
3,3.0,Active
4,4.0,Active
5,5.0,Active
I want this code
System.out.println(name.getbalance(6));
to return just the print line "This account number is not found" and not return 0.0
I can't do
if(balance == 0.0){
System.out.println("This account number is not found");
}
since some account might have 0 balance.