简体   繁体   中英

If statement isn't being read

This is how the if statement is written

            //Check and see if current is less than the next character.
            if (node.thisChar().charAt(0) < current.thisChar().charAt(0) )
            {
                temp = current;
                previous = node;
                node.next = current;

                System.out.println("Its in here");
                break;
            }// end if

            previous = current;
            current = current.next;

        //what was empty at the end of the list now has a new node being linked.

        System.out.println(current.thisChar().charAt(0) + " This is the current");
        System.out.println(node.thisChar().charAt(0) + " This is the new character");
        System.out.println("Character " + node.thisChar() + " has been added");

The issue's with that highlighted if statement. If for example a node letter 'd' is less than current letter 'f' , the if statement would end up being skipped, resulting in a unsorted list. The output placed within the if statement is never reached. What's odd is how my output would be showing the characters with thisChar().CharAt() ( thisChar simply returns the character, and it's of type String ) ; showing that the statements were working. Unless the issue lies with how the variables are of type String , I have no clue what is going on with the if statement. Any advice will be appreciated.

You could alternate your comparing with the method

string.compareTo(String)

example

String s1, s2;
//blabla
//giving string variables values
s1.compareTo(s2)

See this link for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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