Results 21 to 27 of 27
Thread: beginning - IF
- 06-04-2011, 08:24 PM #21
Member
- Join Date
- Jun 2011
- Posts
- 14
- Rep Power
- 0
- 06-04-2011, 08:31 PM #22
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 ...
- 06-04-2011, 10:32 PM #23
Member
- Join Date
- Jun 2011
- Posts
- 14
- Rep Power
- 0
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?
- 06-04-2011, 10:34 PM #24
Read the API doc for the Scanner class. It will read input and convert to many different data types.But how can I store an input into an integer?
If you have a String with digits eg "123" see the Integer class for methods to convert the String to an int.
- 06-05-2011, 02:39 PM #25
Member
- Join Date
- Jun 2011
- Posts
- 14
- Rep Power
- 0
Yeh now it worksJava 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"); }}}
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? :DLast edited by sparkling; 06-05-2011 at 02:42 PM.
- 06-05-2011, 02:44 PM #26
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.
- 06-05-2011, 04:17 PM #27
Similar Threads
-
Getting code to run from the beginning in a jFrame form
By Foxtrot in forum NetBeansReplies: 6Last Post: 01-15-2012, 04:01 AM -
how to loop through an array and jump back to beginning
By drhinri in forum New To JavaReplies: 4Last Post: 02-21-2011, 03:52 AM -
Reset a Scanner to the beginning of a file
By Nosrettap in forum New To JavaReplies: 2Last Post: 02-06-2011, 03:23 AM -
Beginning Programming with Java For Dummies
By jon01 in forum New To JavaReplies: 4Last Post: 02-16-2009, 08:40 PM -
Beginning Game Development - Suggestions?
By JDCAce in forum Advanced JavaReplies: 1Last Post: 12-08-2008, 05:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks