Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Scanner.nextLine()返回null_Java - Fatal编程技术网

Java Scanner.nextLine()返回null

Java Scanner.nextLine()返回null,java,Java,我编写了以下代码: class PerfectPair { public static void main(String args[]) throws IOException{ Scanner scan = new Scanner(System.in); int test=scan.nextInt(); StringBuffer b=null; String a=null; while(test!=0){

我编写了以下代码:

class PerfectPair {
    public static void main(String args[]) throws IOException{
        Scanner scan = new Scanner(System.in);
        int test=scan.nextInt();
        StringBuffer b=null;
        String a=null;
        while(test!=0){
            a=scan.nextLine();
            b=new StringBuffer(scan.nextLine());
            System.out.println(a);
            String reverse=b.reverse().toString();
            if((a.length()!=b.length()) || !(reverse.equals(a))){
                System.out.println("No");
            } 
            else 
            {   
                if((a.length()==b.length()) && (reverse.equals(a))) System.out.println("Yes");
            }

            --test;
        }
    }
}
所输入的输入:

1
aa
ab

但是变量a的值为空..为什么??请解释。同时请更正代码,使其读取完整输入。

这是因为您输入的是1,然后是enter。因此,您的nextLine方法调用只读取返回键,而nextInt只读取整数值,忽略返回键。要避免此问题,请执行以下操作:

读取输入后,您可以调用如下内容:

int test=scan.nextInt();
scan.nextLine();//to read the return key.
int test=Integer.parseInt(scan.nexLine());
如果你想避免这种情况,我建议你读整行,然后把它转换成整数。比如:

int test=scan.nextInt();
scan.nextLine();//to read the return key.
int test=Integer.parseInt(scan.nexLine());

这是因为输入1后是enter。因此,您的nextLine方法调用只读取返回键,而nextInt只读取整数值,忽略返回键。要避免此问题,请执行以下操作:

读取输入后,您可以调用如下内容:

int test=scan.nextInt();
scan.nextLine();//to read the return key.
int test=Integer.parseInt(scan.nexLine());
如果你想避免这种情况,我建议你读整行,然后把它转换成整数。比如:

int test=scan.nextInt();
scan.nextLine();//to read the return key.
int test=Integer.parseInt(scan.nexLine());

这是因为输入1后是enter。因此,您的nextLine方法调用只读取返回键,而nextInt只读取整数值,忽略返回键。要避免此问题,请执行以下操作:

读取输入后,您可以调用如下内容:

int test=scan.nextInt();
scan.nextLine();//to read the return key.
int test=Integer.parseInt(scan.nexLine());
如果你想避免这种情况,我建议你读整行,然后把它转换成整数。比如:

int test=scan.nextInt();
scan.nextLine();//to read the return key.
int test=Integer.parseInt(scan.nexLine());

这是因为输入1后是enter。因此,您的nextLine方法调用只读取返回键,而nextInt只读取整数值,忽略返回键。要避免此问题,请执行以下操作:

读取输入后,您可以调用如下内容:

int test=scan.nextInt();
scan.nextLine();//to read the return key.
int test=Integer.parseInt(scan.nexLine());
如果你想避免这种情况,我建议你读整行,然后把它转换成整数。比如:

int test=scan.nextInt();
scan.nextLine();//to read the return key.
int test=Integer.parseInt(scan.nexLine());
当你使用

int test=scan.nextInt();
输入整数后推送的回车不会从输入流中读取,因此您可以做的是

int test=scan.nextInt();
scan.nextLine();
当你使用

int test=scan.nextInt();
输入整数后推送的回车不会从输入流中读取,因此您可以做的是

int test=scan.nextInt();
scan.nextLine();
当你使用

int test=scan.nextInt();
输入整数后推送的回车不会从输入流中读取,因此您可以做的是

int test=scan.nextInt();
scan.nextLine();
当你使用

int test=scan.nextInt();
输入整数后推送的回车不会从输入流中读取,因此您可以做的是

int test=scan.nextInt();
scan.nextLine();