Welcome to Dream.In.Code
Become a Java Expert!

Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,192 people online right now. Registration is fast and FREE... Join Now!




For Loop Problem

 
Reply to this topicStart new topic

For Loop Problem, Can't figure out how to display highest and second highest values

Stoughty
1 Feb, 2008 - 04:50 PM
Post #1

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 4

The following program is supposed to allow the user to enter the desired number of students, enter those student's names and scores, and then retrieve and display the highest and second highest scoring students. I'm not quite sure what to do here...

CODE

public class Exercise49 {
/** Main method */
    public static void main(String[] args) {      
      //Enter number of students
      String intString = JOptionPane.showInputDialog(
          "Enter number of students, for example 5:");
      int numberStudents = Integer.parseInt(intString);
            
      for (int i = 1; i <= numberStudents; i++) {
          String studentString = JOptionPane.showInputDialog("Enter student name:");
        
          String studentGrade = JOptionPane.showInputDialog("Enter student score:");
            int studentScore = Integer.parseInt(studentGrade);
~edit: code tags added PB

This post has been edited by PennyBoki: 2 Feb, 2008 - 12:39 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: For Loop Problem
1 Feb, 2008 - 07:06 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Welcome to </dream.in.code> Stoughty, glad you could make it! Hope to see you around the forums someday smile.gif

For future reference you need to post your programming questions in the proper forum, . Don't worry about reposting it the proper forum, a Mod or Team Member will move it for you smile.gif

Happy Coding!
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: For Loop Problem
2 Feb, 2008 - 12:41 PM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi Stoughty, what seems to be the problem with the code? A for loop? OK what about it? Feel free to post the question smile.gif
User is offlineProfile CardPM
+Quote Post

Stoughty
RE: For Loop Problem
2 Feb, 2008 - 04:09 PM
Post #4

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 4

QUOTE(PennyBoki @ 2 Feb, 2008 - 01:41 PM) *

Hi Stoughty, what seems to be the problem with the code? A for loop? OK what about it? Feel free to post the question smile.gif


Hey, well I'm not quite sure how to find the highest number contained in that loop, or the second highest number. How would I go about pulling that information from the current coding?
User is offlineProfile CardPM
+Quote Post

KYA
RE: For Loop Problem
2 Feb, 2008 - 04:34 PM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,951



Thanked: 159 times
Dream Kudos: 1375
My Contributions
QUOTE(Stoughty @ 2 Feb, 2008 - 06:09 PM) *

QUOTE(PennyBoki @ 2 Feb, 2008 - 01:41 PM) *

Hi Stoughty, what seems to be the problem with the code? A for loop? OK what about it? Feel free to post the question smile.gif


Hey, well I'm not quite sure how to find the highest number contained in that loop, or the second highest number. How would I go about pulling that information from the current coding?



Well for the highest grade right after the input is taken have an 'int max' be set to the value of that number. Then after each sequential iteration of the loop have it check if the newest entry is higher then the previous max value such as:

CODE

int max = 0; //declaration
//your loop code .....
//your input line here
if (currentEntry > max)
     max = currentEntry
//So on and so forth at the end max will contain the highest number entered
//print out max or w/e you need to do here


edit: Alternately you can declare an array and simply sort it after all input is finished and return the last two slots of the array.

CODE

Array[] studentGrades = new Array [numberOfStudents];
Arrays.sort(studentGrades);
//etc....


--KYA

This post has been edited by KYA: 2 Feb, 2008 - 04:47 PM
User is online!Profile CardPM
+Quote Post

Stoughty
RE: For Loop Problem
2 Feb, 2008 - 05:38 PM
Post #6

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 4

QUOTE(KYA @ 2 Feb, 2008 - 05:34 PM) *

QUOTE(Stoughty @ 2 Feb, 2008 - 06:09 PM) *

QUOTE(PennyBoki @ 2 Feb, 2008 - 01:41 PM) *

Hi Stoughty, what seems to be the problem with the code? A for loop? OK what about it? Feel free to post the question smile.gif


Hey, well I'm not quite sure how to find the highest number contained in that loop, or the second highest number. How would I go about pulling that information from the current coding?



Well for the highest grade right after the input is taken have an 'int max' be set to the value of that number. Then after each sequential iteration of the loop have it check if the newest entry is higher then the previous max value such as:

CODE

int max = 0; //declaration
//your loop code .....
//your input line here
if (currentEntry > max)
     max = currentEntry
//So on and so forth at the end max will contain the highest number entered
//print out max or w/e you need to do here


edit: Alternately you can declare an array and simply sort it after all input is finished and return the last two slots of the array.

CODE

Array[] studentGrades = new Array [numberOfStudents];
Arrays.sort(studentGrades);
//etc....


--KYA


Thanks for all the help guys, I appreciate it! I think I'll try to use the array method...

User is offlineProfile CardPM
+Quote Post

fsloke
RE: For Loop Problem
4 Feb, 2008 - 08:44 PM
Post #7

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
First time I saw this function

Arrays.sort(studentGrades);

Quite surprising.

Actually using array is more feasible because you can loop them in for loop ...

Then you can swap them also....

lol...

Thank ya...
User is offlineProfile CardPM
+Quote Post

KYA
RE: For Loop Problem
5 Feb, 2008 - 01:37 AM
Post #8

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,951



Thanked: 159 times
Dream Kudos: 1375
My Contributions
QUOTE(fsloke @ 4 Feb, 2008 - 10:44 PM) *

First time I saw this function

Arrays.sort(studentGrades);

Quite surprising.

Actually using array is more feasible because you can loop them in for loop ...

Then you can swap them also....

lol...

Thank ya...



I suppose you could use the Collections class as well.

--KYA
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:16PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month