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.