hi there i cant seem to figure out how to print each name and letter grade of each person once and my program displays a indexStringoutofBound execption please help solve my problem heres my codes.
ackage project;
import javax.swing.JOptionPane;
public class DoingATest {
public static void main(String[] args)throws StringIndexOutOfBoundsException
{
String studentsAnswer;
String letterMemo;
String numberOfStudentsInClass;
String studentName;
StringBuilder studentNames = new StringBuilder();
StringBuilder studentsAnswers = new StringBuilder();
int studentsProcessed;
letterMemo = JOptionPane.showInputDialog(null, "Please enter the "
+ "memo answers to the 10 multiple choice questions no spaces"
+ " and all in Capital letters e.g. ABCDEABCDE:", "Answers to "
+ "memo", JOptionPane.QUESTION_MESSAGE);
numberOfStudentsInClass = JOptionPane.showInputDialog(null,
"Please enter the number of students in the class:",
"Total student's in class", JOptionPane.QUESTION_MESSAGE);
studentsProcessed = Integer.parseInt(numberOfStudentsInClass);
for (int x = 0; x < studentsProcessed; x++) {//CHANGED FOR STATEMENT
studentName = JOptionPane.showInputDialog(null, "Please enter"
+ " the Student's name:", "Student name",
JOptionPane.QUESTION_MESSAGE);
studentsAnswer = JOptionPane.showInputDialog(null, "Please enter the "
+ studentName + "'s answers to the 10 multiple choice questions no spaces"
+ " and all in Capital letters e.g. ABCDEABCDE:", "Student's Answers",
JOptionPane.QUESTION_MESSAGE);
studentName += " ";
studentNames.append(studentName);
studentsAnswer += ",";
studentsAnswers.append(studentsAnswer);
}
int count = 0;
int index;
String positionStudentAnswers;
String postionofmemo;
int compared;
int total = 0;
int marks = 0;
for (int x = 0; x < studentsProcessed*10; x++) {
index = studentsAnswers.indexOf(",");
String letters = studentsAnswers.substring(0, index);
System.out.println("Letters: "+letters);
positionStudentAnswers = " " + letters.charAt(x);
letters.replaceAll(letters, "");
System.out.println("Student answers: "+positionStudentAnswers);
postionofmemo = " " + letterMemo.charAt(x);
System.out.println("MEMO ANSWER: "+postionofmemo);
if (postionofmemo.compareTo(positionStudentAnswers) == 0)
{
marks ++;
}
System.out.println("Marks for: "+letters+" "+marks);
}
total += marks;
int classAverage = total/studentsProcessed;
for (int x = 0; x < studentsProcessed*10; x++)
{
index = studentNames.indexOf(" ");
String names = studentNames.substring(0, index);
if(marks == 10||marks == 9 && marks > classAverage)
{
System.out.println(names+" "+ marks+"A AboveAverage");
}
else if(marks == 8 && marks > classAverage)
{
System.out.println(names+" "+ marks+"B AboveAverage");
}
else if(marks == 7 && marks > classAverage)
{
System.out.println(names+" "+ marks+"C AboveAverage");
}
else if(marks == 6 && marks > classAverage)
{
System.out.println(names+" "+ marks+"D AboveAverage");
}
else if(marks == 6 && marks < classAverage)
{
System.out.println(names+" "+ marks+"D BelowAverage");
}
else if(marks == 5 && marks < classAverage)
{
System.out.println(names+" "+ marks+"E BelowAverage");
}
else
{
System.out.println(names+" "+ marks+"F BelowAverage");
}
names.replaceAll(names, "");
}
System.out.println("ClassAve: " + total );
}