Results 1 to 16 of 16
  1. #1
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default help w/ storing/scanning numbers in arrays

    these are my instructions
    A Computer Technology Instructor has a small class of 10 students. The instructor evaluates the performance of students in the class by administering 2 midterm tests and a Final Exam.

    Write a program that prompts the instructor to enter the 10 grades of Midterm 1 and store these numbers in an array. Next prompt for the 10 grades of Midterm2 and store these numbers in a different array. Next prompt for the 10 grades of the Final Exam and store these in a different array. Next add Midterm1 to Midterm2 to Final and store the total of grades in a different array. Next, scan the array that has the totals and identify the minimum grade and maximum grade. Inform the instructor of the minimum grade and maximum grade.

    Note : do not assume that the grades are in the range 0 to 100. Your program should function properly whether the grades are in the range 0 to 100 or any other range.
    *I'm using eclipse btw

    it prompts me to enter my numbers and stores them in the array correctly but when it tells me what my lowest score it says 0.0 and my highest score is 0.0...also, im not sure exactly how to add the total of midterm1 to midterm2 to final exam or how scan the array..and it repeats my highest and lowest score

    so far this is my output after i enter the grades

    Your highest test score is: 0Your lowest test score is: 0Your highest test score is: 0Your lowest test score is: 0Your highest test score is: 0Your lowest test score is: 0Your highest test score is: 0Your lowest test score is: 0Your highest test score is: 0Your lowest test score is: 0Your highest test score is: 0Your lowest test score is: 0Your highest test score is: 0Your lowest test score is: 0Your highest test score is: 0Your lowest test score is: 0Your highest test score is: 0Your lowest test score is: 0Your highest test score is: 0Your lowest test score is: 0You have entered following grades for midTerm1: [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
    You have entered following grades for midTerm2: [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
    You have entered following grades for Final Exam: [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]

    here is my code

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Assign9_Roberts
    
    {
    	public static void main(String[] args)
    	{
    		//input scanner
    		Scanner input = new Scanner(System.in);
    
    			//Prompt User for midterm1 grades
    			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");
    
               //Prompt User for midterm2 grades
                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");
    
              //Prompt User for Final Exam grades
                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;
    			
    		   //Display highest score
    			for (int t = 0; t < grades.length; t ++)
    			{
    				if (grades[t] > max)
    				{
    					max = grades[t];
    				}
    				
    				System.out.print("Your highest test score is: " + max);
    				
    			//Display lowest score
    			int min = 0;
    				if(grades[t] < min)
    				{
    					min = grades[t];
    				}
    				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));
    
    			}
    
    			}
    also this is cross posted
    http://www.javaprogrammingforums.com...html#post21185
    Last edited by clemsontigers; 11-18-2010 at 02:46 AM.

  2. #2
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    When posting code can you please use the code formatter. Put
    Java Code:
     before your code and then
    at the end. It makes it easier to debug.
    - Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java Code:
    //Display highest score
    for (int t = 0; t < grades.length; t ++)
    {
       if (grades[t] > max)
       {
          max = grades[t];
       }
                   
       System.out.print("Your highest test score is: " + max);
                   
       //Display lowest score
       int min = 0;
       if(grades[t] < min)
       {
          min = grades[t];
       }
       System.out.print("Your lowest test score is: " + min);
    }

    You won't know about the minimum and maximum scores until after the for loop has finished and checked everybody's score. So move the System.out.print() statements to after the for loop.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    On the formatting, I think Allspark meant: put [CODE] before your code and the [/CODE] at the end.

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

    Default

    Quote Originally Posted by Allspark View Post
    When posting code can you please use the code formatter. Put
    Java Code:
     before your code and then
    at the end. It makes it easier to debug.
    That was written as:

    Put [code] before your code and then [/code] at the end.

    And the previous line was written using a little trick to mislead that stupid forum software. ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    here is my updated code and after i run it and enter a number it gives me this error

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Assign9_Roberts.main(Assign9_Roberts.java:29)


    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Assign9_Roberts
    
    {
    	public static void main(String[] args)
    	{
    		//input scanner
    		Scanner input = new Scanner(System.in);
    
    		  double[] grades = new double[10];
    	      double[] midTerm1 = new double[10];
    	      double[] midTerm2 = new double[10];
    	      double[] finalExam = new double[10];
    	      
    	      
    	        // add midterm1 , midterm2 and final grades and store them in grade array
    	        for (int i=0; i<finalExam.length; i++ ) {
    	            grades[i] = midTerm1[i] + midTerm2[i] + finalExam[i];
    	        }
    	        System.out.println("Please enter a MidTerm1 grade:");
                midTerm1[10] = input.nextDouble();
            
    	        
                 System.out.println("Now");
    
                for(int B=0; B<midTerm2.length;B++){
                }
                System.out.println("Please enter a MidTerm2 grade:");
                midTerm2[10] = input.nextDouble();
                
                System.out.println("Now");
    
                for(int C = 0; C<finalExam.length; C++){
                }
                System.out.println("Please enter a Final Exam grade:");
            	finalExam[10] = input.nextDouble();
                
            	double max = grades[0];
            	double min = grades[0];
    			
            	//Display highest score
            	for (int t = 0; t < grades.length; t ++)
            	{
            	   if (grades[t] > max)
            	   {
            	      max = grades[t];
            	   }
            	               
            	   System.out.print("Your highest test score is: " + max);
            	               
            	   //Display lowest score
               	   if(grades[t] < min)
            	   {
            	      min = grades[t];
            	   }
            	   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));
    
    			}
    
    			}

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
     midTerm1[10] = input.nextDouble();
    midTerm1[10] does not exist, arrays(and computers)begin counting from 0, if you want the tenth element use 9, 0-9 is ten elements.

  8. #8
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    ok i changed that..thanks, but im still getting the same error..here's my updated code

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Assign9_Roberts
    
    {
    	public static void main(String[] args)
    	{
    		//input scanner
    		Scanner input = new Scanner(System.in);
    
    		  double[] grades = new double[9];
    	      double[] midTerm1 = new double[9];
    	      double[] midTerm2 = new double[9];
    	      double[] finalExam = new double[9];
    	      
    	      
    	        // add midterm1 , midterm2 and final grades and store them in grade array
    	        for (int i=0; i<finalExam.length; i++ ) {
    	            grades[i] = midTerm1[i] + midTerm2[i] + finalExam[i];
    	        }
    	        System.out.println("Please enter a MidTerm1 grade:");
                midTerm1[9] = input.nextDouble();
            
    	        
                 System.out.println("Now");
    
                for(int B=0; B<midTerm2.length;B++){
                }
                System.out.println("Please enter a MidTerm2 grade:");
                midTerm2[9] = input.nextDouble();
                
                System.out.println("Now");
    
                for(int C = 0; C<finalExam.length; C++){
                }
                System.out.println("Please enter a Final Exam grade:");
            	finalExam[9] = input.nextDouble();
                
            	double max = grades[0];
            	double min = grades[0];
    			
            	//Display highest score
            	for (int t = 0; t < grades.length; t ++)
            	{
            	   if (grades[t] > max)
            	   {
            	      max = grades[t];
            	   }
            	               
            	   System.out.print("Your highest test score is: " + max);
            	               
            	   //Display lowest score
               	   if(grades[t] < min)
            	   {
            	      min = grades[t];
            	   }
            	   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));
    
    			}
    
    			}

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

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    You just declare your arrays with a lenght of 9, index in array starts with zero meaning when
    you declare
    Java Code:
        double[] midTerm1 = new double[9];
    it means that the maximum index is 8.
    Java Code:
        midTerm1[8] = something;

    Look at this line. You put 9 in midTerm1 index which gives you error because 9th midTerm1 is
    actually midTerm1[8].
    Java Code:
        midTerm1[9] = input.nextDouble();

  11. #11
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    also, trying to submit this assignment within and hour so a quick response is greatly appreciated..thanks

    i updated my code and while im not getting the same error its only allowing me to enter 3 numbers and giving me this out put.

    Please enter a MidTerm1 grade:
    100
    Now
    Please enter a MidTerm2 grade:
    90
    Now
    Please enter a Final Exam grade:
    90
    Your highest test score is: 0.0Your lowest test score is: 0.0Your highest test score is: 0.0Your lowest test score is: 0.0Your highest test score is: 0.0Your lowest test score is: 0.0Your highest test score is: 0.0Your lowest test score is: 0.0Your highest test score is: 0.0Your lowest test score is: 0.0Your highest test score is: 0.0Your lowest test score is: 0.0Your highest test score is: 0.0Your lowest test score is: 0.0Your highest test score is: 0.0Your lowest test score is: 0.0Your highest test score is: 0.0Your lowest test score is: 0.0You have entered following grades for midTerm1: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0]
    You have entered following grades for midTerm2: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 90.0]
    You have entered following grades for Final Exam: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 90.0]




    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Assign9_Roberts
    
    {
    	public static void main(String[] args)
    	{
    		//input scanner
    		Scanner input = new Scanner(System.in);
    
    		  double[] grades = new double[9];
    	      double[] midTerm1 = new double[9];
    	      double[] midTerm2 = new double[9];
    	      double[] finalExam = new double[9];
    	      
    	      
    	        // add midterm1 , midterm2 and final grades and store them in grade array
    	        for (int i=0; i<finalExam.length; i++ ) {
    	            grades[i] = midTerm1[i] + midTerm2[i] + finalExam[i];
    	        }
    	        System.out.println("Please enter a MidTerm1 grade:");
                midTerm1[8] = input.nextDouble();
            
    	        
                 System.out.println("Now");
    
                for(int B=0; B<midTerm2.length;B++){
                }
                System.out.println("Please enter a MidTerm2 grade:");
                midTerm2[8] = input.nextDouble();
                
                System.out.println("Now");
    
                for(int C = 0; C<finalExam.length; C++){
                }
                System.out.println("Please enter a Final Exam grade:");
            	finalExam[8] = input.nextDouble();
                
            	double max = grades[0];
            	double min = grades[0];
    			
            	//Display highest score
            	for (int t = 0; t < grades.length; t ++)
            	{
            	   if (grades[t] > max)
            	   {
            	      max = grades[t];
            	   }
            	               
            	   System.out.print("Your highest test score is: " + max);
            	               
            	   //Display lowest score
               	   if(grades[t] < min)
            	   {
            	      min = grades[t];
            	   }
            	   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));
    
    			}
    
    			}

  12. #12
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    As stated in instruction, prompt the user to enter 10 grades each for midTerm1, midTerm2 and FinalExam. After the user input these that is the time you compute its total.

    But what you did is,
    Java Code:
    [b]You compute the total first[/b]
    	        for (int i=0; i<finalExam.length; i++ ) {
    	            grades[i] = midTerm1[i] + midTerm2[i] + finalExam[i];
    	        }

    Then prompt the user to enter midTerm grades only ONCE.
    Java Code:
    	        System.out.println("Please enter a MidTerm1 grade:");
                midTerm1[8] = input.nextDouble();
            
    	        
                 System.out.println("Now");
    
                for(int B=0; B<midTerm2.length;B++){
                }
                System.out.println("Please enter a MidTerm2 grade:");
                midTerm2[8] = input.nextDouble();
                
                System.out.println("Now");
    
                for(int C = 0; C<finalExam.length; C++){
                }
                System.out.println("Please enter a Final Exam grade:");
            	finalExam[8] = input.nextDouble();

    and pass the value to the last index of you array
    Java Code:
                midTerm1[8] = input.nextDouble();
                midTerm2[8] = input.nextDouble();
                FinalExam[8] = input.nextDouble();
    which leaves others grades zero.

  13. #13
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    prolly sound like an idiot but im not really sure what to do now

  14. #14
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Your first post to ask the user to enter the grades is correct but not the array, ok...

    Also you have logical error in getting the minimum grade. But maybe we can discuss that after you fix the "asking for grades 10x"...

    :)

  15. #15
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    OK, I read your first/orignal post, it is perfectly correct. The only error is
    your loop in getting the lowest and highest grade.

  16. #16
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I also read that pbrockway told you to move the System.out.println(...) after the loop, what he means
    is ONLY the one that prints the highest and lowest grade.

    Here is the code that should looks on what pbrockway means.
    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Assign9_Roberts
    {   public static void main(String[] args)
        {
            //input scanner
            Scanner input = new Scanner(System.in);
    
            //Prompt User for midterm1 grades
            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");
    
            //Prompt User for midterm2 grades
            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");
    
            //Prompt User for Final Exam grades
            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];
            [b]
            int max = 0;
    
            //Display highest score
            for (int t = 0; t < grades.length; t ++)
            {
                if (grades[t] > max)
                {   max = grades[t];
                }
    
                int min = 0;
                //Display lowest score
                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));
            [/b]
        }
    }
    Compare your original post in this thread to the bold fonts in this code.

Similar Threads

  1. Storing/retrieving arrays in a data structure...
    By pbandjay in forum New To Java
    Replies: 0
    Last Post: 11-03-2010, 01:39 AM
  2. Need help storing arrays from a file
    By arson09 in forum New To Java
    Replies: 4
    Last Post: 03-08-2010, 05:11 PM
  3. Java program problem.. Arrays.. Random Numbers
    By Chewart in forum New To Java
    Replies: 16
    Last Post: 11-16-2009, 11:21 PM
  4. Storing and managing huge arrays
    By trust in forum Java Software
    Replies: 2
    Last Post: 08-22-2009, 03:47 PM
  5. Random numbers and arrays
    By caro in forum New To Java
    Replies: 6
    Last Post: 06-10-2009, 02:09 AM

Posting Permissions

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