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

}

Please post the full text of the error messages.

Post the program's current out put and add comments to show what is wrong with it and show what you want it to look like,

Edited 4 Years Ago by NormR1

When i process one students answers into my program there is no exeption error, but it displays the name and letter grade more then once plz help me display it once:

Nicky 10A AboveAverage
Nicky 10A AboveAverage
Nicky 10A AboveAverage
Nicky 10A AboveAverage
Nicky 10A AboveAverage
Nicky 10A AboveAverage
Nicky 10A AboveAverage
Nicky 10A AboveAverage
Nicky 10A AboveAverage
Nicky 10A AboveAverage
ClassAve: 10
BUILD SUCCESSFUL (total time: 26 seconds)

But if i process more then one students name and answers it throws an execption error and does not process any students name or grading:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 10
at java.lang.String.charAt(String.java:695)
at project.DoingATest.main(DoingATest.java:60)
Java Result: 1
BUILD SUCCESSFUL (total time: 38 seconds)

index = studentNames.indexOf(" ");

Will return -1 if there is no match. Do a check on the value of index while trying to access data using the index value. It is surprisingly helpful as many programmers will overlook that variable assuming it to be true most of the time.

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

this is causing your issue, your always getting the first set of answers because studentAnswers never changes so indexOf(",") is returning the same index and studentsAnswers.subString(0,index) always gets you the first set of answers. also why are you multiplying by 10 in the for loop?

also, there are many more print statements than the ones you gave the example run for, what do they all print when you run it? it's useful to have as much information as possible to help.

Your second loop does the same thing for the names.

Edited 4 Years Ago by sciwizeh: extra information

This article has been dead for over six months. Start a new discussion instead.