Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: beginning - IF

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

    Default

    Well, what if I wanted to do the same, but with integers. Saving my input into an int and then comparing it. For example I'd ask "what is your age?" and react with another messages based on the number put in

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Testing a primitive like an integer is easier than with an object like String.
    Use one of the operators for comparing the values of two integers, like == or < or > or ...

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

    Default

    But I have to store my input somewhere, just like I did with
    String str = printout.nextLine();
    and then I used the equal method

    But how can I store an input into an integer?

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    But how can I store an input into an integer?
    Read the API doc for the Scanner class. It will read input and convert to many different data types.

    If you have a String with digits eg "123" see the Integer class for methods to convert the String to an int.

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

    Default

    Quote Originally Posted by Norm View Post
    Read the API doc for the Scanner class. It will read input and convert to many different data types.

    If you have a String with digits eg "123" see the Integer class for methods to convert the String to an int.
    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"); 
    			 	System.out.println("how old are you?");
    				Scanner femaleage= new Scanner(System.in);
    				int age = femaleage.nextInt();
    				if( age >= 15) {
    					System.out.println("You can register now");
    				}
    				else if ( age < 15) {
    					System.out.println("You must be above 15 in order to register");
    				}
    	 
    		} else if (str.equals("m")) {
    			System.out.println("you're a male");
    				System.out.println("how old are you?");
    				Scanner femaleage= new Scanner(System.in);
    				int age = femaleage.nextInt();
    				if( age >= 15) {
    					System.out.println("You can register now");
    				}
    				else if ( age < 15) {
    					System.out.println("You must be above 15 in order to register");
    				}
    	 
    				
    	 } else System.out.println("You must type either f or m");
    }}}
    Yeh now it works
    but, it seems all so complicated to me, when I looked at the API site, there are literally like thousands of things like this, how am I supposed to learn and remember it all? :D
    Last edited by sparkling; 06-05-2011 at 03:42 PM.

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Yes there are a lot of details. You have to go back and read the API as often as necessary. Eventually some of it sticks.

  7. #27
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    You don't need to know everything.

    You do need to know where to find everything.

    db

Page 2 of 2 FirstFirst 12

Similar Threads

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