Results 1 to 5 of 5
  1. #1
    abrium is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default Multiconditional If

    Me again, I got a feeling I'm going to be here a lot in the future. The good news is I actually do write the code and then try to figure out why it won't work rather than trying to get people to code it for me.

    My current issue: Trying to write a pig latin translator and I have a multi-conditional if statement. For some odd reason if I input a test word such as: show, Show, Chow, Who, or any other word the code does NOT step into the If statement and I can't figure out why. Any takers?

    Java Code:
    	public String convertText(String text){
    
    		String currToken;
    		String outputText = "";
    		
    		StringTokenizer sT = new StringTokenizer(text);
    
    		
    		while (sT.hasMoreTokens()){
    			currToken = sT.nextToken();
    			int length = currToken.length();
    			if (length <= 1){
    				outputText = outputText + currToken + "ay ";
    			}
    			if (length > 2){
    				char[] wordArray = currToken.toCharArray();
    				String firstTwo;
    				firstTwo = String.copyValueOf(wordArray, 0, 2);
    				if (firstTwo == "sh"|| firstTwo =="ch" || firstTwo == "th" || firstTwo == "kn" || firstTwo == "wh"
    					|| firstTwo == "wr" || firstTwo == "qu" || firstTwo == "ph" || firstTwo == "sc"){
    					String remainingLetters;
    					remainingLetters = String.copyValueOf(wordArray, 1, wordArray.length);
    					outputText = outputText + remainingLetters;
    				}
    				
    			}
    		
    		}
    		
    		return outputText;
    	}
    Much appreciated if someone can explain this one to me.

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

    Default

    You can't compare two Strings for equality by using the == operator. Use the equals( ... ) method instead; read the API documentation for the String class.

    kind regards,

    Jos

  3. #3
    abrium is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Many thanks sir, many thanks.

  4. #4
    abrium is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Are the API docs available at Sun?

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

    Default

    Quote Originally Posted by abrium View Post
    Are the API docs available at Sun?
    Yep, here they are; bookmark it, you'll need it (everybody does).

    kind regards,

    Jos

Posting Permissions

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