Reputation: 31
Hey, so i have this hw assignment and i have to write a program that allows the user to input students names and grades and give back the higest lowest and average. The only problem is there is a max 50 students in the class(or so the sheet says) and there are only 10 students names and grades on it. How do i get the array to end after the last student?
static final int MAX = 50;
public static void main(String[] args)
{
int index, average;
int highest = 0, student = 1;
Scanner keyboard = new Scanner(System.in);
String [] students = new String[MAX];
int[] grades = new int[MAX];
System.out.println("Please enter the name of the student and their test grade");
System.out.print("Students: ");
for(index=0; index<MAX; index++)
{
students[index] = keyboard.nextLine();
student = student + index;
if(students[index]=="Done"))
{
student = student - 1;
continue;
}
}
System.out.print("Grades: ");
for(index=0; index<student; index++)
grades[index] = keyboard.nextInt();
averageMan(grades, student);
for(index=0; index<student; index++)
if(grades[index] < averageMan(grades, student))
System.out.println(students[index] + " Your test grade was " +
"below the class average. Step it up.");
for(index=0; index<student; index++)
if(grades[index] > highest)
highest = grades[index];
System.out.println("The highest test grade in the class goes to " +
students[highest] + " with a grade of " + grades[highest]);
}
public static int averageMan(int[] g, int s)
{
int index, sum = 0;
int averages;
for(index=0; index<s; index++)
sum = sum + g[index];
averages = sum / s;
return averages;
}
}
Upvotes: 1
Views: 133
Reputation: 298858
Use Collections, not arrays. Their api is a lot more usable.
Read the Collections Trail to get started.
If you want a drop-in replacement for an array, use a List, if you want uniqueness, use a Set.
Upvotes: 3
Reputation: 11516
Try changing
student = student + index;
if(students[index]=="Done"))
{
student = student - 1;
continue;
}
to:
if(students[index]=="Done"))
{
break;
}
student = student + 1;
Then you will quit the entry loop when a student's name is input as "Done" and the variable student
will contain the number of students that were input.
Upvotes: 1
Reputation: 98
I would use a generic array list http://www.oracle.com/technetwork/articles/javase/generics-136597.html
Upvotes: 0
Reputation: 1538
Your for
loop has three parts, the middle part resolves to a true/false condition. It can be simple, like you have or complex, like this:
for(index=0; index<MAX && !done; index++)
Where done
is a boolean
that is false until you detect a value that indicates you want to stop then becomes true.
Upvotes: 0
Reputation: 48
The easiest solution would be to use a collection class, like java.util.ArrayList instead of an array. If you have to use arrays, maybe you could reallocate the array each time you add a new Student? This page has an example of how to do that - How to resize an array in Java.
Upvotes: 0
Reputation: 3338
Initialize the students
array with empty strings. Also Initialize the grades
array with -1
. In your loop for calculating average/highest/lowest, check for empty string and -1.
Upvotes: 1
Reputation: 7335
You could loop round collecting student details until the user enters a specific token that indicates that they have finished entering details.
EDIT Which looks like what you're trying to do..... but continue
goes to the next item in the loop. You want to break
out don't you?
Upvotes: 1
Reputation: 7662
1) Hey, why are you doing this?
if(students[index]=="Done"))
{
student = student - 1;
continue;
}
2) you can divide code into smaller methods
3) you can use Collections API as someone suggested.
Upvotes: 0