0

Im having trouble updating the name of a variable for an object using scanner. I offer a menu to the user, user selects his choice (updating name), program ask user to enter new name, Scanner reads system.In and updates the name of the variable. The only trouble is the program cant read a string with spaces. For example

while(i ==1 ) {
     System.out.printf("\n%s Properties Menu\n---------------\n1.Update Name\n2.Update Registration\n3.Update Transponder\n4.Update Capacity\n5.Update Length\n6.Update Beam\n7.Update Draft\n8.Update Longitude and Latitude\n9.Update Cargo\n10.Display the Ship\n11.Previous Menu\n",shipArrayList.get(decision).getShipName());
     shipProperties = myScanner.nextInt();  // error line 121 here
     if(shipProperties == 1) {
          System.out.print("\nEnter a new name :");
          newString = myScanner.next();     // or nextLine():    
          shipArrayList.get(decision).setShipName(newString);
     }

this works if i type something like tDog but if i type T dog i will get a java.util.InputMismatchException, output example follows

Enter a new name :t dog

t Properties Menu
---------------
1.Update Name
2.Update Registration
3.Update Transponder
4.Update Capacity
5.Update Length
6.Update Beam
7.Update Draft
8.Update Longitude and Latitude
9.Update Cargo
10.Display the Ship
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
11.Previous Menu
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at main.Map.updateShip(Map.java:121)
    at main.main.main(main.java:48)
Java Result: 1

so im guessing it reads only t and when the scanner scans for the shipProperty value it reads in dog instead of waiting for input. Ive read about nextLine() but that skips the whole user input portion, for example, switching out newString = myScanner.next() with newString = myScanner.nextLine() outputs....

DarkGoat Properties Menu
---------------
1.Update Name
2.Update Registration
3.Update Transponder
4.Update Capacity
5.Update Length
6.Update Beam
7.Update Draft
8.Update Longitude and Latitude
9.Update Cargo
10.Display the Ship
11.Previous Menu
1                           //i enter 1 to update name

Enter a new name :          //skips name input and waits for property input
 Properties Menu
---------------
1.Update Name
2.Update Registration
3.Update Transponder
4.Update Capacity
5.Update Length
6.Update Beam
7.Update Draft
8.Update Longitude and Latitude
9.Update Cargo
10.Display the Ship
11.Previous Menu

so my question is how do i get the Scanner to accept input with spaces as a valid string.

2 Answers 2

1

next reads one word. If you want to read an entire line (until the user presses ENTER), use nextLine.

You get the exception because the next call reads and returns "T", and the "dog" is left for the next call - which happens to be nextInt, which fails because "dog" isn't an int.

Sign up to request clarification or add additional context in comments.

Comments

0

After myScanner.nextInt(); you should add myScanner.nextLine(); before you read the next data.

Comments

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.