?
Solved

This question has to do with Arrays in Java

Posted on 2005-04-15
7
Medium Priority
?
334 Views
Last Modified: 2008-03-10
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
Comment
Question by:NikkitaKMiles
  • 3
  • 2
  • 2
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13795943
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
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 13795944
follwoing link shows how to convert a string to an int

http://www.objects.com.au/java/conversions.do
0
 
LVL 92

Expert Comment

by:objects
ID: 13795948
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
limaideal earned 1800 total points
ID: 13798631
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
 

Author Comment

by:NikkitaKMiles
ID: 13799563
I like what limaideal did.  The points are awarded here.
0
 

Author Comment

by:NikkitaKMiles
ID: 13799599
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
 
LVL 2

Expert Comment

by:limaideal
ID: 13799903
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question