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

Thread: Problem with finding value from array

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

    Question 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.

    Here is what I have:
    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]);
    		}
    	}
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Finding the largest object in an array
    By ashl7 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2013, 06:57 PM
  2. finding and deleting from an array?
    By 93tomh in forum Java Theory & Questions
    Replies: 19
    Last Post: August 13th, 2012, 09:39 AM
  3. finding specified NUMBERS in an array
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 10:35 PM
  4. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM