Results 1 to 8 of 8
  1. #1
    Always.Smile is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default 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:

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    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.

  3. #3
    Always.Smile is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    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");}
        	}
    	}

  4. #4
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    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 04:59 PM. Reason: content

  5. #5
    Always.Smile is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by racerxadam View Post
    You just need to initialize your two float variables when they are created.

    Java Code:
    float num11 = 0, num22 = 0;
    But then the sum is 0+0... though I passed values for num11 and num22

  6. #6
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    You pass num11 and num22 to fun
    Java Code:
    fun(num11,num22);
    but you never assign them a value before doing so. You try to do it inside fun(), but that won't work.

    You should re-read the chapters about local variables and where (and how long) they live.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by ojn View Post
    You should re-read the chapters about local variables and where (and how long) they live.
    Yes, it must. Read more about variable declarations.

    You can use any local variable before initialization. They are not automatically assign to default values. Good practice is, whatever variables you use, initialize them before use, depends on your design.

  8. #8
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    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

  1. Error occured during initialization of VM
    By red in forum New To Java
    Replies: 4
    Last Post: 10-02-2008, 08:08 PM
  2. 2D Array Initialization
    By M77 in forum Advanced Java
    Replies: 3
    Last Post: 06-04-2008, 03:12 PM
  3. initialization value problem
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 11:54 AM
  4. Lazy Initialization
    By onegcr in forum New To Java
    Replies: 1
    Last Post: 08-14-2007, 04:29 PM
  5. log4j initialization
    By arfatkhan in forum Web Frameworks
    Replies: 3
    Last Post: 08-10-2007, 08:42 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
  •