user2166241
user2166241

Reputation: 11

Java code editing

Help, I cannot get my program to run successfully. It gives me an error with two inputs for midterm mark and final mark. I cannot get it to run fully with the appropriate end pop up. I've tried to write a program that accepts marks from the user and displays the appropriate letter grade for unknown number of students. My code is as follows.

        import javax.swing.*;
    public class StudentGrades_Hann{
        public static void main (String[] args) {
            String input ;
            input= JOptionPane.showInputDialog(" Enter Name, \n ID Number,\n Homework Mark, \n Assignment Mark, \n Midterm Mark, \n and Final Mark \n All Entrys Must Be Separated By Commas");

            int com;
            com= input.indexOf(",");

            String name ;
            name = input.substring(0,com);

            int com2;
    com2= input.indexOf(",", com+1);

    String idnumber ;
    idnumber = input.substring(com+1, com2);

    int com3;
    com3= input.indexOf(",", com2+1);

    String homeworkmark ; 
    homeworkmark = input.substring(com2+1, com3);

    int com4;
    com4= input.indexOf(",", com3+1);

        String assignmentmark ; 
        assignmentmark = input.substring(com3+1, com4);

        int com5;
        com5= input.indexOf(",", com4+1);

        String midtermmark ; 
        midtermmark = input.substring(com4+1, com5);

        int com6;
        com6= input.indexOf(",", com5+1);

        String finalmark ; 
        finalmark = input.substring(com4+1);

        double as ;
        as = Double.parseDouble (assignmentmark.trim());

        double hw ;
        hw = Double.parseDouble (homeworkmark.trim());

        double mm ;
        mm = Double.parseDouble (midtermmark.trim());



        double fm ;
        fm = Double.parseDouble (finalmark.trim());

        String grade ; 

        double totalmark ; 
        totalmark= 0.1*hw+0.1*as+0.3*mm+0.5*fm ;

        double mnf ;
        mnf= 0.5*fm+0.3*mm ;

        if(totalmark < 50 || mnf < 50) {
            JOptionPane.showMessageDialog(null,name + idnumber + "Fail") ;
        }
        else if (totalmark < 85) {
            JOptionPane.showMessageDialog(null, name + idnumber + "A") ;
        }

        else if (totalmark < 75) {
            JOptionPane.showMessageDialog(null,name + idnumber + "B") ;
        }

        else if (totalmark <65) {
            JOptionPane.showMessageDialog(null, name + idnumber + "C");
        }

        else if (totalmark <=50) {
            JOptionPane.showMessageDialog(null,name + idnumber + "D") ;
        }

        else {
            JOptionPane.showMessageDialog(null, "Invalid Entry") ;
        }

    }

    }   

Upvotes: 0

Views: 173

Answers (2)

Achintya Jha
Achintya Jha

Reputation: 12843

I am getting an exception error "Exception in thread "main" java.lang.NumberFormatException: For input string: "80, 90"

You are trying to parse"80,90" somewhere in your code which is not parsable double . So you are getting NumberFormatException.

So instead of doing so much labour in finding indexOf and substring operation . Just try

String[] splitted = input.split(",");

You are doing somewhere mistake in splitting the given string. I found the mistake.

String finalmark ; 
    finalmark = input.substring(com4+1);

should be

String finalmark ; 
    finalmark = input.substring(com5+1);

Upvotes: 0

Jean Waghetti
Jean Waghetti

Reputation: 4727

You are not updating the index of , for the next value.

For the second value, if the input is name, 1, 1, 1, 1, 1, it will try to parse "1, 1".

Use String.split(","), check if the number of elements in array is what you expect and then parse them, like this pseudo-code:

String[] strings = input.split(",");
if (string.length == 6) { //name and 5 values
    name = strings[0];
    id = strings[1];
    ...
} else {
       //provided values are incorrect
}

`

Upvotes: 1

Related Questions