• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

This question has to do with Arrays in Java

I am creating a program that will prompt the user for 6 student's names and scores using input dialog boxes.  This information is to be stored into two parallel arrays.  The program has to calculate the average score of the class, determine the letter grade for each score, and the letter grade has to be stored into another parallel array.

The program should display each student's name, score, letter grade and the class average.

I have the first two parrelle arrays completed by now I am lost.

Help!

This is what I have so far:

import java.util.Scanner;
import javax.swing.*;

public class studentGrades2
{      //      start studentGrades
      //private String studentName[ ] = new String[6];
      //private int studentScores[ ] = new int[6];;
//      private int studentGrade[ ];

      public static void main( String args[ ] )
      {      //      start main( )
      //  Final Length of Array
      final int STUDARRAY_LENGTH = 6;
      final int STUDSCOREARRAY_LENGTH = 6;
      final int STUDGRADEARRAY_LENGTH = 6;

      //      array type
       String studentName[ ] = new String[STUDARRAY_LENGTH];
       String studentScores[ ] = new String[STUDSCOREARRAY_LENGTH];
       //  Produce 6 prompts for user to input 6 Student Names
       for ( int count = 0; count < studentName.length; count++)
             studentName[count] = JOptionPane.showInputDialog( "Please enter the student's name: " );
                  {
                  for (int j = 0; j < studentScores.length; j++)
                        studentScores[j] = JOptionPane.showInputDialog( "Please enter the student's Score: " );
                  }
             //      output Message

            System.out.printf("The student's names and grades are: ");

            //column headings
            System.out.printf( "\n%s%20s%20s\n" , "Student Name" , "Student Score ", "Student Grade" );

            //output of array of names
            //      traverse the array
            for (int count = 0; count < studentName.length; count++)
            
            //output of array of names
            System.out.printf("\n "+ studentName[count] + " \t " + studentScores[count]);
            //output of array Scores      
            
            //      output of Scores
            
                         
      }      //      end main( )

}      //      end studentGrades
0
NikkitaKMiles
Asked:
NikkitaKMiles
  • 3
  • 2
  • 2
2 Solutions
 
objectsCommented:
To determine the average you need to calculate the sum of all scores.
This can be done in your loop by first parsing the string to get the score as an int and add that to the toital (you'll need a new variable for the total).
0
 
objectsCommented:
follwoing link shows how to convert a string to an int

http://www.objects.com.au/java/conversions.do
0
 
objectsCommented:
to determine the grade you could use an if/then/else statement

String grade = null;
if (score>90)
{
   grade = "A";
{
else if (score>80)
{
   grade = "B";
}
etc.


Then store the grades in another array
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
limaidealCommented:
import javax.swing.*;

public class StudentGrades
{
    public static void main(String args[])
    {
        //  Final Length of Array
        final int STUDARRAY_LENGTH = 6;
        final int STUDSCOREARRAY_LENGTH = 6;
        final char CHARSCORES[] =
            {
            'E', 'D', 'C', 'B', 'A', 'A'};
        //     array type
        String studentName[] = new String[STUDARRAY_LENGTH];
        int studentScores[] = new int[STUDSCOREARRAY_LENGTH];
        int total = 0;
        //  Produce 6 prompts for user to input 6 Student Names
        for (int count = 0; count < studentName.length; count++)
        {
            studentName[count] = JOptionPane.showInputDialog("Please enter the student's name: "
                + (count + 1));
        }
        for (int j = 0; j < studentScores.length; j++)
        {
            while (true)
            {
                try
                {
                    int score = Integer.parseInt(JOptionPane.showInputDialog(
                        "Please enter the student's Score :" + (j + 1)));
                    System.out.println("score:" + score);
                    if (score < 0 || score > 100)
                    {
                        continue;
                    }
                    total += score;

                    studentScores[j] = score;
                    break;

                }
                catch (NumberFormatException ex)
                {
                    continue;
                }
            }
        }

        System.out.println("The student's names and grades are: ");

        //column headings
        System.out.println("Name\tScore\tGrade");

        for (int count = 0; count < studentName.length; count++)
        {
            System.out.println("\n " + studentName[count] + " \t " + studentScores[count]
                               + "\t" + CHARSCORES[studentScores[count] / 20]);
        }

        System.out.println("Average score:" + total / 6);
    } //     end main( )

} //     end studentGrades
0
 
NikkitaKMilesAuthor Commented:
I like what limaideal did.  The points are awarded here.
0
 
NikkitaKMilesAuthor Commented:
limaideal

The code does not give the grading score expected for the US.

100-90 = A
89 - 80 = B
79 - 70 = c
69 - 60 = D
>59      = F

Would you recheck your solution?
0
 
limaidealCommented:
1. delete:
 final char CHARSCORES[] =
            {
            'E', 'D', 'C', 'B', 'A', 'A'};
                  
2. change

System.out.println("\n " + studentName[count] + " \t " + studentScores[count]
                               + "\t" + CHARSCORES[studentScores[count] / 20]);
                                            
to:

char grade;
int gradeNum = studentScores[count];
if ( gradeNum >= 90 )
      grade = 'A";
else if ( gradeNum >= 80 )
      grade = 'B';
else if ( gradeNum >= 70 )
      grade = 'C';
else if ( gradeNum >= 60 )
      grade = 'D';
else
      grade = 'F';
            System.out.println("\n " + studentName[count] + " \t " + studentScores[count]
                               + "\t" + grade);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now