-5
public class StudentGrades {

    String studentID;
    Integer numericGrade;

    Scanner input = new Scanner(System.in);

public void loadStudentGrades(){
    do{
        System.out.println("Please enter a Student ID, or enter 'end' to exit: ");
        studentID = input.next();
        System.out.println("Please enter numeric grade for the ID above: ");
        numericGrade = input.nextInt();
        map.put(studentID, numericGrade);
        }
    while (studentID !String.equals("end")); //this is throwing an error. How is it possible to get this to work?
    }
}

I'm working on this class and am finding it difficult to get the while part of my do-while loop to work the way I was expecting it to. I want to say while studentID is not equal to "end" to go through the loop.

  • 4
    What do you think `studentID !String.equals("end")` should do and why? – Sotirios Delimanolis Dec 06 '14 at 22:18
  • http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Harshul Pandav Dec 06 '14 at 22:20
  • I think it should compare the variable studentID to "end" and if studentID does equal "end", then end the loop. I am new to Java, so as to the why I think it should do that? Well, I've not covered how to compare a String in a boolean format yet, and from what I've seen online, I thought what I had should work as I know .equals() is used to compare Strings rather than != for ints. The examples given below are also not working however, the loop just continues even when I enter "end" for studentID. –  Dec 06 '14 at 22:34
  • actually, if I put while(!studentID.equals("end")); This does sort of work, but after I enter "end" in my tester, I am still prompted to enter a numeric grade, and then it ends the loop. And if I print out the map, it has the string "end" and the random int I give after typing "end" in the map which is not wanted. –  Dec 06 '14 at 22:55

2 Answers2

2

Your while condition should be changed as follows:

while (!studentID.equals("end"))

The way you are currently calling the equals method is incorrect, as the method has no idea that you are trying to compare studentID to "end"; all you are giving it is "end".

cschieb
  • 236
  • 1
  • 5
1

you should write

while(!studentID.contentEquals("end"));
SummerCode
  • 1,403
  • 1
  • 14
  • 28