Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with code

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with code

    Hello!

    I'm really new to Java and i've ran into a problem. I'm making a program that asks you for your age and tells you what you can do at that age. Here is the code:

    package switchpkg;
    import java.util.Scanner;
    public class Main {

    public static void main(String[] args) {
    Scanner auto = new Scanner(System.in);
    System.out.println("Tell me how old you are and i will tell you what you can do");

    int age;

    int month;

    age = auto.nextInt();

    if(age == 1);
    System.out.println(age);



    System.out.println("Now... Tell me which month it is");

    month = auto.nextInt();
    System.out.println("Your lucky number is:"); System.out.println(month + age);

    switch(age){

    case 1:
    System.out.println("You can crawl");
    System.out.println("And you are to young to do this test, error!");
    break;

    case 2:
    System.out.println("You can talk");
    break;

    case 3:
    System.out.println("You can get into trouble");
    break;

    default:
    System.out.println("My knowledge doesn't reach that far!");
    break;




    }


    }

    }



    This is how it turns out:

    Tell me how old you are and i will tell you what you can do
    1
    1
    Now... Tell me which month it is
    2
    Your lucky number is:
    3
    You can crawl
    And you are to young to do this test, error!


    I want the You can crawl after: "Tell me how old you are and i will tell you what you can do" and just only one 1 here:

    Tell me how old you are and i will tell you what you can do
    1
    1
    Now... Tell me which month it is


    Sorry for the long post. Kind of a newbie.

    Thanks!


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Problem with code

    Well one of the '1's is the input you provided, and the other is you printing out the age of the user.
    If you only want one '1' appearing altogether, remove
    System.out.println(age);
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with code

    if(age == 1);

    Hmmmm....there's a ; after your if.

    That should be removed.

Similar Threads

  1. Problem with code for school assignment?
    By Mellisa315 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 16th, 2010, 08:36 PM
  2. Problem with the code
    By noFear in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 9th, 2010, 10:28 AM
  3. problem in my code
    By wannabe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2010, 07:53 AM
  4. Problem with Recursive code
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 22nd, 2010, 08:36 PM
  5. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM