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: Java basic Example

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Java basic Example

    <code=java>

    i m giving again a very simple code of odd or even . I want that user should give the value of x and thereafter ,it is checked whether odd or even.

    class oddoreven
    {
    public static void main (String args[])
    {
    int x;

    System.out.println ("Enter an integer to check if it is even or odd ");
    if(x%2==0)
    System.out.println("you entered an even number ,");
    else
    System.out.println("You entered an odd number .");
    }
    }
    In this prog user should give the value of x and it is then checked. But i m getting an error that variable x might not have been initialized. Thanks


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java basic Example

    You don't seem to get input from the user anywhere in your program, and the compiler is smart enough to see that you're not giving x any value. Consider using a Scanner object for this.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java basic Example

    can u help me in writing the code for this ?

  4. #4
    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: Java basic Example

    No.
    Give X a value and read up on the Scanner class.

    Sorted.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java basic Example

    Hi,
    You can use System.in to read values from console.
    thus get X value from user and pass that to determine whether odd or even.

Similar Threads

  1. [SOLVED] Basic Java Program Help
    By Nuggets in forum Java Theory & Questions
    Replies: 15
    Last Post: January 18th, 2012, 12:47 AM
  2. Basic Java question
    By fred2028 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 13th, 2011, 03:54 PM
  3. Basic Java Tutorials
    By uimbibe in forum The Cafe
    Replies: 0
    Last Post: January 26th, 2011, 12:55 PM
  4. Not sure what to do next (Basic java program)
    By desi22601 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 21st, 2010, 09:05 AM
  5. Basic Java Encryption
    By BronxBomber in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2010, 10:50 PM

Tags for this Thread