Results 1 to 6 of 6
  1. #1
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Exclamation Variables might not have been initialized

    I am new to java, and I am writing a simple program with math operations using JOptionPane. When compiling, I am getting two errors saying that the variables might not have been initialized, pointing to lines 54 and 55. Can anyone give me an idea as to where to go to fix this?


    Java Code:
    import java.util.*;
    import javax.swing.JOptionPane;
    
    public class P2_TreyNixon
    {
    	public static void main(String[] args)
    	{
    		int num1, num2, num3, sum, avg, prod, smallest, largest;
    		String first, second, third;
    		
    		JOptionPane.showMessageDialog(null, "This program will perform simple math and compare 3 integers.\nPlease select 3 numbers.");  
    											  
    		first = 	JOptionPane.showInputDialog(null, "Enter first integer:", "Integer 1", JOptionPane.INFORMATION_MESSAGE);
    		num1 = Integer.parseInt(first);	
    		
    		second = JOptionPane.showInputDialog(null, "Enter second integer:", "Integer 2", JOptionPane.INFORMATION_MESSAGE);
    		num2 = Integer.parseInt(second);
    		
    		third = JOptionPane.showInputDialog(null, "Enter third integer:", "Integer 3", JOptionPane.INFORMATION_MESSAGE);
    		num3 = Integer.parseInt(third);
    		
    		//Perform math operations
    		sum = (num1 + num2 + num3);
    		avg = (sum/3);
    		prod = (num1 * num2 * num3);
    		
    		//Find smallest integer
    		if(num1 < num2 && num1 < num3)
    			smallest = num1;
    		else if(num2 < num3 && num2 < num1)
    			smallest = num2;
    		else if(num3 < num1 && num3 < num2)
    			smallest = num3;
    			
    					
    		//Find largest integer
    		if(num1 > num2 && num1 > num3)
    			largest = num1;
    		else if(num2 > num3 && num2 > num1)
    			largest = num2;
    		else if(num3 > num1 && num3 > num2)
    			largest = num3;
    		
    		
    		JOptionPane.showMessageDialog(null, "The sum of the integers is " + sum +  ".", "SUM", JOptionPane.INFORMATION_MESSAGE);
    		JOptionPane.showMessageDialog(null, "The average of the integers is " + avg + " .", "AVERAGE", JOptionPane.INFORMATION_MESSAGE);
    		JOptionPane.showMessageDialog(null, "The product of the integers is " + prod + " .", "PRODUCT", JOptionPane.INFORMATION_MESSAGE);
    		JOptionPane.showMessageDialog(null, "The smallest of the integers is " + smallest + " .", "SMALLEST", JOptionPane.INFORMATION_MESSAGE);
    		JOptionPane.showMessageDialog(null, "The largest of the integers is " + largest + " .", "LARGEST", JOptionPane.INFORMATION_MESSAGE);
    	}
    	
    }

    Moderator Edit: Code tags added
    Last edited by Fubarable; 01-16-2011 at 11:07 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    1) Which lines are causing the errors? We can't tell since your line numbers may differ from ours.

    2) You may try fix this by initializing the variables noted on the offending lines.


    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

    Good Luck and welcome to the forum!

  3. #3
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    The errors are reffering to the last two lines, when I display the largest and smallest integers.

  4. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    HI, you need to initialize your largest and smallest when you declared them. You are assigning them values only in the last few if statements. However, the compiler is complaining because what if those if statements are never entered? Then what will the value of smallest and largest be? This is the problem.

    If you are sure that a user will enter 3 numbers into your program, then you can maybe initialize the smallest to the largest possible integer Integer.MAX_VALUE and initialize largest to the smallest possible int Integer.MIN_VALUE - this will guarantee that if the users enters 3 values, they will definitely fall within the initial values of smallest and largest.

    So, simply change your declaration line to the following:
    Java Code:
    int num1, num2, num3, sum, avg, prod, smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE;
    Then, it should work.

    Best,
    --user0--

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Yeah,... what he said. :)

  6. #6
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

Similar Threads

  1. variable name might not have been initialized
    By p0rnstar in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 03:27 PM
  2. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  3. Replies: 0
    Last Post: 04-20-2009, 11:08 AM
  4. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 05:00 AM
  5. Not initialized variable
    By bbq in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:02 AM

Posting Permissions

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