Results 1 to 3 of 3
  1. #1
    JordiLaForge is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Exclamation If Statement that refuses to compare a indexed string array to written string.

    Hello,

    I've been trying to make a basic text intrerupator but for some odd reason a simple if statement is refusing to work. In another class I am getting text out of a text box and I am
    splitting the string using .split( " " ). I have no idea why this isn't working, please help.

    Java Code:
    import java.awt.TextArea ;
    
    public class mainIntrerupator {
    
        static TextArea textBox;
        
        public static void main( String[] input ){
    	
    	textBox= frameLoad.textBox;
    	
    	for ( int x = 0 ; x < input.length ; x = x + 1 ){
    	   	    
    	    if ( input[ x ] == "hello" ){
    		
    		printText( "Hello" );
    		
    		return;
    		
    	    }
    	    
    	}
    	
        }
        
        public static void printText( String Text ){
    	
    	textBox.append ( Text  + "\n" );
    	
        }
       
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: If Statement that refuses to compare a indexed string array to written string.

    Don't use == to compare strings. Use equals().

    e.g

    Java Code:
    if ( input[ x ].equals("hello") ) {
    // do something
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JordiLaForge is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Default Re: If Statement that refuses to compare a indexed string array to written string.

    That works, thank's allot :)

Similar Threads

  1. Replies: 10
    Last Post: 02-24-2013, 07:18 PM
  2. Program fails to compare a String Token with a String.
    By yasarqamar in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 12:42 PM
  3. Replies: 11
    Last Post: 08-15-2011, 10:12 AM
  4. Why we use String compare to String Buffer
    By Sharath_Forums in forum New To Java
    Replies: 1
    Last Post: 12-06-2010, 06:21 AM
  5. Replies: 5
    Last Post: 01-11-2009, 10:12 AM

Posting Permissions

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