Hi, this is my basic program for an assignment but I can't figure out why I am not getting the desired output for the grade printed and why there is a repetition of the Enter Student Name at the end before the program loops again. I have also included a screenshot of the results as well as my code.

import java.util.Scanner;
public class EvalMarks1 {
    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        char grade = 'F';
        double cwMark = 0, examMark = 0;
        int i;
        String inputName;
        String endProgram = "NONE";
        String [] student = new String [10];
        System.out.println ("Please enter name of 10 students:");

        for (i = 0; i < 10; i++)
        {
            System.out.print ("Student " + (i+1) + ": ");
            student [i] = input.nextLine();
        }         
        print (student);

        do {
            System.out.print ("Please enter student name: ");
            inputName = input.nextLine ();
            if (inputName.matches(endProgram))
                break;
            else {
                for (i= 0; i < 10; i++){
                    if (inputName.matches (student [i])){
                        System.out.print ("Enter " + student [i] + "'s course work mark: ");
                        cwMark = input.nextDouble ();
                        while (cwMark < 0 || cwMark > 100){
                            System.out.print ("I am sorry; This is not a valid range of course work mark. Please re-enter mark: ");
                            cwMark = input.nextDouble ();

                        }
                        System.out.print ("Enter " + student [i] + "'s exam mark: ");
                        examMark = input.nextDouble ();
                        while (examMark <0 || examMark > 100){
                            System.out.print ("I am sorry; This is not a valid range of exam mark. Please re-enter mark: ");
                            examMark = input.nextDouble ();
                        }
                        getGrade (examMark, cwMark);
                        System.out.print ("Grade: " + grade + "\n");
                    }
                }
            }

        } while (true);


}


     public static void print (String [] student)

    {
        int i ;
        System.out.println ("List of students you entered is as follows: ");
        for (i = 0; i < 10; i++)
        {
            System.out.print ("     " + (i+1) + ".  "  + student [i] + "\n");
        }
    }

     public static char getGrade (double examMark, double cwMark)

     {
       double averMark = (examMark + cwMark) / 2;
       char grade;
       if (averMark >= 0 && averMark < 50){
           grade = 'F';
       }
       else if (averMark >= 50 && averMark < 60){
           grade = 'D';
       }
       else if (averMark >= 60 && averMark < 70){
           grade = 'C';
       }
       else if (averMark >= 70 && averMark < 80){
           grade = 'B';
       }
       else{
         grade = 'A';
       }

       return grade;
     }
}

Click Here

Ok, I fixed the mistake with the grade, but I still don't understand why the programs repeats the enter student name thing again.

while (true);

when do you think this will reach 'false'?

According to the assignment, it's suppose to loop until the user enters NONE

Ok, I fixed the double enter name statement. Just one more thing left to do as part of the assignment that I can't seem to figure out, if the name entered by the user does not match the array, it has to prompt the user to re-enter the name and has to do this in loop until a name is found in the array.

It won't ever evaluate to false, but the break statement would handle this. The matches() method actually uses regular expressions, which is overkill in this situation. Did you use all caps, because the regex engine in case-sensitive by default? In either case, I would recommend using inputName.equals() or inputName.equalsIgnoreCase() instead (on both line 24 and 28).

Ah....ok, yeah I'll try that. I also need help if the user input does not match any of the names in the array. It needs to go in a loop to prompt the user for a name until one in the array matches or the user enters NONE. Thank you

Could you post your current code? From the above code, it looks like it should already handle reasking for a name when it doesn't match anything.

import java.util.Scanner;
public class EvalMarks1 {
    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        char grade;
        double cwMark = 0, examMark = 0;
        int i;
        String inputName;
        String endProgram = "NONE";
        String [] student = new String [10];
        System.out.println ("Please enter name of 10 students:");

        for (i = 0; i < 10; i++)
        {
            System.out.print ("Student " + (i+1) + ": ");
            student [i] = input.nextLine();
        }         
        print (student);

        do {
            System.out.print ("Please enter student name: ");
            inputName = input.next();
            if (inputName.equalsIgnoreCase(endProgram))
                break;
            else {
                for (i= 0; i < 10; i++){
                    if (inputName.equalsIgnoreCase(student [i])){
                        System.out.print ("Please enter " + student [i] + "'s course work mark: ");
                        cwMark = input.nextDouble ();
                        while (cwMark < 0 || cwMark > 100){
                            System.out.print ("I am sorry; This is not a valid range of course work mark. Please re-enter mark: ");
                            cwMark = input.nextDouble ();

                        }
                        System.out.print ("Please enter " + student [i] + "'s exam mark: ");
                        examMark = input.nextDouble ();
                        while (examMark <0 || examMark > 100){
                            System.out.print ("I am sorry; This is not a valid range of exam mark. Please re-enter mark: ");
                            examMark = input.nextDouble ();
                        }
                        System.out.println ("Computing results for " + student [i] + " ... ");
                        getGrade (examMark, cwMark);

                    }
                }
            }

        } while (true);


}


     public static void print (String [] student)

    {
        int i ;
        System.out.println ("List of students you entered is as follows: ");
        for (i = 0; i < 10; i++)
        {
            System.out.print ("     " + (i+1) + ".  "  + student [i] + "\n");
        }
    }

     public static char getGrade (double examMark, double cwMark)

     {
       double averMark = (examMark + cwMark) / 2;
       char grade;
       if (averMark >= 0 && averMark < 50){
           grade = 'F';
       }
       else if (averMark >= 50 && averMark < 60){
           grade = 'D';
       }
       else if (averMark >= 60 && averMark < 70){
           grade = 'C';
       }
       else if (averMark >= 70 && averMark < 80){
           grade = 'B';
       }
       else{
         grade = 'A';
       }
       System.out.print ("Average Mark: " + averMark+ "%\n");
       System.out.print ("Grade: " + grade + "\n");    
       return grade;
     }
}

This is it so far

what I need for it to do now is for the program to go in a loop when the user inputs a name that is not in the array by displaying "I'm sorry; Name is not found, please enter student name again" and assign the new input to inputName. I have tried doing that by using else that co-relates to the if in line 24 but it does not work as intended. Anyone has any input as to what I am doing wrong?

Inside the else case, before the for-loop, initialize a boolean variable to false. Then inside the for-loop, when a match is found, set it to true. Outside of the for-loop, you can check that variable and if its false, show your message.

Alright, thank you. That worked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.