Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: beginning - IF

  1. #1
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default beginning - IF

    Hey guys answer me please. How do I use "if" statement from user input. For example. System.out.println("Are you a MALE or a FEMALE, type f/m: "); , I want to use an IF statement that is going to react based on what I type to the program (f, or m) Please help thanks. Hope you understood me

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    When comparing Strings use the equals method. For char you use ==
    So read the response into a variable and use one of the above to test the variables contents.

  3. #3
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    When comparing Strings use the equals method. For char you use ==
    So read the response into a variable and use one of the above to test the variables contents.
    Hey, thank you for your answer.
    The problem is, I have absolutely no idea how to do it. I may sound really stupid but I am sorry, I am big noob. xD

    import java.util.Scanner;

    class apples{
    public static void main(String args[]){


    System.out.println("Are you a MALE or a FEMALE, type f/m: ");
    Scanner printout = new Scanner(System.in);
    System.out.print(printout.nextLine());

    { what should I put here? if ( woot ) {
    System.out.println("true");


    }
    }}}
    thanks in advance and please, have patience with me :P

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Save printout.nextLine() in a String variable and evaluate that content using the equals method.

    For more details about equals method, check Strings API.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    oh thanks mate , now I get it

  6. #6
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    well, I have got an another problem now, the program doesn't report any errors or red flags, but it does not run properly
    what did I do wrong this time? :D

    import java.util.Scanner;

    class apples{
    public static void main(String args[]){


    System.out.println("Are you a MALE or a FEMALE, type f/m: ");
    Scanner printout = new Scanner(System.in);
    System.out.print(printout.nextLine());
    String str = printout.nextLine();

    {
    if ( str == "f") {
    System.out.println("you're a female");

    if (str == "m")
    System.out.println("you're a male");

    } else System.out.println("You must type either f or m");



    }}}


    it always says

    Are you a MALE or a FEMALE, type f/m:
    f
    f
    You must type either f or m

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    if ( str == "f") {
    Do you know if the variable str is a String or a char?

    When comparing Strings use the equals method. For char you use ==
    Look back at posts #2 and #4

  8. #8
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Do you know if the variable str is a String or a char?



    Look back at posts #2 and #4
    oh yeh, sorry
    when I replace my == with = (how it is supposed to be)
    I get an error Type mismatch: cannot convert from String to boolean . What should I do?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    What should I do?
    Read the API documentation for the String class. See post #4

    The character = is not the String method: equals()

  10. #10
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Read the API documentation for the String class. See post #4

    The character = is not the String method: equals()
    So I replaced it with

    if (str.equals("f")) {
    System.out.println("you're a female");

    but it still answers with } else System.out.println("You must type either f or m");

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    So you need to do some debugging. Add a println statement before the if statement to show the value of the str variable:
    System.out.println("str=" + str + "<");
    The print out should show you what the problem is.

  12. #12
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    So you need to do some debugging. Add a println statement before the if statement to show the value of the str variable:
    System.out.println("str=" + str + "<");
    The print out should show you what the problem is.
    Well it gives me this whatever I write there

    str=<
    what does it mean? That str=0 ?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    The value you are looking for is between the = and the <
    Looks to me like the String is empty or = ""

    Your code needs to handle the case where there is no data entered.
    What do you need to do now? Go ask the user for that data again or what?

    Why do you do this: System.out.print(printout.nextLine());
    What happened to the data that was read in by the nextLine() call?
    Did you get the value in your program? Or was it lost?

  14. #14
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The value you are looking for is between the = and the <
    Looks to me like the String is empty or = ""

    Your code needs to handle the case where there is no data entered.
    What do you need to do now? Go ask the user for that data again or what?

    Why do you do this: System.out.print(printout.nextLine());
    What happened to the data that was read in by the nextLine() call?
    Did you get the value in your program? Or was it lost?
    yeh you're right, now it works, thank you
    but, it works only for the first IF statement (f) , how to make multiple if statements then?

  15. #15
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    I guess it has to do something with the position of the "{" bracelets

    {
    System.out.println("str=" + str + "<");
    if (str.equals("f"))
    System.out.println("you're a female");

    if (str.equals("m")) {
    System.out.println("you're a male");

    } else System.out.println("You must type either f or m");



    }}}

    This way it works, however, for the first IF it says both messages, the IF and the ELSE one

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Sorry, I don't understand your last post.
    Have you fixed the problem yet?

    This is terrible code:
    }}}

    Each } should line up with the { or the code that starts the {} block.

    Also put your code in code tags to preserver formatting: http://www.java-forums.org/misc.php?do=bbcode#code

  17. #17
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Sorry, I don't understand your last post.
    Have you fixed the problem yet?

    This is terrible code:
    }}}

    Each } should line up with the { or the code that starts the {} block.

    Also put your code in code tags to preserver formatting: BB Code List - Java Forums
    hehe I am sorry :P
    Java Code:
    import java.util.Scanner;
    
    class apples{
    	public static void main(String args[]){
    
    
    	System.out.println("Are you a MALE or a FEMALE, type f/m: ");	
    	Scanner printout = new Scanner(System.in);
    	
    	String str = printout.nextLine();
    	
    
    	{
    		System.out.println("str=" + str + "<");
    		if (str.equals("f")) 	
    			 System.out.println("you're a female"); }
    			 
    		if (str.equals("m")) {
    			System.out.println("you're a male");
    		
    		} else System.out.println("You must type either f or m");
    	
    	
    		
    }}
    Well the thing is, when I type "m" in
    it comes out how it should come
    Are you a MALE or a FEMALE, type f/m:
    m
    str=m<
    you're a male


    but when I type "f" there
    Are you a MALE or a FEMALE, type f/m:
    f
    str=f<
    you're a female
    You must type either f or m


    Both "you're a female" and "You must type either f or m" come out

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Time to play computer. Look at each statement in turn and evaluate what it does. Do them one by one.
    Your if /else logic needs to be looked at and fixed.

    What is the condition/value of str for "You must type either f or m" to be printed out?

  19. #19
    sparkling is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Time to play computer. Look at each statement in turn and evaluate what it does. Do them one by one.
    Your if /else logic needs to be looked at and fixed.

    What is the condition/value of str for "You must type either f or m" to be printed out?
    Java Code:
    import java.util.Scanner;
    
    class apples{
    	public static void main(String args[]){
    
    
    	System.out.println("Are you a MALE or a FEMALE, type f/m: ");	
    	Scanner printout = new Scanner(System.in);
    	
    	String str = printout.nextLine();
    	
    
    	{
    		System.out.println("str=" + str + "<");
    		
    		
    		if (str.equals("f")) 	{
    			 System.out.println("you're a female"); 
    		
    	
    	 } else if (str.equals("m")) {
    			System.out.println("you're a male");
    		
    		} else System.out.println("You must type either f or m");
    	
    	
    		
    }}}
    now it works, thanks a lot mate, kinda took me a long time to get it, sorry for that :D

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Congrats. On to the next problem.
    Come on back if you want more of the same.

    Why do you have a pair of { }s in the middle of your code. They aren't necessary and could give you problems with scope.

    Again line up your ending }s with the starting {. Don't put them all on one line: }}}

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 6
    Last Post: 01-15-2012, 04:01 AM
  2. Replies: 4
    Last Post: 02-21-2011, 03:52 AM
  3. Reset a Scanner to the beginning of a file
    By Nosrettap in forum New To Java
    Replies: 2
    Last Post: 02-06-2011, 03:23 AM
  4. Beginning Programming with Java For Dummies
    By jon01 in forum New To Java
    Replies: 4
    Last Post: 02-16-2009, 08:40 PM
  5. Beginning Game Development - Suggestions?
    By JDCAce in forum Advanced Java
    Replies: 1
    Last Post: 12-08-2008, 05:42 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
  •