简体   繁体   English

无法在Java的简单输出程序中获取输入

[英]Cannot get input in simple output program in Java

I will keep this short and simple, here's the program- 我将简短简短,这里是程序-

class Sample{
private int n;
public void getDetails(){
    Scanner y=new Scanner(System.in);
    n=y.nextInt();
    System.out.println("Entered 'n' = "+n);
}
public void displayDetails(){
    int i,j;
    int arr[]=new int[n];
    Scanner x=new Scanner(System.in);
    for(j=0;j<n;j++) {
        arr[j] = x.nextInt();
            System.out.println("Entered element = "+arr[j]);
    }
    System.out.println("Entered array: ");
    for(i=0;i<n;i++)
        System.out.print(arr[i]+" ");
    }
}

public class TestClass {
    public static void main(String[] args) {
        Sample obj = new Sample();
        obj.getDetails();
        obj.displayDetails();
        }
    }

This simple program just takes number of elements(n) and the elements of array(arr[]) as input in different methods. 这个简单的程序只是将不同数量的elements(n)和array(arr [])的元素用作不同方法的输入。 When the input is given in interactive mode, everything works fine. 以交互方式输入时,一切正常。 Here's the console output- 这是控制台输出-

5
Entered 'n' = 5
1 2 3 4 5
Entered element = 1
Entered element = 2
Entered element = 3
Entered element = 4
Entered element = 5
Entered array: 
1 2 3 4 5 

But when I give it as Stdin input(or all input at once), it just takes the number of elements(n) and ignores my array input(arr[]). 但是,当我将其作为Stdin输入(或一次所有输入)提供时,它仅获取元素数量(n),而忽略了我的数组输入(arr [])。 Here I had to give the array elements again. 在这里,我不得不再次给数组元素。 Console output- 控制台输出

5
1 2 3 4 5Entered 'n' = 5
1
Entered element = 1
2
Entered element = 2
3 4 5
Entered element = 3
Entered element = 4
Entered element = 5
Entered array: 
1 2 3 4 5 

I have no idea what is happening. 我不知道发生了什么事。 Is it a bug? 是虫子吗? Please help 请帮忙

You areusing scanner to take userinput. 您正在使用扫描仪接受用户输入。

 Scanner y=new Scanner(System.in);

In Scanner class if we call nextLine() method after any one of the seven nextXXX() method then the nextLine() doesn't not read values from console and cursor will not come into console it will skip that step. 在Scanner类中,如果我们在七个nextXXX()方法中的任何一个之后调用nextLine()方法,则nextLine()不会从控制台读取值,并且光标也不会进入控制台,它将跳过该步骤。 The nextXXX() methods are nextInt(), nextFloat(), nextByte(), nextShort(), nextDouble(), nextLong(), next(). nextXXX()方法是nextInt(),nextFloat(),nextByte(),nextShort(),nextDouble(),nextLong(),next()。

In BufferReader class there is no such type of problem. 在BufferReader类中,没有这种类型的问题。 This problem occurs only for Scanner class, due to nextXXX() methods ignore newline character and nextLine() only reads newline character. 由于nextXXX()方法忽略换行符,并且nextLine()仅读取换行符,因此仅对于Scanner类会出现此问题。 If we use one more call of nextLine() method between nextXXX() and nextLine(), then this problem will not occur because nextLine() will consume the newline character. 如果我们在nextXXX()和nextLine()之间再使用一次nextLine()方法的调用,则不会发生此问题,因为nextLine()将消耗换行符。

Please check this SO link it has good explaination. 请检查此SO 链接,它有很好的解释。

Part of the problem is that you are creating multiple scanners. 问题的一部分是您要创建多个扫描仪。 Create one scanner and use it everywhere, like so: 创建一个扫描仪并在各处使用它,如下所示:

import java.util.Scanner;

public class TestClass {
    public static void main(String[] args) {
        Sample obj = new Sample();
        obj.getArraySize();
        obj.displayDetails();
    }
}

class Sample{

    private int arraySize;
    Scanner scanner = new Scanner(System.in);

    public void getArraySize(){
        System.out.print("Enter size of array: ");
        arraySize = scanner.nextInt();
        System.out.println("Entered array size = " + arraySize);
    }

    public void displayDetails(){
        //Create an integer array of size arraySize
        int intArray[] = new int[arraySize];

        //Repeatedly request integers for the array
        for( int i=0; i<arraySize; i++) {
            int nextInt = scanner.nextInt();
            intArray[i] = nextInt;
            System.out.println("Entered element = " + intArray[i]);
        }

        //Print out the entered elements
        System.out.println("Entered array: ");
        for( int i=0; i<arraySize; i++) {
            System.out.print(intArray[i]+" ");
        }

        scanner.close();
    }
}

console output: 控制台输出:

Enter size of array: 5
Entered array size = 5
5 4 3 2 1
Entered element = 5
Entered element = 4
Entered element = 3
Entered element = 2
Entered element = 1
Entered array: 
5 4 3 2 1 

You still need to check that the next value is actually an int. 您仍然需要检查下一个值实际上是一个int。 If you put a different type of value in there, like a letter, it will crash. 如果您在其中输入其他类型的值(例如字母),则会崩溃。 See these links for additional details: 有关其他详细信息,请参见以下链接:

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

Java Multiple Scanners Java多重扫描仪

Why is not possible to reopen a closed (standard) stream? 为什么无法重新打开封闭的(标准)流?

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

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