Question

Cant seem to figure this. If user inputs an invalid balance, how can i reprompt them to enter balance again and still continue my program?

  //EDITED STILL WONT WORK PROPERLY
 boolean again;
    while(again = true) 
    {
        try {

            // pass object to printwriter and pw to write to the file
            pw = new PrintWriter(fw);

            System.out.print("Input beginnning balance: ");
            balance = input.nextDouble();
            again = false;
            // pass user input to object
            AccountWithException acctException = new AccountWithException(fullName, balance, id, RATE);
            again = false;

            System.out.println(acctException.toString());

            // copy object to created file
            pw.println(acctException.toString());
            again = false; 

        // custom exception
        } catch (InvalidBalanceException e) {
            System.out.println(e.getMessage());
        } catch(FileNotFoundException e) {
            System.out.println(e.getMessage());
        } finally {
            pw.close();
Was it helpful?

Solution

you can throw Invalidbalanceexception and catch it in catch block like this

try {

        // pass object to printwriter and pw to write to the file
        pw = new PrintWriter(fw);

        // pass user input to object
        AccountWithException acctException = new AccountWithException(fullName, balance, id, RATE);

        System.out.println(acctException.toString());

        // copy object to created file
        pw.println(acctException.toString());
throw new InvalidBalanceException ();

        // custom exception if balance < 0
    } catch (InvalidBalanceException e) {
        System.out.println(e.getMessage());
        System.out.println("Re-enter balance: ");
        balance = input.nextDouble();
    } catch(FileNotFoundException e) {
        System.out.println(e.getMessage());
    } finally {
        System.out.println("Text file closed, program complete...");
        pw.close();
    }

OTHER TIPS

do{
    //take user input
    //check for invalid balance
    try{
        if(balance is invalid){
            throw new InvalidBlanceException();
        }
    } catch(InvalidBalanceException e) {
        //user input again reprompt
    }
}while(end of file)

One problem is your while loop. The assignment isn't going to help in evaluation. Also, if you throw the exception, it will trigger the catch block.

 boolean again = true;
    while(again) // check for true instead of making assignment 
    {
        try {

            // pass object to printwriter and pw to write to the file
            pw = new PrintWriter(fw);

            System.out.print("Input beginnning balance: ");
            balance = input.nextDouble();
            // pass user input to object
            
            // throw an exception to trigger catch block
            // according to catch block, should be an InvalidBalanceException
            if(balance < 0) throw new AccountWithException(fullName, balance, id, RATE);
            again = false; // this line isn't run if it fails to exception

        // custom exception
        } catch (InvalidBalanceException e) {
            System.out.println(e.getMessage());

            // copy object to created file
            pw.println(e.toString());
        } catch(FileNotFoundException e) {
            System.out.println(e.getMessage());
        } finally {
            pw.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top