简体   繁体   中英

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.

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.

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.

You need a way to discard leading whitespace before inputting strings.

Type in.nextLine() after 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() .

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.

The solution is to call nextLine() (and discard the result) after calling nextInt() / 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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