-2

To be fair, I'm not getting these exceptions but merely trying to find a away to cover these exceptions. The exceptions are NosuchElementException and NumberFormatException.

Note: This programs works perfectly because the txt file is fine. However, introduce anything that is not a number and it will fail.

Here is the main class where the problem could occur:

BankReader.java

package bankreader;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class BankReader
{

    public static void main(String[] args)
    {
        BankReader reader = new BankReader();
        Scanner scan = new Scanner(System.in);
        String fileName = "";
        boolean finished = false;

        while(!finished)
        {
            try
            {
                System.out.print("Enter the name of the file: ");
                fileName = scan.nextLine();
                scan = reader.checkFile(fileName, scan);
                reader.readFile(scan);
                finished = true;
            }
            catch(IOException ex)
            {
                System.out.print("\nThis file does not exist or had");
                System.out.println(" characters that were not numbers. Please enter a different file.\n");
            }
        }

        scan.close();
    }

    public Scanner checkFile(String fileName, Scanner scan) throws IOException
    {
        File file = new File(fileName);
        scan = new Scanner(file);          
        return scan;
    }

    public void readFile(Scanner scan)
    {
        String accountNumber = "";
        double accountBalance = -1;
        Bank bank = new Bank();

        while(scan.hasNext())
        {
            accountNumber = scan.next();
            accountBalance = Double.parseDouble(scan.next());

            BankAccount bankAccount = new BankAccount(accountNumber, accountBalance);
            bank.addAccount(bankAccount);
        }

        if (bank.numberOfAccounts() > 0)
        {
            BankAccount maxBalance = bank.getHighestBalance();
            System.out.println(maxBalance.getAccountNumber() + ": " + "$" + maxBalance.getBalance());
        }
        else
            System.out.println("\nThe file had no accounts to compare.");
    }
}

Here is the txt file I'm working with:

346583155444415 10000.50
379611594300656 5000.37
378237817391487 7500.15
378188243444731 2500.89
374722872163487 25000.10
374479622218034 15000.59
342947150643707 100000.77

So even though this is my own txt file, what if I was accessing a text file that a character that wasn't a number or had an account number but no balance and vice versa. I would like to know how I can deal with these exceptions.

What I've tried:

I've tried to do scan.nextLine() to move away from the exception but it just introduces another exception.

I've also tried to use a method that uses regex to check if the string is a number. The problem is I'm using a variable that is not a string and I would rather not create more checks.

It seems to me that no more what I do, I can't recover my scanner after an exception has occurred.

Luis Averhoff
  • 385
  • 6
  • 22
  • 1
    Just to start: You need to include the actual exception message: put `System.err.println(ex.getMessage())` inside your `catch` to see what's really happening. Also, you may want to print the stack trace of the exception, to see which line of your program is throwing it (put `ex.printStackTrace(System.err)` inside your `catch` to do that) – Barranka Nov 09 '15 at 21:49
  • Its same as notAGenuineConcernException() or didNotReadAboutExceptionsExveption and it will start a rollerCoasterOfExceptions. – Farrukh Subhani Nov 09 '15 at 21:50
  • @Barranka Well like I said I'm not getting an exception but trying to find way to recover from these two exceptions because they are of the highest importance. – Luis Averhoff Nov 09 '15 at 21:54

1 Answers1

0

before parsing you can test if it is a double with scan.hasNextDouble and parse or read the number only then else you set default value and move next by reading the incorrect value and dont do anything with it

Angen
  • 400
  • 2
  • 10