简体   繁体   English

Scanner.nextInt上的InputMismatchException

[英]InputMismatchException on Scanner.nextInt

I'm trying to use a Scanner to draw a rectangle in JFrame, but get these errors: 我正在尝试使用扫描仪在JFrame中绘制矩形,但是出现以下错误:

 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 ie: lines, rectangles,o vals, and then input the parameters, ie 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. 该程序的目标是选择要绘制的对象的类型,即:线,矩形,椭圆形,然后输入参数,即如果我正在绘制的矩形,则输入将为r, 200,200,400,400 ,为此,在JFrame上绘制具有这些尺寸的矩形。 Then I'd just type "end" and it'd end waiting for objects to be input and drawn. 然后,我只需要输入“ end”,它就结束了等待对象的输入和绘制。

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");
}

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 . 因此,您必须告诉Scanner使用特定的定界符,以了解他必须在此String标记您的输入流。

I'd recommend you to use the following regex as a delimiter for your Scanner : 我建议您使用以下正则表达式作为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 String s in Java , you should use the String#equals method, and not the primitive comparators. 另外,作为附带说明,我想指出,为了在Java中比较String ,应该使用String#equals方法,而不是原始比较器。

Thus, you should use (as I did in my sample code) !dim.equals("end") instead of dim != "end" . 因此,您应该使用(如我在示例代码中所做的那样) !dim.equals("end")而不是dim != "end"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM