Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: 1 error???

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation 1 error???

    i keep on getting the same error again.

    i was wondering if someone could kindly take the time to look over this simple error, please.

    it says:
    : variable i might not have been initialized
    while(i<=5)
    ^
    1 error

    Process completed.


    my program is:

    import java.lang.String;
    import java.util.Scanner;
    public class WeCanWorkItOut
    {
        public static void main(String[] args) 
        {    
        Scanner input=new Scanner(System.in);
        System.out.println("Please enter a number");
        double num1=input.nextDouble();
        System.out.println("Please enter a second number");
        double num2=input.nextDouble();
        System.out.println("Please enter a third number");
        double num3=input.nextDouble();
        System.out.println("Please enter a fourth number");
        double num4=input.nextDouble();
        System.out.println("Please enter a fifth and final number");
        double num5=input.nextDouble();
     
        if(num1<num2)
        if(num2<num3)
        if(num3<num4)
        if(num4<num5)
        {
        System.out.println(" The first number entered is the smallest number. ");
        }
        else
        {
        System.out.println(" Some of the numbers might be incorrect. " 
        					+ " Enter other numbers. ");
        }
     
     
     
     
        if(num1>num2)
        if(num2>num3)
        if(num3>num4)
        if(num4>num5)
        {
        System.out.println(" The first number entered is the largest number. ");
        }
        else
        {
        System.out.println(" Some of the numbers might be equal. ");
        }
     
     
        if(num1<=num2) 
        if(num2<=num3) 
        if(num3<=num4)
        if(num4<=num5) 
        {
        System.out.println(" There isn't a number with the lowest value as some or all of these numbers may be equal. ");
        }
        else
        {
        System.out.println(" Try entering other numbers. ");
        }
    													// indefinite= greater than or equal to //											
        if(num1>=num2)
        if(num2>=num3)
        if(num3>=num4)
        if(num4>=num5)
        {
        System.out.println(" There isn't a number with the highest value as some or all of the values may be equal. ");
        }
        else
        {
        System.out.println(" Try entering other numbers. ");
        }
     
        double average;
        average= (num1+num2+num3+num4+num5)/5;
        System.out.println ("Average:"+average);
     
        int largest=Integer.MIN_VALUE;
        int smallest=Integer.MAX_VALUE;
        int compared=0;
        int i;
        Scanner scan=new Scanner(System.in);
        while(i<=5)
        {
        	System.out.println ("Please enter a value.");	
        	compared=scan.nextInt();
     
        if(compared>largest)	
        {
        	largest=compared;
        }
        if(compared<smallest)
        {
        	smallest=compared;
        }
     
     
        i++;
        }
        System.out.println (smallest);
        System.out.println (largest);
     
        }
     
    }
    Last edited by helloworld922; November 5th, 2009 at 09:15 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: 1 error???

    Those errors tell you a heck of a lot about what's happening. In this case its alluding to variable i...and telling you it isn't initialized. Look at the code, "int i;" You then try to evaluate i in your while(), but if there is no initial value of i how can it evaluate it?

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation

    i did that several times and then i got two errors so what should happen?
    Last edited by helloworld922; November 5th, 2009 at 09:16 PM.

  4. #4
    Junior Member
    Join Date
    Sep 2009
    Location
    Mount Morgan, Queensland, Australia
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 1 error???

    int i = 0;
    Last edited by helloworld922; November 5th, 2009 at 09:16 PM.