簡體   English   中英

簡單的字符串比較不起作用

[英]Simple String comparison is not working

我需要創建一個循環鏈表,其節點具有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();

我的問題是箭頭突出顯示的if語句未通過,因此我無法將Riders布爾值設置為其正確值。 以我的說法,這似乎是應該可以平穩運行的簡單代碼? 請幫助。 抱歉,這是我的第一篇文章。

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

您在循環中兩次使用fileread.next()

將第二個更改為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM