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 5 of 5

Thread: Need help please

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help please

    I have a program I am trying to write and I can not figure it out. I have to create a multi dimensional array and find the max and min values of the arrays. Here is my code;

    import java.util.Scanner;
    import java.util.Arrays;
     
    public class Assign10_Key
    {   public static void main(String[] args)
        {
     
            Scanner input = new Scanner(System.in);
     
            double[]midTerm1= new double [10];
            for (int A = 0; A <midTerm1.length; A++){
                System.out.println("Please enter MidTerm1 grade:");
                midTerm1[A] = input.nextInt();
            }
            System.out.println("Now");
     
            double[]midTerm2=new double[10];
            for(int B=0; B<midTerm2.length;B++){
                System.out.println("Please enter a MidTerm2 grade:");
                midTerm2[B] = input.nextInt();
            }
            System.out.println("Now");
     
            double[]finalExam = new double[10];
            for(int C = 0; C<finalExam.length; C++){
                System.out.println("Please enter a Final Exam grade:");
                finalExam[C] = input.nextInt();
            }
            int [] grades = new int [10];
     
            int max = 0;
     
     
     
            for (int t = 0; t < grades.length; t ++)
            {
                if (grades[t] > max)
                {   max = grades[t];
                }
     
    			 int min = 0;
     
                if(grades[t] < min)
                {
                    min = grades[t];
                }
     
            }
     
            System.out.print("Your highest test score is: " + max);
            System.out.print("Your lowest test score is: " + min);
            System.out.print("You have entered following grades for midTerm1:    ");
            System.out.println(Arrays.toString(midTerm1));
            System.out.print("You have entered following grades for midTerm2:    ");
            System.out.println(Arrays.toString(midTerm2));
            System.out.print("You have entered following grades for Final Exam:   ");
            System.out.println(Arrays.toString(finalExam));
     
        }
    }
    and this is my error....

    C:\Users\Penny\Desktop\CPT187\Assign10_Key.java:56 : cannot find symbol
    symbol : variable min
    location: class Assign10_Key
    System.out.print("Your lowest test score is: " + min);

    Could someone please help??

    Thanks!

  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help please

    You have a scope issue. You have declared min inside the for loop and trying to use it outside the loop. You cannot do this. Just like a method cannot access a variable declared inside another method. Why not declare min at the same time as max?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help please

    Thank you, I don't know why I did not catch that. I declared min at same time as max, and it compiles. I run it and it works, until it is supposed to give me the min and max and they come up as 0. Any ideas?


    Please enter MidTerm1 grade:
    120
    Please enter MidTerm1 grade:
    99
    Please enter MidTerm1 grade:
    99
    Please enter MidTerm1 grade:
    99
    Please enter MidTerm1 grade:
    87
    Please enter MidTerm1 grade:
    78
    Please enter MidTerm1 grade:
    87
    Please enter MidTerm1 grade:
    76
    Please enter MidTerm1 grade:
    67
    Please enter MidTerm1 grade:
    76
    Now
    Please enter a MidTerm2 grade:
    77
    Please enter a MidTerm2 grade:
    88
    Please enter a MidTerm2 grade:
    99
    Please enter a MidTerm2 grade:
    89
    Please enter a MidTerm2 grade:
    98
    Please enter a MidTerm2 grade:
    87
    Please enter a MidTerm2 grade:
    87
    Please enter a MidTerm2 grade:
    89
    Please enter a MidTerm2 grade:
    97
    Please enter a MidTerm2 grade:
    87
    Now
    Please enter a Final Exam grade:
    88
    Please enter a Final Exam grade:
    99
    Please enter a Final Exam grade:
    77
    Please enter a Final Exam grade:
    78
    Please enter a Final Exam grade:
    87
    Please enter a Final Exam grade:
    89
    Please enter a Final Exam grade:
    98
    Please enter a Final Exam grade:
    97
    Please enter a Final Exam grade:
    79
    Please enter a Final Exam grade:
    99
    Your highest test score is: 0Your lowest test score is: 0You have entered follow
    ing grades for midTerm1: [120.0, 99.0, 99.0, 99.0, 87.0, 78.0, 87.0, 76.0, 67
    .0, 76.0]
    You have entered following grades for midTerm2: [77.0, 88.0, 99.0, 89.0, 98.0
    , 87.0, 87.0, 89.0, 97.0, 87.0]
    You have entered following grades for Final Exam: [88.0, 99.0, 77.0, 78.0, 87.
    0, 89.0, 98.0, 97.0, 79.0, 99.0]
    Press any key to continue . . .

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help please

    All you inputs are stored in midTerm1 array, midTerm2 array and finalExam array. Yet you look for min and max in the grades array. Since you did not store any values in this array it will be filled with the default value of 0.

    Even if you sort that problem out you will have another problem with min. You initialise min with 0. Is 99 < 0? Is 56 < 0? is 65 < 0? etc
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Location
    toronto
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help please

    cuz your questions might be your assignment i can't show you exact code but anyway

    here are some tips!

    first, if you wanna compare values in the grades array then you need to store some values. easy way to store values is to store values when user makes some inputs like 'input.nextInt();'

    second, you better store reasonable values for max and min. if you keep min 0 then nothing can be less than 0 (unless user stores minus smth). i can suggest first grade.

    Third, you should not compare int values and double values. you have different types of arrays (grades is int type, others are double...) i prefer to change arrays as same type to compare easily.

    hope it's helpful and hope you finish the code well