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