hey all im a relative noob with progaming if i could get some help i would be greatfull
this is homework but not the whole assignment thank you all

So when i run this it works fine if i type in the name of the first node but none of the other nodes?

playring is a doubly circular linked list

String name = input.next();

`public boolean playRingContains(String name){`
        boolean playRingContains=false;
        playRing.current=playRing.root;

        do{

            if(playRing.current.name.equals(name)){
                playRingContains=true;
                System.out.println("in check");
            }
            playRing.current=playRing.current.nextNode;
        }while(playRing.current!=playRing.root);
        return playRingContains;
    }

Dani AI

Generated

Quick summary: nothing is wrong with String.equals — the strings you compare are different at the character level even though they look the same when printed. The usual causes are the Scanner delimiter and leftover newline/CR characters (or mixing token-based reads with line-based checks). That explains why the first lookup worked but later ones failed: the visible text matched, but hidden characters on subsequent tokens prevented equals from returning true.

Practical fixes to try (robust, minimal changes):

  • Prefer Scanner's default whitespace tokenization or a whitespace regex: fileIn.useDelimiter("\\s+");. This handles spaces, tabs and newlines reliably.
  • Trim and remove CRs before storing/comparing: name = name.replace("\r", "").trim();
  • When comparing, normalize both sides: storedName.equalsIgnoreCase(inputName.trim()) if you want case-insensitive matches.
  • Avoid mixing hasNextLine() with next(); use hasNext()/next() or read full lines and then split them.

Small example pattern (not reproducing OPs code):

Scanner fileIn = new Scanner(new File(nameOfFile));
while (fileIn.hasNext()) {
    String name = fileIn.next().replace("\r", "").trim();
    int gold = fileIn.nextInt();
    playRing.addNodes(name, gold);
}

Debug tips and housekeeping:

  • Print lengths or bytes to reveal hidden chars: System.out.println("'" + s + "' len=" + s.length() + " bytes=" + Arrays.toString(s.getBytes()));
  • Reuse a single Scanner(System.in) instead of creating one inside a loop (good catch by ).
  • Verify the ring traversal logic and that root is non-null before looping (as suggested).

As discovered, making the delimiter/whitespace handling consistent fixes the symptom. Use trimming and normalization to prevent similar issues in the future.

Recommended Answers

All 7 Replies

further info that might help

I have a linked list with for nodes; nodes have a string name and gold amt.
in order the names are tom,lynn,jack,john. node names are passed in from a file then compared to a string typed in a consol widow.

I can't see any problems in that code. Are you certain that the nodes are being populated correctly with the names and links?

yes .... hhmmmm
i test both to see out put ie
System.out.println(playRing.current.name+name);
and shows that all nodes have there own name and that var name is containing the string passed in by scanner:
var nam = tom
Node : var name
prints
tom: tom
in check
lynn: tom
jack: tom

this works
but when i look for a the second name in the list it does not
tom:lynn
lynn:lynn
jack:lynn
not in check <prints if name was not found

in my code i run a test

System.out.println(playRing.current.name+name);
all nodes are populated withproper names and variable name is populated with prober scanner string i even trim scanner to make sure i am not takeing white space i ignore caps what i dont understand is why it works if i am checking the name on the firs node but not the othersie
tom:tom
lynn:tom
jack:tom
works
tom:lynn
lynn:lynn
jack:lynn
does not

if it is not in my contains method is can anyone tell me if they see a problem where i scan my values

public void methodGameManager(){
        System.out.print("Welcome to the Gold Hunt Manager");
        System.out.println("");
        System.out.println("What file name do you want to use for this game?");
        Scanner fileIn = new Scanner(System.in); 
        String nameOfFile;
        nameOfFile = fileIn.nextLine();


        try
        {
            fileIn = new Scanner(new File(nameOfFile));
            //input file must contain space between name and gold amount and space after gold amount no space after last gold amount

            fileIn.useDelimiter(" ");
            while(fileIn.hasNextLine())
            {
                String name = fileIn.next();
                String value = fileIn.next().trim();
                int goldAmt = Integer.parseInt(value.trim());
                playRing.addNodes(name,goldAmt);
            }
            fileIn.close();
        }
        catch(FileNotFoundException s)
        {
            System.out.println("File does Not Exist Please Try Again: ");
            methodGameManager();
        }

        do{

            //printPlayRing();
            //printBrig();
            if(!gameOver()){
                System.out.println("");
                System.out.println("game not over Who got captured?");
                Scanner caught=new Scanner(System.in);
                String check = caught.nextLine();
                if(playRingContains(check)){
                    System.out.println("playRing contains"+check);

                }
            }

        }while(playRing.root.nextNode!=playRing.root);

    }

you don't need to create a new instance of Scanner each time you read a value, that's just a waste of resources.

do{
            //printPlayRing();
            //printBrig();
            if(!gameOver()){
                System.out.println("");
                System.out.println("game not over Who got captured?");
                Scanner caught=new Scanner(System.in);
                String check = caught.nextLine();
                if(playRingContains(check)){
                    System.out.println("playRing contains"+check);
                }
            }
        }while(playRing.root.nextNode!=playRing.root);

I might be missing something here, but I don't see you actually changing either of those two variables, so either it never runs, or it'll be an infinite loop.

Alright guys i figuered it out when i use the the delimiter "" i am not counting for the "\n"so i ended up useing multiple dilimiters ie fileIn.useDelimiter(" |\n");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.