“线程中的异常”;“主要”;使用扫描仪时的java.util.InputMismatchException

“线程中的异常”;“主要”;使用扫描仪时的java.util.InputMismatchException,java,java.util.scanner,Java,Java.util.scanner,我有一个程序来颠倒单词的顺序,但是输入搞乱了。这是我的输出日志 1 this is a trial Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanne

我有一个程序来颠倒单词的顺序,但是输入搞乱了。这是我的输出日志

1
this is a trial
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Store_Credit.main(Store_Credit.java:13)

Process finished with exit code 1
这是我的代码:

import java.util.Scanner;

public class Reverse_Words {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N;
        N = scanner.nextInt();
        for(int i = 0; i < N; i++) {
            int h  = i+1;
            String[] s = scanner.nextLine().split(" ");
            System.out.print("Case #"+h+": ");
            for(int j = s.length-1; j >=0; j++) {
                System.out.print(s[j]+" ");
            }
            System.out.println();
        }
    }
}
import java.util.Scanner;
公共类反义词{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
int N;
N=scanner.nextInt();
对于(int i=0;i=0;j++){
系统输出打印(s[j]+“”);
}
System.out.println();
}
}
}

尝试执行此操作,并检查是否仍有错误

public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        int n;
        System.out.println("enter number");
        n = scanner.nextInt();
        for(int i = 0; i < n; i++) {
            int h  = i+1;

            System.out.println("enter string");

            String[] s = scanner1.nextLine().split(" ");
            System.out.print(s.length);
            System.out.print("Case #"+h+": ");
            for(int j = s.length-1; j >=0; j--) {
                System.out.print(s[j]+" ");
            }
            System.out.println();
        }
    }
publicstaticvoidmain(字符串[]args){
@抑制警告(“资源”)
扫描仪=新的扫描仪(System.in);
int n;
System.out.println(“输入编号”);
n=scanner.nextInt();
对于(int i=0;i=0;j--){
系统输出打印(s[j]+“”);
}
System.out.println();
}
}

减小最后一个for循环中的j值,即j--(不是j++)

Scanner.nextInt()
使用您键入的数字,但不使用回车符。调用
Scanner\nextLine()
会使用返回值。I second cricket\u 007:堆栈跟踪是针对Store\u Credit.java的,您已经发布了Reverse\u Words.java的代码。这不是正确的类<代码>商店的信用卡。main。请说明,如果您希望帮助@deep使用与堆栈跟踪匹配的正确代码更新您的问题,请使用放置在问题下方的选项(或者更准确地说,位于标记下方)。@Tilak question希望反转单词而不是字符问题中的代码不会抛出上述错误。有两个扫描仪是不必要的是的,你是对的。我意识到这一点。因为我没有得到上面提到的错误,所以我不确定代码到底出了什么问题。所以我给出了一个正确运行的代码。我想我已经明白了。将scanner.nextLine()放在scanner.nextInt()之后。问题是nextInt()不接受返回。nextLine()接受您键入的数字n和字符串。因此,添加另一个scanner.nextLine()会有所帮助。