简体   繁体   English

简单的字符串比较不起作用

[英]Simple String comparison is not working

I need to create a Circular Linked List, with nodes having Rider class objects, with each Rider having a name and a boolean value. 我需要创建一个循环链表,其节点具有Rider类对象,并且每个Rider都有一个名称和一个布尔值。 Given an text input, the program scans the input and adds riders to a "line" 给定文本输入,程序将扫描输入并将骑手添加到“行”中

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

My problem is that the if statement highlighted by the arrows is not running through, so i cannot set Riders booleans to their correct values. 我的问题是箭头突出显示的if语句未通过,因此我无法将Riders布尔值设置为其正确值。 By my accounts it seems that this is simple code that should run smoothly? 以我的说法,这似乎是应该可以平稳运行的简单代码? Help please. 请帮助。 Sorry for any weird ness this is my first post. 抱歉,这是我的第一篇文章。

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

you are using fileread.next() twice in the loop. 您在循环中两次使用fileread.next()

change the second one to 将第二个更改为

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

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

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