2012-12-10 34 views
0

我需要創建一個循環鏈接列表,其中節點具有Rider類對象,每個Rider都有一個名稱和一個布爾值。給定一個文本輸入,程序掃描輸入,並增加了車手「線」簡單字符串比較不起作用

public static void main(String[] args) throws FileNotFoundException{ 

    Scanner read = new Scanner(System.in); 
    CLLOfRiders line= new CLLOfRiders(); 
    System.out.print("Welcome to Project 1." + 
      "\nWhat is the name of the data file?\n>"); 
    String input=read.nextLine(); 
    File f= new File(input); 
    Scanner fileread = new Scanner(f).useDelimiter(" - |\n"); 
    while(fileread.hasNext()) 
    { 
     Rider r=new Rider(); 
     r.name=fileread.next(); 
     System.out.println(r.name); 
>>>>>  if(fileread.next().equals("Y")) <<<<< 
     r.changePass(true); 
     line.addRider(r); 
     System.out.println(r.speedPassStatus); 
    } 
    System.out.println("Finished reading the file."); 
    line.showWait(); 

我的問題是,如果由箭頭強調聲明沒有運行過,所以我不能設置車手布爾到正確的值。通過我的賬戶,這似乎是簡單的代碼,應該順利運行?請幫助。對不起,這是我的第一篇文章。

+0

不要修剪()在fileread.next(),看看哪裏的開始和結束大括號如果? – kosa

+0

其只是一行if語句 – user1890605

+1

好的。然後我會確保空格或區分大小寫或兩者都不會導致問題。 – kosa

回答

1
... 
r.name=fileread.next(); 
System.out.println(r.name); 
if(fileread.next().equals("Y")) 
... 

您在迴路中兩次使用fileread.next()

變化,第二個到

if(r.name.equals("Y")) 
+0

對不起,混淆,2讀取是騎士類的不同參數,名稱是一個部分,第二次讀取讀取單個字符字符串,我看看是否布爾值是true或false。我試過做一個字符串通過對象,但它不會比較 – user1890605

+0

如果您打印'String s = fileread.next(); System.out.println(「'」+ s +「'」);'你得到了什麼? (請注意,我添加了單引號) –