Results 1 to 8 of 8
Thread: Initialization Error
- 10-11-2008, 03:16 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
Initialization Error
Hi....
This is the function:
static void sumFun (float num1, float num2) throws IOException
{
BufferedReader read = new BufferedReader( new InputStreamReader(System.in));
String strNum1, strNum2;
System.out.println("Enter the first number: ");
strNum1 = read.readLine();
System.out.println("Enter the second number: ");
strNum2 = read.readLine();
num1 = Float.parseFloat(strNum1);
num2 = Float.parseFloat(strNum2);
}
This is in the main>> switch statement>> case1:
case 1:
sumFun(num11,num22);
System.out.println("The sum of " + (num11 + num22));
break;
And I have defined these before the switch statement:
float num11, num22;
But I received this error:
variable num11 might not have been initialized
variable num22 might not have been initialized
What's the wrong??
:confused::confused:
- 10-11-2008, 03:27 PM #2
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
Your main method has no idea what the values of num1 and num2 are.
Also, when you post code, post it all, and also use the CODE (or even better, PHP) tags, to make it more readable.
- 10-11-2008, 03:49 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
Okay... this is my code...
PHP Code:import java.lang.Math; import java.io.*; public class TestDfunction { static void fun(float num1, float num2) throws IOException { BufferedReader read = new BufferedReader( new InputStreamReader(System.in)); String strNum1, strNum2; System.out.print("Enter the first number: "); strNum1 = read.readLine(); num1 = Float.parseFloat(strNum1); System.out.print("Enter the second number: "); strNum2 = read.readLine(); num2 = Float.parseFloat(strNum2); } static void menu() { System.out.println("Please choose the type of operation needs to be computed by pressing the corresponding number: "); System.out.println("----------------------------------------------------------------------------------------------"); System.out.println(" 1. Addition"); System.out.println(" 2. Subtraction"); System.out.println(" 3. Multiplication"); System.out.println(" 4. Division"); System.out.println(" 5. Remainder"); System.out.println(" 6. Minimum"); System.out.println(" 7. Maximum"); System.out.println("----------------------------------------------------------------------------------------------"); } public static void main(String[] args) { menu(); try{ BufferedReader read = new BufferedReader( new InputStreamReader(System.in)); float num11, num22; String strChoose; int choose; strChoose = read.readLine(); choose = Integer.parseInt(strChoose); System.out.println(""); while ((choose <1) || (choose >8)) { System.out.println(); System.out.println("The number you have entered is invalid..."); menu(); strChoose = read.readLine(); choose = Integer.parseInt(strChoose); } switch (choose) { case 1: fun(num11,num22); System.out.println("The sum is: "+ (num11 + num22)); break; case 2: fun(num11,num22); System.out.println("The diferrence is: " + (num11 - num22)); break; case 3: fun(num11,num22); System.out.println("The product is: " + (num11 * num22)); break; case 4: fun(num11,num22); System.out.println("The quotion is: " + (num11 / num22)); break; case 5: fun(num11,num22); System.out.println("The remainder is: " + Math.IEEEremainder(num11,num22)); break; case 6: fun(num11,num22); System.out.println("The smaller value is: " + Math.min(num11,num22)); break; case 7: fun(num11,num22); System.out.println("The greater value is: " + Math.max(num11,num22)); break; case 8: System.exit(0); } } catch (IOException e) {System.out.println("IO Error");} } }
- 10-11-2008, 03:58 PM #4
Member
- Join Date
- Jul 2008
- Posts
- 68
- Rep Power
- 0
You just need to initialize your two float variables when they are created.
Java Code:float num11 = 0, num22 = 0;
Last edited by racerxadam; 10-11-2008 at 03:59 PM. Reason: content
- 10-11-2008, 04:04 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
- 10-11-2008, 04:19 PM #6
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
You pass num11 and num22 to fun
but you never assign them a value before doing so. You try to do it inside fun(), but that won't work.Java Code:fun(num11,num22);
You should re-read the chapters about local variables and where (and how long) they live.
- 10-12-2008, 06:12 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-12-2008, 02:04 PM #8
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
Hopefully this can get you on your way. You need to add a lot of stuff yourself and it can probably be done more efficient, but hey.. No one can do it all for you.
PHP Code:import java.util.Scanner; public class MathProgram { // I prefer using the Scanner class here. Use whatever you prefer. private Scanner sc = new Scanner(System.in); public void menu() { // Prints the menu. } public void doTheMath() { /* * Add try/catch and do/while blocks as you like. */ float result = 0; // Whatever the result will be. menu(); // Print the choices. int choice = // However you get an int from Scanner of BufferedReader /* * Ask for 2 numbers here. */ switch (choice) { case 1: result = // Whatever's on option 1. System.out.print("The <some math thing here> is: "); break; } System.out.println(result); } public static void main(String[] args) { MathProgram mp = new MathProgram(); mp.doTheMath(); } }
Similar Threads
-
Error occured during initialization of VM
By red in forum New To JavaReplies: 4Last Post: 10-02-2008, 07:08 PM -
2D Array Initialization
By M77 in forum Advanced JavaReplies: 3Last Post: 06-04-2008, 02:12 PM -
initialization value problem
By ravian in forum New To JavaReplies: 2Last Post: 01-28-2008, 10:54 AM -
Lazy Initialization
By onegcr in forum New To JavaReplies: 1Last Post: 08-14-2007, 03:29 PM -
log4j initialization
By arfatkhan in forum Web FrameworksReplies: 3Last Post: 08-10-2007, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks