1

I'm having a few problems with my code, this is the over all goal of the program.

One of your professors hears of your emerging programming expertise and asks you to write a SINGLE program that can be used to help them with their grading. The professor gives three 50-point exams and a single 100-point final exam. Your program will prompt the user for the student’s name, entered as Firstname Lastname (i.e. Bob Smith), the student’s 3-exam scores and 1-final exam score (all whole numbers). Class size varies from semester to semester, but 100 is the limit (declare as a constant).

Read in information for ALL students before doing any calculations or displaying any output. Verify that the 3 exam scores are between 0-50 points and that the final is between 0-100 as they are entered. Declared minimums and maximums as constant so that they can easily be updated, as needed. If invalid, display an error message and allow the user to re-enter that invalid score. Once all student info is read in, display each student’s name in the format LASTNAME, FIRSTNAME (all uppercase), the student’s exam percentage (total of all exams plus final / total possible) to 1 decimal and the student’s final grade.

This is what I have:

import java.util.*;
import java.text.*;


public class Proj4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String input;
String again = "y";
final int MAX_STUDENTS = 100;
final int MIN_EXAM = 0;
final int MAX_EXAM = 50;
final int MIN_FINAL = 0;
final int MAX_FINAL = 100;

String[] names = new String[MAX_STUDENTS];
int [] exams = new int[MAX_STUDENTS * 4];
int student = 1;

do
{
        System.out.print("PLease enter the name of student " + student + ": " );
        for (int k = 0; k < 1; k++) {
            names[k] = s.nextLine().toUpperCase();
        }
        for ( int i = 0; i < 4; i++){
            if(i==3){
                System.out.print("Please enter score for Final Exam: ");
                exams[i] = s.nextInt();
            }
            else{
            System.out.print("Please enter score for Exam " + (i+1) + ": ");
            exams[i] = s.nextInt(); 

                if((exams[0]<MIN_EXAM||exams[0]>MAX_EXAM)||(exams[1]<MIN_EXAM||exams[1]>MAX_EXAM)||(exams[2]<MIN_EXAM||exams[2]>MAX_EXAM)){
                    System.out.println("Invalid enter 0-50 only...");
                    System.out.print("Please re-enter score: ");
                    exams[i] = s.nextInt();
                }
                else if(exams[3]<MIN_FINAL||exams[3]>MAX_FINAL){
                    System.out.println("Invalid enter 0-100 only...");
                    System.out.print("Please re-enter score: ");
                    exams[i] = s.nextInt();
                }
            }
        }
        System.out.print("do you wish to enter another? (y or n) ");
        again = s.next();
        if(again!="y")
            student++;
}while (again.equalsIgnoreCase ("y"));

System.out.println("***Class Results***");
System.out.println(names[1] + "," + names[0] + "   " + "Exam Percentage: "+ ((exams[0]+exams[1]+exams[2]+exams[3])/(MAX_EXAM*3+MAX_FINAL)));

}
}

The problems i have have are:

  • figuring out how to assign the user entered test scores beyond just the first student, i believe i have it set up correct for just one, but it runs into a problem when i would move on to the second student.
  • For some reason that I cannot figure out the line

    System.out.print("do you wish to enter another? (y or n) ");
    again = s.next();
    

    doesn't allow me to enter anything, not y not n not anything, so my program effectively ends there, it doesn't make sense to me because I've done it exactly like that before and it has worked.

  • other than that, if there are any other problems that you can see with my code pointing them out would be extremely helpful.

Thank you

EDIT-

new problem i am having, after changing to

if(!again.equalsIgnoreCase("y"))
            student++;
}while (again.equalsIgnoreCase ("y"));

it lets me type things in now, but after i type in y it prints the next line as

Please enter the name of student 1: Please enter score for Exam 1:

I don't know why or what i need to change to fix it, any suggestions?

Joseph Mindrup
  • 65
  • 1
  • 3
  • 10

3 Answers3

1
`if(again!="y")` is the culprit here 

You should use equals() method to check string equality.

if(!again.equals("y"))

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • 1
    Strange error since OP uses `again.equalsIgnoreCase("y")` below that line of code – Luiggi Mendoza Mar 01 '13 at 16:53
  • thank you, that fixed the problem with it not letting me type in y but for some reason it didn't count up the students variable like it was supposed to, and another error ( not sure why ) but it also printed the next line with it looking like `Please enter the name of student 1: Please enter score for Exam 1:` – Joseph Mindrup Mar 01 '13 at 16:56
1

If you compare Strings in Java using the == or != operators then you are not actually comparing the values. Instead you are testing if the two Strings are the same Object.

This post explains String comparison well.

To do what you want, change if (again != "y") to if(! (again.equalsIgnoreCase("y")) )

EDIT

I believe your new problem stems from the first for loop you do inside your do loop. Each time you type "y" at the end of your do/while you are going to execute the entire

for (int k = 0; k < 1; k++) {

loop again. This is why after you type "y" you are seeing Please enter the name of student 1: Please enter score for Exam 1:

A "solution" to your new issue would be to make it so the outer for enclosed the inner one, looping through 4 exams for each student usually referred to as a "double for" or "nested for loop".

That said you would then be presented with the issue of having all the exams for ALL students in a single array.

I think now is the time to sit down and put some serious thought into the design of your program. It would be much easier for you if you used a Student object to represent a student and hold their exam scores, IMO. Then you could create an array of Students as opposed to two different arrays you have now.

Here are some "starter" steps (Not necessarily a complete list):

  1. Make a Student class that has variables for the student's first & last name, as well as an array to hold that Students exam scores
  2. In your main class create an ArrayList to hold all of the new Student objects you will be creating.
  3. Do your do/while loop. At the beginning of the loop create a new Student object. Then ask for the Students name and exam scores (note that if you KNOW there will only be the 4 exam scores you don't have to do any extra logic there. You can simply ask for the 4 exam scores using a for loop, or if you want all at one time. if there are a variable number of scores you will have to do some sort of check)
  4. Add the new Student you have created to the ArrayList of Students.
  5. Once the person selects "n", loop through the ArrayList and print the information for each Student!
Community
  • 1
  • 1
knoight
  • 479
  • 8
  • 18
  • both this line and the one @PremGenError mentioned do the same thing after i type in y where is gives me `Please enter the name of student 1: Please enter score for Exam 1:` – Joseph Mindrup Mar 01 '13 at 16:59
  • you are incrementing student after testing for `again` being NOT equal to "y". So basically if they type "y" student will NOT get incremented. you probably want `if (again.equalsIgnoreCase("y")){ student++; }` – knoight Mar 01 '13 at 17:02
  • 1
    Actually, as the poster found out, you CAN, but the results are not what you expect. == or != ask if it's the same Object, .equals() will compare the content. On some JVM's, the poster's method will work because the JVM decides to re-use the same "y" string rather than create a new one. (I just ran this program in intellij and it worked fine for example) This inconsistent behavior is why you should (almost) NEVER use == or != to compare strings. – Gus Mar 01 '13 at 17:03
  • so you're saying what i had should have worked? hmm i wonder what was wrong with what i was doing, I am using eclipse to type, compile, and run the code. – Joseph Mindrup Mar 01 '13 at 17:05
  • Yes, you are correct, you CAN compare them that way, however most people's intention is not to compare the Objects but the value as you say. I will edit as I knew better but assumed the OP wanted to compare values so "can't" is "true" in that case. Thanks. – knoight Mar 01 '13 at 17:10
  • oh okay thanks, i am going to use the `if (again.equalsIgnoreCase("y")){ student++; }` line, it seems to work. but the program is still printing out the Please enter score for Exam 1: line with the second student line, any reason you guys can see why this would happen? – Joseph Mindrup Mar 01 '13 at 17:14
  • the do loop should be closing right at the while? it seems to be that way too because i counted up the brackets and they seem all correct, I'm not entirely sure how you want me to indent it sorry. just move the do over along with everything else? – Joseph Mindrup Mar 01 '13 at 18:37
  • Whoops...appears maybe that you had it indented fine, the trouble was with my browser (that I'm forced to use...) `do` loop looks ok...i'll look for other issues. – knoight Mar 01 '13 at 18:53
  • I've updated my answer to address your new issue....IMHO you should re-think your design...I think you will continue to have issues with this code and you'd be better served to try to implement a more "OO" design. – knoight Mar 01 '13 at 19:38
  • Thank you for looking over my code and giving me steps on how to accomplish this, but i do not think I am allowed to use array lists, is there any way to do this without using them. – Joseph Mindrup Mar 01 '13 at 21:19
  • additionally i can only use one file, so i don't think i can do two different classes – Joseph Mindrup Mar 01 '13 at 22:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25397/discussion-between-knoight-and-joseph-mindrup) – knoight Mar 02 '13 at 01:03
0

Your for (int k = 0; k < 1; k++) loop will execute only once (for one student) because you have it set up to execute only while k < 1, which will happen only once. As soon, as you increase k to 1, the loop will stop. I would change it to for (int k = 0; k < MAX_STUDENTS; k++) to make sure it will loop through until you reach the max number of students allowed.

geeky_chik
  • 28
  • 1
  • 5
  • when i do this, the rest of the code does not print out or run, k is initialized and runs just for that for loop i don't think it gets remembered after each re run of the do-while loop – Joseph Mindrup Mar 01 '13 at 17:08