We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,605 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Need Help

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

3
Contributors
11
Replies
1 Day
Discussion Span
8 Months Ago
Last Updated
12
Views
Question
Answered
rvijaya
Newbie Poster
13 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

rvijaya
Newbie Poster
13 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

while (true);

when do you think this will reach 'false'?

stultuske
Industrious Poster
4,366 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23

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

rvijaya
Newbie Poster
13 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

rvijaya
Newbie Poster
13 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

nmaillet
Posting Pro
537 posts since Aug 2008
Reputation Points: 111
Solved Threads: 103
Skill Endorsements: 3

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

rvijaya
Newbie Poster
13 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

nmaillet
Posting Pro
537 posts since Aug 2008
Reputation Points: 111
Solved Threads: 103
Skill Endorsements: 3
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

rvijaya
Newbie Poster
13 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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?

rvijaya
Newbie Poster
13 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

nmaillet
Posting Pro
537 posts since Aug 2008
Reputation Points: 111
Solved Threads: 103
Skill Endorsements: 3

Alright, thank you. That worked

rvijaya
Newbie Poster
13 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 8 Months Ago by nmaillet and stultuske

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0987 seconds using 2.82MB