Results 1 to 9 of 9
  1. #1
    Bewitched1 is offline Member
    Join Date
    Jul 2010
    Location
    Simpsonville SC
    Posts
    3
    Rep Power
    0

    Default Need help with array and storing input

    I am working on a project that should allow user input and to store 10 midterm1 grades, 10 midterm2 grades, 10 final exam grades then add the 3 to find the minimum total grade and maximum total grade and display them to the user.

    I got the code running but it only allows 1 score input for each of the the 3 tests. How do I change the midterm1, midterm2, and final exam to an array that gets the variables from the user input?:(


    Java Code:
    import java.util.Scanner;
    
    public class Assign9_Cheshire
    
    {
    	public static void main(String[] args)
    	{
    		//Variable Declarations
    		Scanner keyboard = new Scanner(System.in);
    		int midTerm1=0;
    		int midTerm2=0;
    		int finalExam=0;
    		int averageScore;
    		int userInput;
    
    
    			//User Prompts
    			System.out.print("Please enter the Midterm 1 scores: ");
    			midTerm1 = keyboard.nextInt();
    			System.out.print("Please enter the Midterm 2 scores: ");
    			midTerm2 = keyboard.nextInt();
    			System.out.print("Please enter the Final Exam scores: ");
    			finalExam = keyboard.nextInt();
    
    			//Array to hold user input
    			int [] nums = new int [] {midTerm1, midTerm2, finalExam};
    			int max = 0;
    		   //To display highest score
    			for (int i = 0; i < nums.length; i ++)
    			{
    				if (nums[i] > max)
    				{
    					max = nums[i];
    				}
    
    			}
    			System.out.print("\nYour highest test score is: " + max);
    
    			//To display lowest score
    			int min = midTerm1;
    			for (int i = 0; i < nums.length; i++)
    			{
    				if(nums[i] < min)
    				{
    					min = nums[i];
    				}
    			}
    			System.out.print("\nYour lowest test score is: " + min);
    
    			//To calculate Average Score
    			averageScore = (midTerm1 + midTerm2 + finalExam)/3;
    
    	}
    
    }
    Thanks so much for any help.

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

    Default

    You would usually declare and initialize your arrays before getting input, and then use a for loop to get input repetitively to place in an array. For instance:

    Java Code:
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Fu1 {
       public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          
          int[] smallArray = new int[10];
          for (int i = 0; i < smallArray.length; i++) {
             System.out.print("Enter an int: ");
             smallArray[i] = scanner.nextInt();
             scanner.nextLine(); // to get and discard the nextline token
          }
          
          System.out.print("You have entered: ");
          System.out.println(Arrays.toString(smallArray));
       }
    }

  3. #3
    Bewitched1 is offline Member
    Join Date
    Jul 2010
    Location
    Simpsonville SC
    Posts
    3
    Rep Power
    0

    Default

    Ok...I was able (with the advice) to store the 3 different types of scores into arrays. I tried to add the scores to find the maximum score and minimum score but can not figure out how to add the 3 arrays to a final array then test the final array for the max and min. Below is what I have so far.
    :confused:


    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Assign9_Cheshire
    
    {
    	public static void main(String[] args)
    	{
    		//Variable Declarations
    		Scanner input = new Scanner(System.in);
    
    			//User Promt midterm1
    			int[]midTerm1= new int [10];
                for (int i = 0; i <midTerm1.length; i++){
                    System.out.println("Please enter a MidTerm1 grade:     ");
                    midTerm1[i] = input.nextInt();
                }
                 System.out.println("Thank you next");
    
                            //User prompt midterm2
                int[]midTerm2=new int[10];
                for(int j=0; j<midTerm2.length;j++){
                   System.out.println("Please enter a MidTerm2 grade:    ");
                   midTerm2[j] = input.nextInt();
                }
                System.out.println("Thank you next");
    
                        //User prompt final exam
                int[]finalExam = new int[10];
                for(int k = 0; k<finalExam.length; k++){
                	System.out.println("Please enter a Final Exam grade:   ");
                	finalExam[k] = input.nextInt();
                }
                int [] grades = new int [10];
    			int max = 0;
    			
    		   //To display highest score
    			for (int t = 0; t < grades.length; t ++)
    			{
    				if (grades[t] > max)
    				{
    					max = grades[t];
    				}
    				System.out.print("\nYour highest test score is: " + max);
    
    			//To display lowest score
    			int min = 100;
    				if(grades[t] < min)
    				{
    					min = grades[t];
    				}
    			}
    			System.out.print("\nYour lowest test score is: " + min);
    
    
    
              System.out.print("You have entered for midTerm1:    ");
              System.out.println(Arrays.toString(midTerm1));
              System.out.print("You have entered for midTerm2:    ");
              System.out.println(Arrays.toString(midTerm2));
              System.out.print("You have entered for Final Exam:   ");
              System.out.println(Arrays.toString(finalExam));
    
    
    			}
    
    			}

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

    Default

    First question to ask: Does your code compile? If not, are you familiar with variable "scope"? For instance that you cannot refer to the min variable if it was declared within a for loop and you try to use it after the for loop has concluded (compare with how you handle the max variable)?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    how to add the 3 arrays to a final array
    write an expression to add 3 variables and put the result in the fourth variable.
    Make all the variables array references and put it in a loop.

    In your search for max and min values, you could use the Integer class's MAX... and MIN... values.
    Your value of 100 for min might not be high enough.

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

    Default

    Also, as my example above shows, one trick of learning to program is to solve your current problem away from your large program but instead in isolation in a small program built specifically for this purpose. For instance, if I were trying to solve your problem, I'd create a simple program that has 3 arrays and in it I would try to add the items in two of the arrays and place the result in the 3rd array:

    Java Code:
    public class AddArrays {
      public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5};
        int[] arr2 = {5, 4, 3, 2, 1};
        int[] sum = new int[arr1.length];
        
        for (int i = 0; i < sum.length; i++) {
          //  sum[i] =    .... ? what one line of code would add each array item and place into sum?
        }
        
        for (int i = 0; i < sum.length; i++) {
          System.out.println("sum[" + i + "] = " + sum[i]);
        }
      }
    }

    Once I've solved this, it would be trivial to use this logic in my main program.

  7. #7
    Bewitched1 is offline Member
    Join Date
    Jul 2010
    Location
    Simpsonville SC
    Posts
    3
    Rep Power
    0

    Default

    The program compiled before I tried adding the last array to sum up the 3 tests and find the min and max.

    I tried this one that is an example from my book but it gave me a bunch of error codes.

    int []grades= new int {midTerm1, midTerm2, finalExam}

    and
    []grades=new int {midterm1[10, midTerm2[10],finalExam[10]}

    I think I have tried so many combination's I am totally lost now.

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

    Default

    You can't just add the arrays by declaring them that way, you have to use a for loop (please look at my post above) to add each individual item in the two arrays and place in the item of the third array, the one holding the sum. I have a feeling that you still don't fully understand how arrays work, and that gaining this understanding will help you the most. Please re-read your notes, text and tutorials on arrays as it will help you greatly.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Bewitched1 View Post
    int []grades= new int {midTerm1, midTerm2, finalExam}

    and
    []grades=new int {midterm1[10, midTerm2[10],finalExam[10]}

    I think I have tried so many combination's I am totally lost now.
    You can say that again because the line(s) above aren't even syntactically legal Java. If you have to add the elements of two arrays A and B together and make up an array of the sums, i.e. sum[i]= A[i]+B[i], you have to use a loop to add all the individual elements and store them in the corresponding element of your array 'sum'. Assuming all arrays A, B and sum have equal length, the following code snippet does the job:

    Java Code:
    for (int i= 0; i < A.length; i++)
       sum[i]= A[i]+B[i];
    See? No magic involved nor any mysterious notation; Java is a simple language.

    kind regards,

    Jos

Similar Threads

  1. standard input stream storing to a generic method?
    By vendetta in forum New To Java
    Replies: 3
    Last Post: 01-29-2010, 08:13 PM
  2. Replies: 3
    Last Post: 10-28-2009, 01:45 AM
  3. Storing a record set into an array
    By DJCali in forum New To Java
    Replies: 2
    Last Post: 10-25-2009, 02:47 PM
  4. Storing in an Array
    By Bascotie in forum New To Java
    Replies: 10
    Last Post: 10-15-2009, 05:12 AM
  5. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 07:32 AM

Tags for this Thread

Posting Permissions

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