Results 1 to 7 of 7

Thread: Exercises

  1. #1
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default Exercises

    hey guys

    Write a java program to capture the users name and age and display a message based on the following information...
    if age > 65 display "retired"
    if age is between 22-65 display "working"
    if age is between 7-21 display "studying"
    if age < 7 display "playing"

    The user will be prompted to enter another name or age.If the user decides not to enter another name,the program will terminate.

    im actly new on java programming and i need some help,i spend hours of trying to make this program run but seems like i cant get it right.
    ---------------------------------------------------------------------------
    public class Intel {

    public static void main(String[] args)throws Exception{
    {
    BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
    String name;
    System.out.println("Enter Name:");
    name=input.readLine();
    System.out.println("Name is:" + name);


    int age = 65;

    if (age <= 7){

    System.out.println("User is playing");
    }
    else if (age > 7 && age <21 ){
    System.out.println("User is studying");
    }
    else if (age > 21 && age <65) {
    System.out.println("User is working");
    }
    else
    System.out.println("User is retired");
    }







    }
    }
    ---------------------------------------------------------------------------

    This is how i made it. Is there some mistakes i've made cause i cant try to input age?and what am i missing


    little help please

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    You have no code in there to read the age. All you do is read the name, and set age as 65.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Have you read the API documentation for the Scanner class? It can read ints from an InputStream for you (as well as entire lines (Strings)).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    how do i put it in a way that i can input AGE and display those answers?i mean,i can input name but im new to IF ELSE statement on java. I cant input the age, so how do i start with it?

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    pretty much how you got name stored....but nexInt() instead of readLine() and use Scanner like JosAh said

  6. #6
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    public class Intel {

    public static void main(String[] args)throws Exception{
    {
    BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
    String name;
    System.out.println("Enter Name:");
    name=input.readLine();
    System.out.println("Name is:" + name);

    Scanner sc = new Scanner(System.in);
    int age = sc.nextInt();

    if (age <= 7){

    System.out.println("User is playing");
    }
    else if (age > 7 && age <21 ){
    System.out.println("User is studying");
    }
    else if (age > 21 && age <65) {
    System.out.println("User is working");
    }
    else
    System.out.println("User is retired");
    }

    --------------------------------------------------------------------

    corrections anyone?lol

    Havent figure how scanner and ELSE statement works yet

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by amzers View Post
    Havent figure how scanner and ELSE statement works yet
    Alway first read (and understand) the API documentation for a class before you attempt to use it, otherwise you end up guessing (wrong) and silly hacking like you just did. Read first, type later.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. A place to post beginner/inter exercises/scenarios
    By helpisontheway in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 09:03 AM
  2. Some exercises
    By javaamateur in forum New To Java
    Replies: 7
    Last Post: 12-02-2009, 12:00 AM
  3. Exercises
    By lclclc in forum New To Java
    Replies: 3
    Last Post: 09-14-2009, 10:20 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
  •