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 5 of 5

Thread: What is wrong with my code??

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code??

    I am still new to this and have been trying to figure this out for about three days. I think that I have most of it done, in fact I think that I only have 1 problem with it. When I compile it shows only one error. Any help would be greatly appreciated. It is supposed to show if a number is even or odd. Here is my code:


    import java.util.Scanner;

    class EvenOdd
    {
    public static void main(String[]args)
    {
    int a;
    System.out.println("Enter a Number");
    Scanner stdIn = new Scanner(System.in);
    a = in.nextInt();
    if ( a % 2 == 0 )
    System.out.println("Number is even");
    else
    System.out.println("Number is odd");
    }
    }


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: What is wrong with my code??

    Please see Announcements - What's Wrong With My Code? for instructions on how to use the code tags. You mention an error, yet give no explanation as to what it is. It helps to post that error, not only to avoid relying on us to copy, paste, compile, and run your code, but also to help you look closely at the error and/or help us explain the error

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What is wrong with my code??

    hi..
    u should change a=in.nextInt(); as a=stdIn.nextInt(); here scanner object ref var stdIn

    --- Update ---

    hi..
    u should change a=in.nextInt(); as a=stdIn.nextInt(); here scanner object ref var stdIn

  4. The Following User Says Thank You to rajeshmuduru For This Useful Post:

    joes71monte (November 27th, 2012)

  5. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What is wrong with my code??

    You made a mistake while creating object for Scanner class. The reference you initialised stdIn and the one you are using are wrong.
    use "a = stdIn.nextInt();"

    then it will work . hope it helps.
    // corrected program is
    <import java.util.Scanner;
     
    class EvenOdd
    {
    public static void main(String[]args)
    {
    int a;
    System.out.println("Enter a Number");
    Scanner stdIn = new Scanner(System.in);
    a = stdIn.nextInt();
    if ( a % 2 == 0 )
    System.out.println("Number is even");
    else
    System.out.println("Number is odd");
    }
    }>

  6. The Following User Says Thank You to RAJ467 For This Useful Post:

    joes71monte (November 27th, 2012)

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: What is wrong with my code??

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Please what is wrong with my code
    By LordDavid in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 29th, 2012, 03:33 PM
  2. Please what is wrong with my code
    By LordDavid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 28th, 2012, 09:08 PM
  3. What's wrong with my code?
    By lindmando in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2011, 10:13 PM
  4. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 02:24 PM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM