0

Trying to figure out how to adjust my code to match the output in the picture. When asked for the student's name the second time in the output when I enter a string I receive an error message. I am trying to get the while-loop to continue asking me for the student's name and their score until I type in "alldone" for the student's name for the program to display the student's name who has the highest score.

Instructions & Expected output picture: click here

import java.util.Scanner;

public class practice {

public static void main(String[] args) {

//Scanner Object
Scanner input = new Scanner(System.in);

//Variables
String name = "Nobody";
String highName = "";
int highScore = 0;

//While-Loop
while (!name.equals("alldone")) {

//User Input Required
System.out.println("Please enter student's name, or \"alldone\" if finished >");
name = input.nextLine();

System.out.println("Please enter student's score >");
int score = input.nextInt();

//If statement
if ((name != "alldone") && (score > highScore)) {
highName = name;
highScore = score;
}else {

}
}

//Output: highName + highScore
System.out.println(highName + " had the highest score which was " + highScore);

//Closed Scanner Object
input.close();
}
}
MGA
  • 1
  • 2
  • `if ((name != "alldone")` need to compare strings properly, at least. https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – markspace Apr 19 '18 at 01:31
  • That was an option I tried earlier but for some reason the output is the same as when I use !name.equals("alldone") and if ((name != "alldone") – MGA Apr 19 '18 at 01:40

2 Answers2

0

A few things you should consider, especially since you are new with this language.

A) The only scenarios you should ever really use "!=" or "==" in java are for primitive type comparisons (int's, long's, byte's, char's, Enum's, etc)

//i.e
if (2 != 3) //False
if (1000 == 1000) //True
if (SomeEnum.SomeEnumType == SomeEnum.SomeOtherEnumType) //False

B) When you are comparing two objects, which are not primitive, you must do so via the "equals(Object obj)" method built into the core of java. This method determines the equality between two different objects. You can implement your own equals function in any class or object you wish, by adding the "@Override" annotation above your own "public boolean equals(Object obj)" implementation.

if ("This".equals("That")) //False
if (!("This".equals("That"))) //True
if (SomeRandomObject.equals(SomeOtherRandomObject)) //Truth value is dependent on what makes "SomeRandomObject" equals method return true for a comparative input

C) Although the keyword "do", in java, is not widely used.. This is one of the scenarios where it could prove to be more efficient, instead of checking the same truth value twice. You can use a do-while loop like this:

Scanner input = new Scanner(System.in);
String name = "";
do {
    name = input.nextLine();
    score = input.nextInt();
    if (score > highScore) {
        highScore = score;
        highName = name;
    }
} while (!name.equals("alldone"));
Joe
  • 1,316
  • 9
  • 17
0

A couple of changes are necessary. One is the name != "alldone" to !name.equals("alldone") as mentioned by everyone above. Another, try taking the if (!name.equals("alldone") before the prompt for score is printed. That way, you will also not have to check the condition in the lower part. Also, instead of name = input.nextLine(), try name = input.next(). i.e. Try:

import java.util.Scanner;

public class practice {

    public static void main(String[] args) {

        //Scanner Object
        Scanner input = new Scanner(System.in);

        //Variables
        String name = "Nobody";
        String highName = "";
        int highScore = 0;
        int score = 0;

        //While-Loop
        while (!name.equals("alldone")) {

        //User Input Required
            System.out.println("Please enter student's name, or \"alldone\" if finished >");
            name = input.next();

            if(!name.equals("alldone")){
                System.out.println("Please enter student's score >");
                score = input.nextInt();

                //If statement
                if (score > highScore) {
                    highName = name;
                    highScore = score;
                }   
            }
        }

        //Output: highName + highScore
        System.out.println(highName + " had the highest score which was " + highScore);

        //Closed Scanner Object
        input.close();
    }
}