2014-09-03 24 views
0

我是一個新的Java的C++程序員,並且在字符串(或屬性或任何類)的字符串中遇到麻煩。基本的Java程序:字符串對象將不會輸入

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(); 
    } 
} 

輸出:

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 

的麻煩: 從for循環的第二次迭代,則直接要求我輸入REGNO和標記跳過名稱String.I不明白怎麼它適用於第一次迭代,但不適用於其他人。請解釋我在這裏出了什麼問題。

回答

1

我的猜測是,它與許多人所遇到的問題是一樣的。在C中爲scanf,即前一個輸入的結束換行符仍然在輸入緩衝區中,並且導致nextLine函數將此換行符視爲空行。

您需要一種方法在輸入字符串之前丟棄前導空格。

+0

大!!工作,只需要用in.next()替換in.nextLine()。 (我希望Scanner的緩衝區清除自己,但它沒有) – Shridharshan 2014-09-03 08:21:55

0

in.nextLine()類型後s[i].setMarks(in.nextDouble());

正在發生的事情:

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 
     } 
+0

編輯:不是輸入鍵應該結束in.nextInt()(或in.nextDouble()),並簡單地啓動一個新的in.nextLine ()而不是以in.nextInt()結尾並插入'\ n'。這就像兩個'\ n'字符一個接一個地添加 – Shridharshan 2014-09-03 08:39:49

+0

@ Shridharshan-最後一個「\ n」將在流中。 'nextInt()'跳過第一個。 – TheLostMind 2014-09-03 08:42:25

0

的問題是,你混合nextInt()/nextDouble()nextLine()

當你輸入你的int/double時,它後面會跟着一個新行,當你在下一次迭代中調用nextLine()時,它將讀取這一新行。

解決方法是在致電nextInt()/nextDouble()後致電nextLine()(並放棄結果)。

下面是一個簡單的代碼出現此問題(和溶液):

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(); 
} 
+0

很好的解釋,但爲什麼不關閉Java併爲每個輸入啓動一個新的緩衝區?它可能會使事情更加實際。 – Shridharshan 2014-09-03 08:27:59

+0

@Shridharshan它不會爲每個輸入重新創建一個新的緩衝區,因爲你正在重用'in'。如果每次使用'in = new Scanner(System.in);'重新創建一個新的'Scanner',它將重新創建一個新的緩衝區。 – 2014-09-03 08:34:06

+0

@Shridharshan另一件需要考慮的事情是,有時候,你不希望它重新創建一個新的緩衝區(例如:當你從一個文件/另一個命令管道輸入),它不是一個好的解決方案默認做「它」。 – 2014-09-03 08:35:21