简体   繁体   English

Java基本程序:字符串对象甚至都不会输入

[英]Basic Java Program : String objects won't even input

I'm a C++ programmer new to Java and have trouble with Strings as fields(or attribute or whatever) of classes. 我是Java的新手C ++程序员,在使用String作为类的字段(或属性或其他任何东西)时遇到麻烦。

import java.util.Scanner;
class Student{
    String name;
    static int count=0;
    int regno;
    double marks;
    Student(){
        count++;
    }
    static void getCount(){
        System.out.println("Count is "+count);
    }
    void setName(String n1){
        name=n1;
    }
    void setMarks(double m1){
        marks=m1;
    }
    void setRegNo(int rno){
        regno=rno;
    }
    String getName(){
        return name;
    }
    int getRegNo(){
        return regno;
    }
    double getMarks(){
        return marks;
    }
}
class Demo{
    int i;
    Student s[]=new Student[3];
    Scanner in=new Scanner(System.in);
    Demo(){
        for(i=0;i<3;i++){
            s[i]=new Student();
            System.out.println("Enter name of student "+(i+1)+" :");
            s[i].setName(in.nextLine());
            System.out.println("Enter Reg. No. of student "+(i+1)+" :");
            s[i].setRegNo(in.nextInt());
            System.out.println("Enter marks of student "+(i+1)+" :");
            s[i].setMarks(in.nextDouble());
        }
    }
    public void Display(){
        System.out.println("Students with marks >70 : ");
        System.out.println("RegNo\tName\tMarks\t");
        for(i=0;i<3;i++){
            System.out.println(s[i].getRegNo()+"\t"+s[i].getName()+"\t"+s[i].getMarks());
        }
    }
}
class P4{
    public static void main(String args[]){
        Demo d=new Demo();
        d.Display();
    }
}

Output: 输出:

Enter name of student 1 :
1
Enter Reg. No. of student 1 :
1
Enter marks of student 1 :
100
Enter name of student 2 :
Enter Reg. No. of student 2 :
2
Enter marks of student 2 :
100
Enter name of student 3 :
Enter Reg. No. of student 3 :
3
Enter marks of student 3 :
100
Students with marks >70 : 
RegNo   Name    Marks   
1   1   100.0
2       100.0
3       100.0

The Trouble: From the second iteration of the for loop, the program directly asks me to enter regno and marks skipping the name String.I don't understand how it works for the first iteration but not the others.Please explain me what went wrong here. 麻烦:从for循环的第二次迭代开始,程序直接要求我输入regno并标记跳过名称String。我不了解它在第一次迭代中的工作原理,但对其他迭代不起作用。这里。

My guess is that it's the same problem as many have with eg scanf in C, namely that the ending newline from the previous input is still in the input buffer, and that leads to the nextLine function to see this newline as an empty line. 我的猜测是,这与许多问题相同,例如C中的scanf ,即前一个输入的结尾换行符仍在输入缓冲区中,这导致nextLine函数将该换行符视为空行。

You need a way to discard leading whitespace before inputting strings. 在输入字符串之前,您需要一种丢弃前导空格的方法。

Type in.nextLine() after s[i].setMarks(in.nextDouble()); s[i].setMarks(in.nextDouble());之后键入in.nextLine() s[i].setMarks(in.nextDouble());

What is happening : 怎么了 :

for(i=0;i<3;i++){
            s[i]=new Student();
            System.out.println("Enter name of student "+(i+1)+" :");
            s[i].setName(in.nextLine()); // second time around, you read the newline character
            System.out.println("Enter Reg. No. of student "+(i+1)+" :");
            s[i].setRegNo(in.nextInt());// once you press "enter" here, the newline 
            System.out.println("Enter marks of student "+(i+1)+" :");
            s[i].setMarks(in.nextDouble());// once you press "enter" here, the newline character is added to the input stream
        }

The problem is that you are mixing nextInt() / nextDouble() with nextLine() . 问题是您正在将nextInt() / nextDouble()nextLine()

When you enter your int / double , it will be followed by a new line, and when you call nextLine() in the next iteration, it will read this new line. 当您输入int / double ,它将跟随新行,并且在下一次迭代中调用nextLine()时,它将读取此新行。

The solution is to call nextLine() (and discard the result) after calling nextInt() / nextDouble() . 解决方案是在调用nextInt() / nextDouble()之后调用nextLine() (并丢弃结果nextDouble()

Here is a simple code that exhibit the problem (and the solution): 这是显示问题(和解决方案)的简单代码:

try (Scanner in = new Scanner(System.in)) {
    System.out.println("Int:");
    in.nextInt();
    // in.nextLine(); // uncomment to fix the problem
    System.out.println("Line:");
    in.nextLine();
}

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

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