r/programminghomework • • Nov 04 '17

[Java] Introduction to Arrays

Hello All , We recently started working on arrays and I find the possibilities interesting but apparently cant even implement a simple array yet. Basically we had to edit an old assignment and use arrays to store test scores and eventually sort them. At this point I am only trying to figure out how to store (and secondly to add although my code is not even getting to that point) them.

This code worked just fine before I tried to add an array and works without it so I believe that my issue comes down to this loop being incorrectly implemented.

for (i = 1 ; i <=test.length; i++){
	System.out.println("Please enter test score number " 
+ i);
	test[i] = stdIn.nextInt();
	for(i = 0; i < test.length; i++){
	    totalTestScore += test[i];
	}

Basically when my code gets to this point I get this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at TestScores.main(TestScores.java:42)

The loop does execute but when it satisfies the condition of i<test.length the program crashes. I'm sure its something obvious but being new to arrays and not seeing any compilation errors I'm at a little bit of a loss. I'm posting the rest of my code below in case context helps but there are some parts which will make no sense since I am in the middle of converting it from normal variable to arrays. Also please disregard this line for now:

totalTestScore = IntStream.of(test[i -1]).sum();  

That's just something I'm trying out and it doesn't compile or work. Sorry for all the confusion , I'm just pretty lost at this point.

Any insight or a point in the right direction would be very much appreciated. Thanks

import java.util.Scanner;
import java.util.stream.*;

public class TestScores
{

public static void main (String[] args)
{

Scanner stdIn = new Scanner (System.in);
String firstName ="" ;
String lastName = "";

int  totalTestScore = 0, numberOfTests = 0 ;
int testAverage = 0;
int i;
char letterGrade = 'A' ;
String tryAgain = "";
Boolean stop = false;

while (!stop) {
System.out.print("How Many Tests are you entering?");
numberOfTests = stdIn.nextInt();
if (numberOfTests <= 0) {
	stop = true;
}
System.out.print("What is your first name");
firstName = stdIn.next();
System.out.println("What is your Last name?");
lastName = stdIn.next();
totalTestScore = 0;
int[] test = new int[numberOfTests];
for (i = 1 ; i <=test.length; i++){
	System.out.println("Please enter test score number " + i);
	test[i] = stdIn.nextInt();
	for(i = 0; i < test.length; i++){
	    totalTestScore += test[i];
	}
	
	
}
 totalTestScore = IntStream.of(test[i -1]).sum();	

}
testAverage = (totalTestScore)/numberOfTests;
if (testAverage >= 90) {
	letterGrade = 'A';

}else if (testAverage >= 80) {
    letterGrade = 'B';
    
}else if (testAverage >= 70){
	letterGrade = 'C';
	
}else if (testAverage >= 60) {
	letterGrade = 'D';
	
}else if (testAverage <= 59){
	letterGrade = 'F';
	
}

System.out.println("The average of " + firstName.charAt(0)+ 
"." + lastName + "'s test scores is " + testAverage + ". Your current grade is " + letterGrade);
System.out.println("Would you like to continue? (yes or no)");
    tryAgain = stdIn.next();

    if(tryAgain.equalsIgnoreCase("no")) 
    {
        stop = true;
    }
}
}
1 Upvotes

5 comments sorted by