Results 1 to 7 of 7
  1. #1
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default confused on how to compare simple string lenths using boolean

    i have two strings one named yes and the other named no. I need to compare them using && and ||. what I need to do is see if the first one is greater than 1 in length and second is no.

    if this hold true, then print "yes" else "no".

    im thinking I need xxx.length, but im confused from here. any ideas?

  2. #2
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: confused on how to compare simple string lenths using boolean

    Is this what you mean?

    Java Code:
    import java.util.Scanner;
    
    public class tjena{
    	public static void main(String[] args){
    		Scanner hej = new Scanner(System.in);
    		String yes = "yes", no = "no";
    		if(yes.length() > 1 && no.equals("no"))
    			System.out.print(yes);
    		else
    			System.out.print(no);
    	}
    }

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: confused on how to compare simple string lenths using boolean

    I am not quite certain what you want to do. Are your strings really named yes and no or do they contain "yes" or "no"? You can't really tell what a variable is named unless you use more advanced capabilities. A little more explanation would help.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: confused on how to compare simple string lenths using boolean

    I guess im confused as to why the "no" goes into no.equals. also why its no.equals and why theres no number(int) after the .length()

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: confused on how to compare simple string lenths using boolean

    That was how I perceived your explanation; I'm checking if the contents of the string no is equal to "no", please explain more if that's not what you meant.

    Why it is no.equals()? Well, when you're comparing strings you can't use the "==" operator because then you will compare the pointers which are pointing to the values of the strings. However, if you want to compare the values you have to use example.equals(example2).

    What do you mean with that there's no number after the length()?
    Last edited by Zelaine; 09-18-2013 at 11:02 PM.

  6. #6
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: confused on how to compare simple string lenths using boolean

    sorry for the lack of clarity. lets say I made two strings, one named hopeOne and the other hopeTwo. hopeOne has "yes" assigned to it and hopeTwo has "no" assigned to it. I did what the 2nd post said and it works.

    however now im running into an issue where I have an if statement where a string and a double need to be true or ill use an else.
    example: if my string a is="active" && my balance is > withdrawl, its telling me it doesn't recognize the &&

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: confused on how to compare simple string lenths using boolean

    Post your code inside code tags.
    Post the exact error message you get.

Similar Threads

  1. Replies: 2
    Last Post: 04-28-2013, 01:50 AM
  2. Simple code but very confused
    By Faytrow in forum New To Java
    Replies: 4
    Last Post: 10-07-2012, 06:27 AM
  3. 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
  4. Replies: 11
    Last Post: 08-15-2011, 10:12 AM
  5. Simple Boolean
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 05:01 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
  •