views:

78

answers:

3

I'm working on a program that takes in book records in the form

<book #>,<name>,<publisher>

for example

123,Example Book,Publisher

After which, the user is returned to a menu, using numbers to select what option they would like.

i get an java.util.InputMismatchException on this menu, whenever the string is taken in with a space. such as in the example above with 'Example Book'. whereas if it were 'ExampleBook' i would get no such error.

Is there some other way to get the input? For the string I've been using keyboard.next(), which doesn't pick up the \n and i think that may be the problem.

A: 

From the docs for StringTokenizer

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

what happens if you uses something like String.split() it should return a list of Strings in an array and you can work when them? Sorry without showing any code its difficult to suggest an improvement. PS: Whats a keyboard?

Paul Whelan
It doesn't ever throw `InputMisMatchException` and doesn't have `next()` method. So he's certainly not using the legacy `StringTokenizer` class :)
BalusC
A: 

Example on how to use scanner class

    String s = "1,1n,1b\n2,2n,2b\n2,2n,2b\n";
    Scanner sc = new Scanner(s);
    StringBuilder sb = new StringBuilder();

    while (sc.hasNextLine()) {
        String[] sa = sc.nextLine().split(",");
        sb.append("nr:\t").append(sa[0]).append("\n");
        sb.append("\tbook name:\t").append(sa[1]).append("\n");
        sb.append("\tpublisher:\t").append(sa[2]).append("\n");
    }

    System.out.println(sb.toString());

This prints out:

nr: 1
    book name:  1n
    publisher:  1b
nr: 2
    book name:  2n
    publisher:  2b
nr: 2
    book name:  2n
    publisher:  2b

Note when using a file, don't forget to close it after:

Scanner sc = new Scanner(new File("input.data"));
...
sc.close();
Margus
A: 

As stated in its javadoc, the java.util.Scanner uses a default delimiter of whitespace. The line 123,Example Book,Publisher will be interpreted as two lines: 123,Example and Book,Publisher. Most likely you're calling next() only once (which returns the first part) and then when moving to a menu, calling another next method, maybe a nextInt() which should return some chosen menu item number. But instead it attempts to return the second part which it couldn't parse/convert to an int at all and thus throws InputMismatchException.

There are several ways to solve this. The easiest solution would be to let the scanner delimit input by newlines instead of whitespace (which not only covers newlines, but also spaces and tabs and some more special whitespace).

keyboard.useDelimiter("\n");

Further, your input-checking and exception handling should be made more robust to avoid future InputMismatchExceptions. Make use of hasNext() methods and its type-specific counterpars like hasNextInt() whenever possible.

That said, don't call a Scanner a StringTokenizer. That's only confusing to others ;)

BalusC