0

Here is my code

while(scan_movie_theater.hasNext()){                    
        line = scan_movie_theater.next();
        i = scan_movie_theater.nextInt();
        System.out.println(line);
        System.out.println(i);          
    }
    scan_movie_theater.close();

and my text is like

dsa 435
salon 123
xxxx 123214324
trump tower 15

how can i fix the inputmismatchexception?

Thank you.

ce.arifemre
  • 31
  • 1
  • 6

5 Answers5

0

Try follow. And strongly suggest to debug your code to see what's going wrong. I'ts your best buddy. :)

while (scan_movie_theater.hasNextLine()) {
            String line = scan_movie_theater.nextLine();
            String[] words = line.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");

            System.out.println(words[0]);
            System.out.println(Integer.valueOf(words[1]));
        }

Then you can split string for any number. If your format is consistent this should work. See string split with by a number here.

Community
  • 1
  • 1
chAmi
  • 1,774
  • 3
  • 20
  • 27
0

The method Scanner.next() is as defined here

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

the default delimiter is \p{javaWhitespace}+ (returned by Scanner.delimiter() ) which match any char validated by isWhitespace() .

for your case if you are sure that an integer is the last token in the line , then call .nextLine() after your call nextInt() like this :

while(scan_movie_theater.hasNext()){                    
        line = scan_movie_theater.next();
        i = scan_movie_theater.nextInt();
        scan_movie_theater.nextLine();// catch the carriage return and point to the next line if it exist
        System.out.println(line);
        System.out.println(i);          
    }
    scan_movie_theater.close();
Mifmif
  • 3,132
  • 18
  • 23
  • i'm sorry it didn't work also.Think about like that.If my text is like dsa 435 salon 123 xxxx 123214324 trump tower 15 how can i read one string and one integer? – ce.arifemre May 30 '14 at 17:11
  • is your file's structure already defined ? if you are sure that there are an integer in the end of line so my edited should work. but if the string could contain whitespace then you have to take line by line and split it – Mifmif May 30 '14 at 17:15
0

Well you said that there is an occurrence of two word with space trump tower which will of course catch an exception..

solution:

you could catch that exception and check if its an int or string..

while(scan_movie_theater.hasNext()){                    
    line = scan_movie_theater.next();

try { 
    int s = Integer.parseInt(line ); 
    System.out.println("I am integer" + s);
} catch(NumberFormatException e) { 
    System.out.println("I am String" + line);
}

}
scan_movie_theater.close();
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

Most likely first next() picks up dsa, then nextInt() picks up 435 and second next() pick up the carriage return. So the second nextInt() picks up salon, causing your mismatch exception. So like I said, just read line by line, split the line and parse the int.

EDIT:

for two words or more you need to get the index of the integer and get the substring

public class Test {
    public static void main(String[] args) {
        String line = "Trump Tower 23445";
        int pos = line.replaceFirst("^(\\D+).*$", "$1").length();
        String number = line.substring(pos);
        System.out.println(Integer.parseInt(number));
        System.out.println(line.substring(0, pos));
    }
}

Result

23445

Trump Tower

If the regex is too hard for you to understand, you could always just create a method to get the index of the first int

public static int getIndexOfFirstInt(String line) {
    char[] chars = line.toCharArray();
    for (char c : chars) {
        if (Character.isDigit(c)) {
            return line.indexOf(c);
        }
    }
    return -1;
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thank you for your solution.But i'm amateur for this :).If i can't solve this with scanner, i do that one word for string :) – ce.arifemre May 30 '14 at 17:27
  • You can solve this with Scanner. Just use the while loop like you are, but read it line by line with `while(scanner.hasNextLine()) { String line = scanner.nextLine()); }`. Then get the index of the first integer, using either of the method's I've provided. The same index can be used to get the substring of the String and and the `int`. If there's something specific you don't understand about my code, ask – Paul Samsotha May 30 '14 at 17:28
0

Try this if you could have white space in your String before the integer.

while (scan_movie_theater.hasNext()) {
            String[] data = scan_movie_theater.nextLine().split(" +");
            i = Integer.parseInt(data[data.length - 1]);
            line = data[0];
            for (int j = 1; j < data.length - 2; ++j)
                line +=" "+ data[j];
            System.out.println(line);
            System.out.println(i);
        }
        scan_movie_theater.close();
Mifmif
  • 3,132
  • 18
  • 23