# Problem with finding value from array

• June 7th, 2013, 03:45 PM
Alexryu
Problem with finding value from array
So I have to write a simple program for homework that adds together different grades and then finds the students with the min and max grade using a different class and a constructor. I have everything working except the findMin method. It always returns 0, the findMax method works correctly though. I feel like I'm missing something simple, but not quite sure what it is. Any help would be appreciated, thanks. :confused:

Here is what I have:
Code :

```import java.util.Scanner; import java.util.Arrays;   class ScanArray { private static int[] finalArray = null; private static int minGrade = 0, maxGrade = 0, arrayNumberMax = 0, arrayNumberMin = 0;   public ScanArray(int[] array) { finalArray = array; }   public static void findMax() { for (int i = 0; i < finalArray.length; i++) { if (maxGrade < finalArray[i]) { maxGrade = finalArray[i]; arrayNumberMax = i; } } System.out.println("The highest score is: " + maxGrade + "/300, which is student number " + arrayNumberMax + "."); }   public static void findMin() { for (int i = 0; i < finalArray.length; i++) { if (minGrade > finalArray[i]) { minGrade = finalArray[i]; arrayNumberMin = i; } } System.out.println("The lowest score is: " + minGrade + "/300, which is student number " + arrayNumberMin + "."); } }   class Assign7_Sniffin { public static int [] termType, midTerm_1 = new int[10], midTerm_2 = new int[10], finalExam = new int[10], allGrades = new int[10]; public static int termInput; public static String termName;   public static void main(String[] args) { //Process the input of grades for (int i = 0; i <= 2; i++) { termInput = i; inputTypes(); }   //Add the grades together and find min and max using scanarray class addGrades();   ScanArray grades = new ScanArray(allGrades); ScanArray.findMax(); ScanArray.findMin(); }   //Switch terms public static void inputTypes() { switch (termInput) { case 0: termName = "Midterm 1"; termType = midTerm_1; break;   case 1: termName = "Midterm 2"; termType = midTerm_2; break;   case 2: termName = "Final exam"; termType = finalExam; break;   default: System.out.println("Error in term type."); } gatherArray(); System.out.print(termName + " grades: "); System.out.println(Arrays.toString(termType)); }   public static void gatherArray() { try { Scanner input = new Scanner(System.in); for (int i = 0; i < termType.length; i++) { System.out.print("Enter " + termName + " grade for student number " + i + ": "); termType[i] = input.nextInt(); } } catch(Exception e) { System.out.println("Invalid input."); } }   private static void addGrades() { for (int i = 0; i < allGrades.length; i++) { allGrades[i] = (midTerm_1[i] + midTerm_2[i] + finalExam[i]); } } }```
• June 7th, 2013, 06:36 PM
jps
Re: Problem with finding value from array
Try using println statements to see the value of the variables in various places to see what is taking place as the code runs.
You say the method always returns 0. Think about that as you check the variables.
Try to find where the value 0 is coming from.
Pay attention to the areas that should change that value to something other than 0.
• June 10th, 2013, 02:15 AM
garymar2013
Re: Problem with finding value from array
I think you should not initialize the variable minGrade = 0 because this values is the minimum value of all students' grade unless there is actaully a student who got "0". To initialize minGrade is supposed to be the fist value of that array. Then use that value to compare with the rest elements in that array.