Results 1 to 8 of 8
  1. #1
    GDW
    GDW is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Question Help with initializing error

    Hi, i am really new to java and i keep getting this error:
    :20: variable might not have been initialized.
    It has happened several times now and i cannot pin point the problem. here is my code (yes i know it is simple, but i am a noob)... It isn't for class so don't worry about helping me cheat... i am just using a text book i got from ebrary.

    //multiplication table with input

    import java.io.*;
    public class Ch2Three {
    public static void main(String args[]) {
    int num1;
    BufferedReader reader;

    reader = new BufferedReader(new InputStreamReader(System.in));
    try {
    System.out.println("Insert whole number:");
    num1 = Integer.parseInt(reader.readLine());
    reader.close();
    }
    catch (IOException ioe) {
    System.out.println("I/O Error Occured.");
    }

    System.out.println("These are the multiples of that number:");
    System.out.println((1 * num1) + ".... Well, you get the point.");
    }
    }

    i would love some help... thanx!

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

    Default

    Quote Originally Posted by GDW View Post
    Hi, i am really new to java and i keep getting this error:
    :20: variable might not have been initialized.
    It has happened several times now and i cannot pin point the problem. here is my code (yes i know it is simple, but i am a noob)... It isn't for class so don't worry about helping me cheat... i am just using a text book i got from ebrary.

    i would love some help... thanx!
    The compiler is right (as always): suppose you type something that can't represent an int (such as "foo"); the parseInt( ... ) method will throw an Exception and your code continues and tries to print out the value of something that uses the value of 'num1'; but num1 doesn't have a value ...

    kind regards,

    Jos

  3. #3
    GDW
    GDW is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    if i add something into the catch like num1 = 1 would that solve the problem?

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Technically, but that isn't the way that you want to do it. You should keep everything dealing with num1 together. So simply moving your System.out... to another location would work. Try messing around with that idea, I don't want to just give you the answer or you won't learn :P
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    Quote Originally Posted by GDW View Post
    if i add something into the catch like num1 = 1 would that solve the problem?
    Yup (try it, that's what computers are for). As long as num1 is initialized one way or another, the compiler will be happy. You have to ask yourself the question then: would 1 (one) be a fine default value of variable num1?

    kind regards,

    Jos

  6. #6
    GDW
    GDW is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanx!!! it worked. I am sure there is a better way to do it, but for now putting it into the catch worked.... so far i haven't learnt any other ways to solve it... but this is only my second day trying to learn java....

    how do i change the thread to say solved?

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

    Default

    Quote Originally Posted by GDW View Post
    Thanx!!! it worked. I am sure there is a better way to do it, but for now putting it into the catch worked.... so far i haven't learnt any other ways to solve it... but this is only my second day trying to learn java....

    how do i change the thread to say solved?
    It's somewhere in the thread tools menu (I apologize, I only asked a question once here). There's an easier way to make your variable an initialized variable:

    Java Code:
    int num1= 1;
    That line defines and initializes a variable num1. If it doesn't change its value the value will still be its initial value but at least it has an initialized value.

    kind regards,

    Jos

  8. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Simply putting your output statements inside the Try would have worked as well. The way you did it calls for extra lines of unnecessary code.
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Creating and initializing a SWT spinner widget
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:50 PM
  2. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:28 PM
  3. Initializing property object from a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:55 PM
  4. Initializing SAXParser
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-14-2007, 06:33 PM
  5. Initializing Arrays
    By bugger in forum New To Java
    Replies: 5
    Last Post: 11-09-2007, 09:43 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
  •