Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Program won't go to result after input. Help ASAP please..

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Program won't go to result after input. Help ASAP please..

    I have to create a program that takes a list of student names and grades and then takes the 2 highest scores and reads them, but the result won't show. I'm pretty sure it's because I'm not sure about how to code the last part which should output the results. Here's my code. Help please and thank you.
    import java.util.Scanner;
     
    //Reads input and outputs 2 highest scores
    //for loop. nested for.
    public class HighestScores {
    	public static void main(String[] args) {
    //create values
    	String studentName;
    	String student1 = null;
    	String student2 = null;
    	double studentScore = 0.0;
    	double highestScore = 0.0;
    	double secondHighest = 0.0;
    	int numberStudents = 0; //How many students in class
     
    //User inputs for loop
    	Scanner input = new Scanner(System.in);
    		System.out.print("Enter number of students in class: ");
    		numberStudents = input.nextInt();
    //do-while loop w/ number of students -- to go down to 0
    	do {
    		System.out.print("Enter student's name: ");
    			studentName = input.next();
    		System.out.print("Enter student's grade: ");
    			studentScore = input.nextDouble();
    		numberStudents--;
    	}
    		while (numberStudents != 0);
    //Find the 2 highest scores
    	if (highestScore > 90)
    		{ System.out.println("The highest grade is " + highestScore + " by" + student1);
    		}
    	if (secondHighest < highestScore && secondHighest > 90)
    		{ System.out.println("The second highest grade is " + secondHighest + " by" + student2);
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Program won't go to result after input. Help ASAP please..

    the result won't show
    Can you copy the full contents of the console from when you execute the program and paste it here so we can see the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Program won't go to result after input. Help ASAP please..

    Enter number of students in class: 4
    Enter student's name: R
    Enter student's grade: 98
    Enter student's name: L
    Enter student's grade: 99
    Enter student's name: E
    Enter student's grade: 91
    Enter student's name: C
    Enter student's grade: 95
    The student with the highest grade is C with a 95.0
    The student with the second highest grade is  with a 0.0

    I guess it shows the results (last 2 lines), but now it's just showing the highest grade as the last input and not entirely sure how to go about saving all the inputs on this problem so I can do both the highest and 2nd highest grades.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Program won't go to result after input. Help ASAP please..

    Can you post the code that produced the output shown in post#3?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Program won't go to result after input. Help ASAP please..

    import java.util.Scanner;
     
    //Reads input and outputs 2 highest scores
    //for loop. nested for.
    public class HighestScores {
    	public static void main(String[] args) {
    //create values
    	String allStudents;
    	String studentName;
    	String student1 = "";
    	String student2 = "";
    	double allScore = 0.0;
    	double score = 0.0;
    	double score1 = 0.0;
    	double score2 = 0.0;
    	int numberStudents = 0; //How many students in class
     
    //User inputs for loop
    	Scanner input = new Scanner(System.in);
    		System.out.print("Enter number of students in class: ");
    		numberStudents = input.nextInt();
    //do-while loop w/ number of students -- to go down to 0
    	do {
    		numberStudents--;
    		System.out.print("Enter student's name: ");
    			studentName = input.next();
    		System.out.print("Enter student's grade: ");
    			score = input.nextDouble();
    	}
    		while (numberStudents != 0);
     
    //Find the 2 highest scores
     
    if (score > score1) 
    {
        student2 = student1;
        score2 = score1;
        student1 = studentName;
        score1 = score;
    }
    else if (score > score2)
    {
        student2 = studentName;
        score2 = score;
    }
    System.out.println("The student with the highest grade is " + student1 + " with a " + score1);
    System.out.println("The student with the second highest grade is " + student2 + " with a " + score2);
    }
    }

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Program won't go to result after input. Help ASAP please..

    I guess it shows the results (last 2 lines), but now it's just showing the highest grade as the last input and not entirely sure how to go about saving all the inputs on this problem so I can do both the highest and 2nd highest grades.
    Hi, just a suggestion:

    I think it will be best if you put your code
    // you can change conditional statement if you want, and codes inside it
    if (score > score1) 
    {
     
    }
    else if (score > score2)
    {
     
    }
    inside your loop

    before that create a variable that will handle your highest grade and equate it to zero, let say it's double finalGrade
    and whenever you get a grade higher than variable finalGrade,
    replace its value by what you got.
    (Same process for student's name)

    i hope it will help you.

  7. The Following User Says Thank You to dicdic For This Useful Post:

    Elyril (February 20th, 2014)

  8. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Program won't go to result after input. Help ASAP please..

    once you take the second input, its overwriting the first input.
    try reading java arrays.

  9. #8
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Program won't go to result after input. Help ASAP please..

    Your loop iterates numberStudents times but it stores all the values in the same variables, hence there are only 1 set of values with you when the loop ends and that is the last value entered. This is the reason that even after entering values like 98 you are getting 95 as the max value(because it was entered last).

Similar Threads

  1. Fixing a program that reads a .txt file java beginner I need help ASAP
    By coding2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 12th, 2014, 04:45 PM
  2. Trying to input a text file but the program won't read it.
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 27th, 2013, 11:50 AM
  3. GUI program won't take 'char' input code, can't seem to find the correct one...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2012, 07:12 AM
  4. Need simple JAVA program fixing ASAP
    By theviper in forum Paid Java Projects
    Replies: 1
    Last Post: April 14th, 2010, 10:59 AM
  5. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM

Tags for this Thread