0

I'm basically trying to end the program only when the user enter zero, else keep looping in input for processing.

I have a question on what's going on, whenever I try to compile it, it keeps giving me an error like this:

Expected Output Your Output 1 16 1 Exception in thread "main"java.util.InputMismatchException 2 Tom 69.28 2 at java.util.Scanner.throwFor(Scanner.java:909) 3 Mickey 108.42 3 at java.util.Scanner.next(Scanner.java:1530) 4 Juliet 2488.71 4 at java.util.Scanner.nextInt(Scanner.java:2160) 5 Ann 2201.94 5 at java.util.Scanner.nextInt(Scanner.java:2119) 6 Christy 894.14 6 at Bank.main(Bank.java:103)

  public static void main(String[] args) {
 // declare the necessary variables
 double x;
 int p; 
 double balance;

 String name; 
 List<Person> list = new ArrayList<Person>();

 // declare a Scanner object to read input
 Scanner sc = new Scanner(System.in); 
 // read input and process them accordingly
 x = sc.nextDouble();
 p = sc.nextInt(); 

 // while (sc.hasNext() == true)
 String action;
 // {
 do {
 action = sc.next();

 //ArrayList list = new ArrayList(); 

 if (action.equals("Create"))
 {
 do something
 }

 if (action.equals("Withdraw"))
 {

 {
 do something

 }
 }

 }


 if (action.equals("Deposit"))
 {



 // output the result
         }


 if (action.equals("0"))
 {

 System.out.println(list.size());
 for(Person d : list){
 double bal = d.getBalance(); 

 System.out.println(d.getName());

 }     }

 }

 while ( /*action != null &&*/ !action.isEmpty());
 }
 }
1
  • Have you declared x and p as being double and int, respectively? Commented Sep 9, 2013 at 8:06

1 Answer 1

1

Docs

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

Looking at your stacktrace:

java.util.Scanner.nextInt(Scanner.java:2160) 5 Ann 2201.94 5 at

That means whatever you are inputting is not an integer.

p = sc.nextInt(); 
Sign up to request clarification or add additional context in comments.

3 Comments

but my input is 13, which is an integer and even if i changed the scanner method to nextDouble(); i am still having the same compilation error.
Can you paste the entire input?
i managed to solve it, you were right. the input was not an int! thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.