Home > Enterprise >  NoSuchElementException error for ATM Machine app (Modified)
NoSuchElementException error for ATM Machine app (Modified)

Time:10-05

I am currently learning Java and I am trying to retain the information I learned by building a ATM machine app (I plan on adding more to it in the future). My current issue is I would like to ask a user 'What would you like to do' repeatedly until a valid input is provided(current valid inputs are 'Withdraw' and 'Deposit').

I have a loop that will repeatedly ask the user 'Please select a valid choice' if the input is not valid. If the input is valid it will execute only once, ask 'What would you like to do', and then display a NoSuchElementException. Not sure how to fix this.

Here is my code:

App.java

import java.util.Scanner;

public class App {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in); 
        boolean done = false;       

        while(!done) {
            System.out.println("What woul you like to do?");
            String response = scanner.next().toLowerCase();
            Transactions newTransaction = new Transactions();

            if (response.equals("withdraw")) {
                newTransaction.Withdrawal();
            } else if (response.equals("deposit")) {
                newTransaction.Deposit();
            } else {
                System.out.println("Please select a valid choice");
            }   
        }
        scanner.close();
    }
}

Transactions.java

import java.util.Scanner;

public class Transactions {
    private int currentBalance = 100;

    public void Withdrawal() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("How much would you like to withdraw?");

        int withdrawAmount = scanner.nextInt();

        if (withdrawAmount > 0) {
            if (currentBalance > 0) {

                currentBalance -= withdrawAmount;
                System.out.println("Amount withdrawn: $"   withdrawAmount);
                System.out.println("Current Balance: $"   Balance());
                if (currentBalance < 0) {
                    System.out.println(
                            "You have withdrawn more than you have in current balance.\nYou will be charged a overdraft fee");
                }
            } else {
                System.out.println(
                        "You have withdrawn more than you have in current balance.\nYou will be charged a overdraft fee");
            }
        } else {
            System.out.println("Can't remove 0 from account");
        }

        scanner.close();
    }

    public void Deposit() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("How much would you like to deposit?");

        int depositAmount = scanner.nextInt();

        if (depositAmount > 0) {
            currentBalance  = depositAmount;
            Balance();
        }
        System.out.println("Amount deposited: $"   depositAmount);
        System.out.println("Current Balance: $"   Balance());

        scanner.close();
    }

    public int Balance() {
        return currentBalance;
    }
}

Error Message

NoSuchElementException

CodePudding user response:

Like has been said you should learn about loops. There are two types while-loop and for-loop. In this case you should youse the while-loop.

The implementation of your problem could be this.

public class App {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("What woul you like to do?");
       
        String response;
        Transactions newTransaction = new Transactions();

        
        while (true) {
                response = scanner.next().toLowerCase();        
                if (response.equals("withdraw")) {
                    newTransaction.Withdrawal();
                    break;
                } else if (response.equals("deposit")) {
                    newTransaction.Deposit();
                    break;
                } else {
                    System.out.println("Please select a valid choice");        
                }
        }

        scanner.close();
    }
}

CodePudding user response:

You can use a sentinel. A sentinel is a value entered that will end the iteration.

//incomplete code showing logic
int choice;
Scanner input = new Scanner(System.in);
System.out.println("Enter Choice");
choice = input.nextInt();
while(choice != -1){  //-1 is the sentinel, can be value you choose
    //Some logic you want to do
    System.out.println("Enter choice or -1 to end");
    choice = input.NextInt();  //notice we input choice again here
}
  • Related