Results 1 to 5 of 5
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default problems with if statement

    I am attempting to write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the name of the student with the highest score. However, instead of displaying the student with the highest score, the program displays the name and score of the last student input. I'd appreciate it if someone could take a look at my code and tell me where I am going wrong. Thank you.

    Java Code:
    /* HighestScore.java
     * jim01
     * 11/23/2010
     * This program sorts inputted students names and scores and displays the
     * student with the highest score.
    */
    
    import java.util.Scanner;
    
    public class HighestScore {
    	public static void main(String [] args) {
    
    		//create variables
    		double highestScore = 0;
    		double studentScore = 0;
    		String studentName = "";
    		double count = 0;
    		double totalStudents = 0;
    
    		//input and processing
    
    		//create new scanner object
    		Scanner input = new Scanner(System.in);
    
    		//prompt user to enter the total number of students
    		System.out.print("Enter the total number of students: ");
    		totalStudents = input.nextDouble();
    
    		/*Prompt user to enter the student names and scores and keep   track of the number of students*/
    		while ( count < totalStudents ) {
    			//Prompt user to enter student name
    			System.out.print("Enter Student Name: ");
    			studentName = input.next();
    			//Prompt user to enter student score
    			System.out.print("Enter Student Score: ");
    			studentScore = input.nextDouble();
    			count++;
    		}
    
    		if(studentScore > highestScore) {
    			highestScore = studentScore;
    			count++;
    		}
    		//output
    		System.out.println(studentName + " has the highest score of " + highestScore);
    	}
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Well, if you hadn't already figured out, these four lines are the issue:
    Java Code:
    		if(studentScore > highestScore) {
    			highestScore = studentScore;
    			count++;
    		}

    Instead of checking who has the highest score, you've simply checked if the last one entered has the highest score.

    What you'll want to do is, instead, put your if statement in the while loop to check each name as it's entered. If it's higher, record the name and score in your highestScore (and another variable, a String called highestStudentName) then output THOSE in your println instead.

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Well, if you hadn't already figured out, these four lines are the issue:
    Java Code:
    		if(studentScore > highestScore) {
    			highestScore = studentScore;
    			count++;
    		}

    Instead of checking who has the highest score, you've simply checked if the last one entered has the highest score.

    What you'll want to do is, instead, put your if statement in the while loop to check each name as it's entered. If it's higher, record the name and score in your highestScore (and another variable, a String called highestStudentName) then output THOSE in your println instead.
    OK, I see where I made the mistake of not incorporating the if statement with the while statement. However, I am confused on how to match the name of the person with the highest score with the highestStudentName variable. I tried this:

    Java Code:
    while ( count < totalStudents ) {
    			//Prompt user to enter student name
    			System.out.print("Enter Student Name: ");
    			studentName = input.next();
    			//Prompt user to enter student score
    			System.out.print("Enter Student Score: ");
    			studentScore = input.nextDouble();
    			count++;
    
    		if(studentScore > highestScore) {
    			highestScore = studentScore;
    			studentName = highestStudentName;
    			count++;
    		}
    		}
    		//output
    		System.out.println(highestStudentName + " has the highest score of " + highestScore);
    	}
    }
    but I knew that was wrong before I even compiled and ran it.

    I'm having a real hard time with if statements. I don't know what I'm missing but I can't seen to figure them out.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You're actually completely correct except for two things:
    1) The count++; inside the if statement shouldn't be there. You only want count to increase once, and it already does that.
    2) studentName = highestStudentName; -- remember how variables work! This should be the other way around, as you want the highestStudentName to BECOME the studentName. ;)

    Give that a try and see how it works for ya.

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    I completely forgot about the count. That explains why I was having problems when I combined the if statement with the while statement.

    It works now. I don't know why I couldn't see that I had it backwards, especially since highestScore = studentScore was directly above it. LOL! Duh.

    I hope I get the hang of these darned if statements sooner or later. They are hampering my progress and my final exam is less than a month away.

    Thank you for your help.

Similar Threads

  1. While statement problems
    By Danieldcc in forum New To Java
    Replies: 5
    Last Post: 10-18-2010, 04:37 PM
  2. problems with print statement
    By soccer_kid_6 in forum New To Java
    Replies: 8
    Last Post: 03-22-2010, 03:24 PM
  3. add an If Else statement and......uh????
    By sonny in forum New To Java
    Replies: 6
    Last Post: 03-04-2010, 07:57 PM
  4. for statement help
    By helpisontheway in forum New To Java
    Replies: 5
    Last Post: 11-14-2009, 05:14 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •