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

Thread: help w/ storing/scanning numbers in arrays

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

    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...thanks guys
    help w/ storing/scanning numbers in arrays - Java Forums
    Last edited by robertsbd; November 17th, 2010 at 02:15 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help w/ storing/scanning numbers in arrays

    /Display highest score

       for (int t = 0; t < grades.length; t ++)
                {
                    if (grades[t] > max)
                    {
                        max = grades[t];
                    }

    Well, since you put nothing in your grades array, all the values are null.

  3. #3
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ storing/scanning numbers in arrays

    ok here is my updated code...when i run it, it prompts me to enter a grade and when i do i receive this error:

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



    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));
     
    			}
     
    			}
    Last edited by robertsbd; November 17th, 2010 at 08:54 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help w/ storing/scanning numbers in arrays

    Arrays are indexed at zero, so if you create an array of size n, the maximum array position you can access is n-1. So in your code, yourarray[10] will be out of bounds, yourarray[9] will be the last index in the array

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ storing/scanning numbers in arrays

    ok i changed that..thanks, but im still getting the same error..here's my updated 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));
     
    			}
     
    			}

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help w/ storing/scanning numbers in arrays

    You also reduced the size of the array. Remember, the array indexes start at zero, so the last index is n-1 where n is the size of the array. So setting the size to 9, array[9] will be out of bounds and array[8] the last index. Setting to 10, array[10] will be out of bounds but array[9] the last index.

  7. #7
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ storing/scanning numbers in arrays

    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]

     
    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));
     
    			}
     
    			}

Similar Threads

  1. Storing data
    By Joyce in forum Collections and Generics
    Replies: 1
    Last Post: September 20th, 2010, 09:16 AM
  2. Conversions of Numbers in Arrays
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 1st, 2010, 07:59 PM
  3. Scanning Document Issue
    By redvenice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2010, 08:18 AM
  4. Reading .txt and storing into an array
    By vluong in forum Collections and Generics
    Replies: 1
    Last Post: January 4th, 2010, 02:07 PM
  5. Storing in different types of variables
    By giorgos in forum Collections and Generics
    Replies: 0
    Last Post: November 22nd, 2009, 02:02 PM