joshbenner851
joshbenner851

Reputation: 111

InputMismatchException on Scanner.nextInt

I'm trying to use a Scanner to draw a rectangle in JFrame, but get these errors:

 Exception in thread "main" java.util.InputMismatchException
 at java.util.Scanner.throwFor(Unknown Source)
 at java.util.Scanner.next(Unknown Source)
 at java.util.Scanner.nextInt(Unknown Source)
 at java.util.Scanner.nextInt(Unknown Source)
 at DrawerAutoRect.main(DrawerAutoRect.java:39)

My goal of this program is to choose the type of object to draw i.e.: lines, rectangles,o vals, and then input the parameters, i.e. if it's a rectangle I'm drawing, the input would be r, 200,200,400,400 and for it to draw a rectangle with those dimensions on the JFrame. Then I'd just type "end" and it'd end waiting for objects to be input and drawn.

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Scanner;
import javax.swing.JFrame;

public class DrawerAutoRect extends JFrame {
    public DrawerAutoRect(){
    setSize(1020,1020);
}

public static void paint(Graphics g, int a, int b,int c, int d)
{
   Graphics2D g2 = (Graphics2D)g;
   g2.drawRect(a, b, c, d);
}

public static void main(String[] args) {
    int x1 = 100;
    int x2 = 100;
    int y1 = 50;
    int y2 = 50;
    Scanner s = new Scanner(System.in);
    String dim = s.next();
    while(dim!="end"){
        if(dim.equals("r")){
            x1 = s.nextInt();
            y1 = s.nextInt();
            x2 = s.nextInt();
            y2 = s.nextInt();
        }
    }
    paint(null, x1, y1, x2, y2);
    DrawerAutoRect r = new DrawerAutoRect();
    r.setDefaultCloseOperation(EXIT_ON_CLOSE);
    r.setVisible(true);
    //r.pack();
    r.setTitle("Tutorial");
}

Upvotes: 1

Views: 3039

Answers (1)

ccjmne
ccjmne

Reputation: 9618

The problem is that your input does not only contain the tokens you're looking for, but also commas and spaces, apparently.

Thus, you have to tell your Scanner to use a specific delimiter to understand that he has to tokenise your input stream on this String.

I'd recommend you to use the following regex as a delimiter for your Scanner:

,\s*|\s+

This would split your input on:

  • any comma, followed by eventually some spaces, or
  • at least one space.

Consider this sample code:

try (final Scanner s = new Scanner(System.in)) {
    s.useDelimiter(",\\s*|\\s+");

    String dim;
    do {
        dim = s.next();
        if (dim.equals("r")) {
            System.out.println(s.nextInt());
            System.out.println(s.nextInt());
            System.out.println(s.nextInt());
            System.out.println(s.nextInt());
        }
    } while (!dim.equals("end"));
}

Just by entering:

r 1, 2, 3, 4, end

... on the console, I got the following output:

1
2
3
4

... It works!

Also, as a side note, I'd like to point out that, in order to compare Strings in Java, you should use the String#equals method, and not the primitive comparators.

Thus, you should use (as I did in my sample code) !dim.equals("end") instead of dim != "end".

Upvotes: 1

Related Questions