简体   繁体   English

控制台上的输入不适用于此Java程序-为什么?

[英]Input on console doesn't work with this java program - why?

With this program it skips past the inputs - and outputs this to the console: 使用此程序,它将跳过输入-并将其输出到控制台:

    C:\Users\User\workspace\ClassManager\bin>java AccessPupilData
What would you like to do?
l = list pupils -------- a = add pupil ---------
a
a
You want to add a pupil.
Enter your first name:
Enter your surname:
Firstname :null
Surname: null
Age :0
You created a pupil called null null who is aged 0

(I'm using a dos prompt to run the program in, not the eclipse console). (我使用的是dos提示符,而不是在eclipse控制台中运行程序)。 Why don't I get to input when the scanner is called? 为什么在调用扫描仪时无法输入?

First the initial class that kicks off everything: 首先,开始一切的初始课程:

public class AccessPupilData {

public static void main (String arguments[]){

... ...

case 'a': Pupil pupil = new Pupil(); break;

And then the Pupil class that is where I want to collect all the information: 然后是想要收集所有信息的Pupil类:

import java.util.Scanner;

public class Pupil {
    private String surname;
    private String firstname;
    private int age;

public Pupil(){

    Scanner in = new Scanner(System.in);


       // Reads a single line from the console 
       // and stores into name variable
       System.out.println("Enter your first name: ");
       if(in.hasNext()){
           this.firstname = in.nextLine();
       }

       System.out.println("Enter your surname: ");
       if(in.hasNext()){
           this.surname = in.nextLine();
       }
       // Reads a integer from the console
       // and stores into age variable
       if(in.hasNext()){ 
       System.out.println("Enter your age: ");
       this.age=in.nextInt();
       }
       in.close();            

       // Prints name and age to the console

       System.out.println("Firstname :" +firstname);
       System.out.println("Surname: " + surname);
       System.out.println("Age :"+ age);


    System.out.print("You created a pupil called " + this.firstname + " " + this.surname + " who is aged " + this.age);
}

} }

Use Console.readLine 使用Console.readLine

Console c = System.console();
this.firstname = c.readLine("Enter your first name: ");

I would suggest using BufferedReader 我建议使用BufferedReader

Scanner is definitely very convenient to use but it is slower since it has to read the content of the stream every time next token in the stream is requested. While Buffered Reader buffers the tokens so that there is no need to read from the stream again. Quoted from Scanner vs BufferedReader . 引用Scanner vs BufferedReader

public static void main(String[] args) {
    String firstName = getInput("Enter your first name: ");
    System.out.println("Hello " + firstName);
}

public static String getInput(String prompt) {
    BufferedReader in = new BufferedReader(
            new InputStreamReader(System.in));

    System.out.print(prompt);
    System.out.flush();

    try {
        return in.readLine();
    } catch (IOException e) {
        return "Error: " + e.getMessage();
    }

}

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

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