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

Thread: What is wrong with my I/O code?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with my I/O code?

    Hello friends;

    It is Joe here again with ; just finished my first chapter but got stuck on one of my challenges. I am not using any IDE i am using command line, and the compiler returns an error: "The local variable userNum may not have been initialized"

    My questions are:
    1 - How do i make my code work?
    2 - How do i initialize that variable and is it really necessary?
    3 - There must be a way to shorten the variable declaration of int from 1 to 12, which one is it?
    4 - Is it necessary to close the reader variable with reader.close() in this case? Because i have not been using it with strings exercises I/O and it does not give any problems.

    Thank you for your help. My challenge bellow:

    "Write an application that accepts a number from the user and prints its multiplication table values (multiply it by 1 through 12 and print the results)"

    The following is the application i am writing:

    import java.io.*;
     
    public class multiplicationInput
    {
       public static void main (String[] arguments)
       {
          int userNum; //variable for user input
          int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12; //variables with set values from 1 to 12
          BufferedReader reader;
     
          reader = new BufferedReader (new InputStreamReader (System.in));
     
          num1 = 1;
          num2 = 2;
          num3 = 3;
          num4 = 4;
          num5 = 5;
          num6 = 6;
          num7 = 7;
          num8 = 8;
          num9 = 9;
          num10 = 10;
          num11 = num10 + num1;
          num12 = num11 + num2;
     
          try
          {
             System.out.print ("\nEnter a number for multiplication:"); //Output asking for user to input a number
             userNum = Integer.parseInt (reader.readLine( )); //The number the user input is assigned to the userNum variable
          }
     
          catch (IOException ioe) //Handling input/out exception errors
          {
             System.out.println ("I/O Exception error has occurred, using 1...");
             num1 = num2 = 1;
          }
     
     
          /*
           * was supposed to handle number format exception, but i will ignore it for now until application is working
           * The code bellow is how i wanted to show the multiplication table with the user entered number 
           * multiplied by the set values and the result displied. Once i know exactly where my code is failing
           * will continue with other lines until multiplied by 12
          */
          System.out.println (num1 + " x " + userNum + " = " + (num1 * userNum));
     
       }
     
    }

    Joe
    Last edited by jps; August 27th, 2013 at 03:02 AM. Reason: code tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is wrong with my I/O code?

    When posting code, please use the highlight tags to preserve formatting.

    This looks like a job for an array.

    What happens if your readline() method throws an Exception? What value should userNum take, since you haven't initialized it to anything?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my I/O code?

    So, what you are saying is that since i have not initialized the userNum, i must handle and exception for readline() method? How do i do that?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is wrong with my I/O code?

    Quote Originally Posted by shimazaky View Post
    So, what you are saying is that since i have not initialized the userNum, i must handle and exception for readline() method? How do i do that?
    That's not at all what I'm saying. Consider this code:

    public class Test{
       public static void main(String... args){
          String s;
          boolean t = false;
          if(t){
             s = "test";
          }
          System.out.println(s);
       }
    }

    Try to compile it, and you'll get a similar error. The reason for this error is this: what should be printed?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: What is wrong with my I/O code?

    Hello.
    You have initialized the variable in the try block. There is a possibility that exception could occur in the try block. Of course, in your case it is not. But for JVM it could be. If the exception occurs it will jump to catch block and thereby follows the latter code.
    If because of exception the initialization of local variable fails to execute then what will JVM print.
    Hence the compile-time error.
    Hope you know how to fix it.

    Syed.

  6. #6
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my I/O code?

    [QUOTE=syedbhai;120594]Hello.
    You have initialized the variable in the try block. There is a possibility that exception could occur in the try block. Of course, in your case it is not. But for JVM it could be. If the exception occurs it will jump to catch block and thereby follows the latter code.
    If because of exception the initialization of local variable fails to execute then what will JVM print.
    Hence the compile-time error.
    Hope you know how to fix it.

    Syed.[/If because of exception the initialization of local variable fails to execute then what will JVM print.
    Hence the compile-time error.
    Hope you know how to fix it.]

    We are getting somewhere, do you perhaps know how to fix it?

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: What is wrong with my I/O code?

    You were told what is wrong in post #2, then it was explained in post #4, and told what was wrong again in post #5.
    If you do not understand the replies, ask a question.
    "How to fix it" is not a question we give an answer to here. That is for you to figure out.
    The problem is, it is possible the variable never gets a value before it is used.
    The solution is to be sure the variable gets a value through all possible code paths.

  8. The Following User Says Thank You to jps For This Useful Post:

    KevinWorkman (August 27th, 2013)

  9. #8
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my I/O code?

    Ok, guys. I figured it out, thank you.

    The trick was to put the last print statement inside the try block and the code works nicely, so i can finish the application.

  10. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is wrong with my I/O code?

    Quote Originally Posted by shimazaky View Post
    The trick was to put the last print statement inside the try block
    No that is an awful hack. The correct solution was to initialise the variable correctly.
    Improving the world one idiot at a time!

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is wrong with my I/O code?

    Quote Originally Posted by Junky View Post
    No that is an awful hack. The correct solution was to initialise the variable correctly.
    Eh, I could see putting the print statement inside the try block.

    The correct thing to do is to actually handle errors. The problem at hand is: what if the user enters text instead of a digit? That's what might cause an Exception, and what should happen in that case? Only printing out the answer if the user enters a digit sounds reasonable, although the next logical step is to do correct error handling and prompt the user for another digit.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is wrong with my I/O code?

    Yeah my reply was a bit short. The point I was trying to make was that simply moving a line of code to get rid of an error message instead of dealing with that error correctly is not a good approach. Similar to declaring everything static to get around the "cannot access non-static from a static" error (whatever the full text is).
    Improving the world one idiot at a time!

  13. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is wrong with my I/O code?

    Yeah, eclipse's auto-fix of that static error is the bane of my existence.

    It's a larger problem of "oh, this random thing that I don't understand seemed to make the problem go away, let's move on" without actually understanding the problem. That just leads to bigger headaches down the road.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. What's Wrong With My Code?
    By markchua in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2013, 03:20 AM
  2. What is wrong with my code?
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 28th, 2013, 05:44 PM
  3. can someone tell me what's wrong with my code
    By Nate08 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 23rd, 2012, 05:07 PM
  4. What's Wrong With My Code?
    By Nuggets in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 31st, 2012, 10:11 PM
  5. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 03:38 AM