0

The following is the requirement: i. If there is a first, middle, and last, only display the first and last name ii. If there is a first and last, then display both first and last iii. If there is only a first name, then just display it

I have the following code:

String fullName = "first middle last";
Scanner nameScanner = new Scanner(fullName);
nameScanner.findInLine(" ");
MatchResult result = nameScanner.match();
if(result.groupCount() > 2)
{   
System.out.println(result.group(1)+ " "+ result.group(3));
}
else
{
System.out.println(fullName);
}

When I try to run the code I get

Exception in thread "main" java.lang.IllegalStateException: No match result available
at java.util.Scanner.match(Unknown Source)
at com.assignment.RunAssignmentApp.main(RunAssignmentApp.java:48)

What am I doing wrong ?

5
  • I think your findInLine argument needs some capture groups (pairs of parenthesis).
    – Gus
    Jan 30, 2014 at 22:33
  • When I tried it, I didn't get an exception (but it just printed out first middle last).
    – ajb
    Jan 30, 2014 at 22:34
  • The MatchResult will refer to the thing you searched for, i.e. the single space. Groups are substrings of the entire matched string. But the entire matched string is just " ".
    – ajb
    Jan 30, 2014 at 22:38
  • What will you do with last names that contain spaces like "van de Puy"? Jan 30, 2014 at 22:41
  • @Jean-FrançoisCorbett A long time ago, I did write data entry software that tried to divide up names into first/middle/last, for printing badges at a convention. Not only did it not work all the time for names like your example, it also had problems with Asians who put their surname first, and Spanish speakers who put their surname in the middle and their mother's maiden name last. I think I eventually had to give the users a lot more control over how to divide the name, because there's really no way to do it automatically.
    – ajb
    Jan 30, 2014 at 22:49

3 Answers 3

1

Scanner doesn't work this way. The way you're writing this, you would be better of splitting the input into string arrays based on the spaces:

public static void nameScanner() {
    String fullName = "first middle last";
    String[] names = fullName.split(" "); // split the string at the spaces
    if (names.length > 2) {
        System.out.println(names[0] + " " + names[2]); // arrays start at index 0, so names[0] is first name
    } else {
        System.out.println(fullName);
    }
}

Of course, if you're taking input from a user, you definitely want to add some error checking and handling to make sure you don't go out of bounds on your array.

1

String has a built in split().

String fullName = "first middle last";
String[] names = fullName.split(" ");

switch(names.length) {
    case 3: // The string had three tokens. Print the first and last one.
        System.out.println(names[0] + names[2]);
        break;
    case 2: // The string had two tokens. Print both.
        System.out.println(names[0] + names[1]);
        break;
    case 1: // The string had one tokens. Print it.
        System.out.println(names[0]);
        break;
    default: // The string had less than one or greater than 3 tokens.
        System.out.println("You either have no name or a very long name!");
}
2
  • 1
    split() is missing a regex :)
    – AWT
    Jan 30, 2014 at 22:35
  • @AWT Oh noooooooo. I could have sworn that split() worked on whitespace by default. Corrected!
    – Rainbolt
    Jan 30, 2014 at 22:36
1

Try

nameScanner.findInLine("(\\w+) (\\w+) (\\w+)");
1
  • I'd recommend putting a + after each of the spaces.
    – ajb
    Jan 31, 2014 at 0:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.