Reputation: 11
I have a homework question that wants me to write a
Program that takes three names and their scores from a user by using delimiters. Example:
Tom:54, Matt:12, Ali:89
I keep getting an input mismatch exception when Java is assigning user2Score
. I am currently testing my program but scanner seems to take in user2
as " Matt"
instead of "Matt"
. I'm not sure if my delimiter syntax is correct. Any feedback would be greatly appreciated!
import java.util.Scanner;
public class General {
public static void main(String[] args) {
Scanner input = new Scanner(System.in).useDelimiter("[: | ,*\\s]");
String user1;
String user2;
String user3;
int user1Score;
int user2Score;
int user3Score;
System.out.println("Please enter the user name and their score:");
user1 = input.next();
user1Score = input.nextInt();
user2 = input.next();
user2Score = input.nextInt();
user3 = input.next();
user3Score = input.nextInt();
System.out.println(user1 + " " + user1Score + "\n" + user2 + " " + user2Score);
}
}
Upvotes: 1
Views: 1092
Reputation: 1171
you can use the trim()-method of String. This elimates all spaces at the beginning and at the end of the string.
Upvotes: 2