Results 1 to 2 of 2
  1. #1
    outforlunch is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Local variable might not have been initialized

    here is my code, its a 3 number calculator, bit i get this error when ever "int num3" is used.

    this is the error i get "The local variable num3 may not have been initialized"

    PHP Code:
    import java.util.*;
    
    
    public class calc
    				{
    	
    public static void main(String args[])
    			{
    	Scanner m = new Scanner (System.in);
    	Scanner s = new Scanner (System.in);
    	int x = 5;
    	
    	int num1, num2, num3;
    	
    	
    	String mod;
    	String mod2;
    	String r;
    	
    	int y = 0;
    	do {
    	
    		
    	System.out.println("Please Enter 1st Number:");
    	num1 = s.nextInt();
        System.out.println("Please enter *,/,-,+.");
        mod = m.next();
    	System.out.println("Please Enter 2nd Number:");
        num2 = m.nextInt();
        System.out.println("Please chose *,/,-,+.");
        mod2 = m.next();
        System.out.println("Please choses 3rd Number.");
        
        
        
        if( mod.equals("+"))// addition 
        {
        	System.out.println("Please chose *,/,-,+.");
            mod2 = m.next();
            System.out.println("Please choses 3rd Number.");
        	if(mod2.equals("+")){
        		System.out.println(num1 + num2 + num3);
        	}
        	if(mod2.equals("-")){
        		System.out.println(num1 + num2 - num3);
        	}
        	if(mod2.equals("/")){
        		System.out.println(num1 + num2 / num3);
        	}
        	if(mod2.equals("*")){
        		
        		System.out.println(num1 + num2 * num3);
        	}
        }
        
        if(mod.equals("-"))//subtaction
        {
        	
        	System.out.println("Please chose *,/,-,+.");
            mod2 = m.next();
            System.out.println("Please choses 3rd Number.");
        	if(mod2.equals("+")){
        		System.out.println(num1 - num2 + num3);
        	}
        	if(mod2.equals("-")){
        		System.out.println(num1 - num2 - num3);
        	}
        	if(mod2.equals("/")){
        		System.out.println(num1 - num2 / num3);
        	}
        	if(mod2.equals("*")){
        		System.out.println(num1 - num2 * num3);
        	}
        }
    	if(mod.equals("/"))//so on
    	{
    		System.out.println("Please chose *,/,-,+.");
    	    mod2 = m.next();
    	    System.out.println("Please choses 3rd Number.");
    	    if(mod2.equals("+")){
        		System.out.println(num1 / num2 + num3);
        	}
        	if(mod2.equals("-")){
        		System.out.println(num1 / num2 - num3);
        	}
        	if(mod2.equals("/")){
        		System.out.println(num1 / num2 / num3);
        	}
        	if(mod2.equals("*")){
        		System.out.println(num1 * num2 * num3);
        		
        	}
    	}
    	if(mod.equals("*"))//and so forth
    	{
    		System.out.println("Please chose *,/,-,+.");
    	    mod2 = m.next();
    	    System.out.println("Please choses 3rd Number.");
    	    if(mod2.equals("+")){
        		System.out.println(num1 * num2 + num3);
        	}
        	if(mod2.equals("-")){
        		System.out.println(num1 * num2 - num3);
        	}
        	if(mod2.equals("/")){
        		System.out.println(num1 * num2 / num3);
        	}
        	if(mod2.equals("*")){
        		System.out.println(num1 * num2 * num3);
        		
        	}
    	}
    	
    	System.out.println("");
    	System.out.println("Run agian? y/n");
    	 r = s.next();
    	if (r.equals("n")) {
    	System.exit(0);
    	}
    
    	}
    	while (y < 1);
    			}
    				}

  2. #2
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: Local variable might not have been initialized

    The local variable num3 has only been declared but never assigned a value. Read your code again.

Similar Threads

  1. Variable may not have been initialized
    By gerchan in forum New To Java
    Replies: 1
    Last Post: 09-06-2012, 02:17 PM
  2. Variable not initialized?
    By seegee in forum New To Java
    Replies: 2
    Last Post: 07-15-2011, 02:00 PM
  3. Variable not initialized.
    By miss_peaches in forum New To Java
    Replies: 14
    Last Post: 04-27-2011, 09:28 AM
  4. variable name might not have been initialized
    By p0rnstar in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 03:27 PM
  5. Not initialized variable
    By bbq in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •