Join 150,199 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,995 people online right now. Registration is fast and FREE... Join Now!
I'm having an awful time with arrays. I'm so confused. Can anyone lend some guidance to get me on track?
CODE
import java.util.Scanner; // Needed for Scanner class
/** This program uses a String array (or ArrayList object) to hold the names of 5 students, an array of 5 characters to hold the student's letter grades, and five arrays of 4 doubles each to hold their test scores.
Program allows user to enter each student's name and 4 test scores.
Program displays student's name, average test score and letter grade. */
public class GradeBook { public static void main(String[] args) { final int STUDENTS = 5;// Number of students final char LETTERS = 5; // Number of grades final int TESTS = 4; // Number of tests
// Create an array to hold students names. String[] names = new String[STUDENTS]; char[] grades = new char[LETTERS]; double[] score1 = new double[TESTS]; double[] score2 = new double[TESTS]; double[] score3 = new double[TESTS]; double[] score4 = new double[TESTS]; double total = 0; double average; for (int index = 0; index < score1.length; index++) total += score1[index]; average = total / score1.length; for (int index = 1; index < score1.length; index++) total += score2[index]; average = total / score1.length; for (int index = 2; index < score1.length; index++) total += score3[index]; average = total / score1.length; for (int index = 3; index < score1.length; index++) total += score4[index]; average = total / score1.length;
int count = 0;
// Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in);
// Get the names of each student. System.out.println("Enter student's name: "); String student = keyboard.nextLine(); System.out.println("Enter student's 1st test score: "); double test1 = Double.parseDouble(keyboard.nextLine()); System.out.println("Enter student's 2nd test score: "); double test2 = Double.parseDouble(keyboard.nextLine()); System.out.println("Enter student's 3rd test score: "); double test3 = Double.parseDouble(keyboard.nextLine()); System.out.println("Enter student's 4th test score: "); double test4 = Double.parseDouble(keyboard.nextLine()); {
public class GradeBook { public static void main(String[] args) { final int STUDENTS = 5;// Number of students final char LETTERS = 5; // Number of grades final int TESTS = 4; // Number of tests
// Create an array to hold students names. String[] names = new String[STUDENTS]; char[] grades = new char[LETTERS]; double[] scores = new double[TESTS];
Scanner input = new Scanner(System.in);
// Get the names of each student System.out.println("Enter student's name: "); String student = input.nextLine();
// Get student scores System.out.println("Insert 4 test scores:"); double[] scores = {input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble()};
// taking the average: double average = 0; for(int i=0; i<scores.length; i++) { average += scores[i]; } average /= scores.length; System.out.println("The average score is: "+ average); } {
Close, I have cleaned up the code to make it a bit nicer looking. You don't need to worry about the names array or the grades array, each of those will have a single value.
Here it is:
CODE
public static void main(String[] args){ char grade; Scanner input = new Scanner(System.in);
// Get the names of each student System.out.println("Enter student's name: "); String student = input.nextLine();
// Get student scores System.out.println("Insert 4 test scores:"); double[] scores ={input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble()};
// taking the average: double average = 0; for (int i = 0; i < scores.length; i++){ average += scores[i]; } average /= scores.length; System.out.println("The average score is: " + average);
if (average >= 90) grade = 'A'; else if (average >= 80) grade = 'B'; else if (average >= 70) grade = 'C'; else if (average >= 60) grade = 'D'; else grade = 'F'; System.out.println(student + " has an " + grade + " in the class."); }
NOTE - This is only the main method (function), not the whole class
Well, I'm able to add five names now, but only one set of scores.
I want to be able to add the (1st) name & (4) test scores, (2nd) name & (4) test scores, etc., and then display the names, grades & averages.
Can anyone help me out with this?
CODE
import java.util.Scanner; // Needed for Scanner class public class GraBook { public static void main(String[] args) {
final int STUDENTS = 5;// Number of students final char LETTERS = 5; // Number of grades final int TESTS = 4; // Number of tests
// Create an array to hold students names. char[] grades = new char[LETTERS]; double[] score1 = new double[TESTS]; double[] score2 = new double[TESTS]; double[] score3 = new double[TESTS]; double[] score4 = new double[TESTS]; char grade;
Scanner input = new Scanner(System.in);
// Get the names of each student System.out.println("Enter student's name: "); String student = input.nextLine(); String[] names ={input.nextLine(), input.nextLine(), input.nextLine(), input.nextLine()};
// Get student scores System.out.println("Insert 4 test scores:"); double[] scores ={input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble()};
// Average 4 test scores: double average = 0; for (int i = 0; i < scores.length; i++) { average += scores[i]; } average /= scores.length;
if (average >= 90) grade = 'A'; else if (average >= 80) grade = 'B'; else if (average >= 70) grade = 'C'; else if (average >= 60) grade = 'D'; else grade = 'F';
System.out.println(student + " has a(n) " + grade + " with an average test score of " + average); } }
I've tried about every loop to enable user to enter 1st Student Name, 4 Test Scores 2nd Student Name, 4 Test Scores ... 5th Student Name, 4 Test Scores, but even without errors, I can only get user to input 1st - 5th Name 1 set of test scores
Does anyone have any suggests? Thanks in advance!
CODE
import java.util.Scanner; // Needed for Scanner class public class GraBook { public static void main(String[] args) {
final int STUDENTS = 5;// Number of students final char LETTERS = 5; // Number of grades final int TESTS = 4; // Number of tests
// Create an array to hold students names. char[] grades = new char[LETTERS]; double[] score1 = new double[TESTS]; double[] score2 = new double[TESTS]; double[] score3 = new double[TESTS]; double[] score4 = new double[TESTS]; char grade;
Scanner input = new Scanner(System.in);
// Get the names of each student for (int i=0; i < names.length; i++) { System.out.println("Enter student's name: " + (i + 1) + ": ");; String[i] = keyboard.nextString();
// Get student scores System.out.println("Insert 4 test scores:"); double[] scores ={input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble()};
// Average 4 test scores: double average = 0; for (int i = 0; i < scores.length; i++) { average += scores[i]; } average /= scores.length;
if (average >= 90) grade = 'A'; else if (average >= 80) grade = 'B'; else if (average >= 70) grade = 'C'; else if (average >= 60) grade = 'D'; else grade = 'F';
System.out.println(student + " has a(n) " + grade + " with an average test score of " + average); } }
Sadly Java doesn't seem to allow you to have multi-dimensional arrays, so we have to make a new class to hold things.
Here is the code I came up with:
Student.java:
CODE
import java.util.Scanner;
public class Student{ public static void main(String[] args){ Scanner input = new Scanner(System.in); Record[] students = {new Record(), new Record(), new Record(), new Record(), new Record()}; for(int x=0; x<5; x++){ if(x>0){ input.nextLine(); } System.out.println("Insert student name:"); students[x].setName(input.nextLine()); System.out.println("Insert 5 scores:"); double[] scores = {input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble()}; students[x].setScores(scores); System.out.println(students[x].getAvg()); }
for(int i=0; i<students.length; i++){ System.out.println("Student entry #"+i+". Name "+students[i].getName()+" Average Test Score "+students[i].getAvg()); } } }
Record.java:
CODE
public class Record{ private String myName; private double[] myScores; public Record(String name, double[] scores){ myName = name; myScores = scores; } public Record(String name){ myName = name; } public Record(double[] scores){ myScores = scores; } public Record(){ myName = ""; } public double getAvg(){ double sum = 0; for(int i=0; i<myScores.length; i++){ sum += myScores[i]; } sum /= myScores.length; return sum; } public String getName(){ return myName; } public void setName(String name){ myName = name; } public void setScores(double[] scores){ myScores = scores; } public void addScore(double score){ myScores[myScores.length] = score; } }
Basicall, the Student class has the main method and creates an array of Records. The records each have an array of test scores, myScores, that can be set.
To declare multi-dimensional arrays in Java, use multiple square brackets [].
[code] double [][] tests = new double [STUDENTS][TESTS]; [\code]
When you prompt for data,save immediately to an array location instead of a variable. Use an outer loop to prompt for the name. After saving a name, use an inner loop to prompt and save the values for test scores into the proper location in your two-dimensional array for storing test scores.
When you finish your outer loop, you will have stored all scores. The next step would be to sum all scores in your two-dimensional array and calculate the average.