Results 1 to 5 of 5

Thread: For loop

  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default For loop

    Hi,

    I have an array "commands" containing two values. I want to compare "inputLine" to every value in the array and if there is a match - return "Command found: " + command message. But whatever value I pass into the method, the outputLine always stays null.

    Why is this happening?

    Java Code:
    public class Processing {
    
        String[] commands = {"help", "start"};
        
        public String sortCommand(String inputLine) {
            
            String outputLine = null;
            
            for (int i = 0; i < commands.length; i++) {
            
                if (inputLine == commands[i]) {
                    outputLine = "Command found: " + commands[i];
                    break;
                }
            
            }
            
            return outputLine;
    
        }
    
    }

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: For loop

    Have you tried verifying that whatever you're passing to the sortCommand function is what you think it is? I tried sortCommand("help") and it worked as you were expecting. Maybe check how you are getting your input.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: For loop

    Don't compare Strings for equality with the == operator; use the equals(...) method instead; also read the API documentation for the String class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: For loop

    @JosAH
    Thanks, I corrected it to equals.

    @Shoss
    I think you are right. I tries to debug it myself but I still cannot find the source of this "null" value. Here is the code that sends "inputLine" to Processing.sortCommand():

    Java Code:
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            
            Processing process = new Processing();
            
            outputLine = process.sortCommand(null);
            
            while ((inputLine = in.readLine()) != null) {
                
                out.println("->" + "\033[36m" + inputLine + "\033[0m");
                outputLine = process.sortCommand(inputLine);
                out.println(outputLine);
                        
            }

  5. #5
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

Similar Threads

  1. Converting a for loop to a do-while loop
    By awesom in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 03:02 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  5. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •