Java5: A simple code to find max score out of the user's inputs

Hi experts:

I am trying to write a simple code to get exam multiple exam scores from a student, and to find the best
score.  The problem is that I do not know how many exam scores the student may enter.
As soon as the student enters a sentinal (-1), the program is to quit and find the best score among the exam scores that the student entered  until  he entered  -1.

Here is my incomplete code which obviously is not working.

Hope you experts may kindly help me again!

_________________ Here is my code ______________________

import javax.swing.JOptionPane;

public class findBestScore
{
 
  public static void main (String[] args)
  {
    FindBestScore duta = new findBestScore ();
   
    duta.findBest ();
   
    System.exit (0);
  }
 
 
  public int[] getScore ()
  {
       
    int [] scores = new int [count];
     
    for (int i; i < count; i++ )  
     {
      while ( scores[i] != - 1) // enter -1 to quit
       {
         scores [i] = getInput ("Enter exam scores " + (i + 1) );
       }
    }
    return scores;
  }
 
 
 
 public int getInput (String msg)
 {
   int num = Integer.parseInt (JOptionPane.showInputDialog (msg) );
   return num;
 }
                                   
  public void display (String msg)
  {
    JOptionPane.showMessageDialog (null, msg);
  }
 
  public int findBest ( )
  {
    int [] scores = getScores ();
    int i = 0;
    int max = 0;
    for ( i = 0; i < scores.length; i ++ )
    {
       max = scores[0];
     
      if ( scores[i] > max )
      {
        max = scores [i];
      }
     
    }
    return max;
    display ("The maximum score is " + max);
  }
   
}                
dutaAsked:
Who is Participating?
 
kannan_ekanathConnect With a Mentor Commented:
List is a collection. You can read any collection tutorial from the Suns standard website http://java.sun.com/docs/books/tutorial/collections/index.html

The following is a working code,

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class FindMax extends JFrame {
      public static void main(String[] args) {
            FindMax duta = new FindMax();
            duta.findBest();
            System.exit(0);
      }

      public List getScore() {
        List scores = new ArrayList();
        Integer score;
            int index = 1;
        while((score = getInput("Enter exam score [" + (index++ ) + "] (-1 to finish)")).intValue() != -1) {
             scores.add(score);
        }
        return scores;
   }

      public Integer getInput(String msg) {
            return Integer.valueOf(JOptionPane.showInputDialog(msg));
      }

      public void display(String msg) {
            JOptionPane.showMessageDialog(null, msg);
      }

      public void findBest() {
            List list = getScore();
            Collections.sort(list);
            display("The maximum score is " + list.get(list.size() - 1));
      }
}

List is one type of collection. As the name implies, it is ordered :)
0
 
dutaAuthor Commented:
Here is my latest code which is working:


My question is still there:  In my latest code, I asked the student "how many scores" which he is going to enter.

Sometimes, I may need to ask students to enter any number of scores he may feel like entering, and to find the best score among the inputs. In other words, I may not know how many scores students may enter, and I may not ask how mahy scores they are going to enter.

In that situation, I need to modify my current code.  

Thanks a lot for your kind help as always!

duta



______________ Here is my latest code ___________________

import javax.swing.JOptionPane;

public class findMax
{
 
  public static void main (String[] args)
  {
    findMax duta = new findMax ();
   
    duta.findBest ();
   
    System.exit (0);
  }
 
 
  public int[] getScore ()
  {
    int count     = getInput ("How many scores?");    //   ??????????????????????
    int [] scores = new int [count];                         //  ???????????????????????
   
   {
    for (int i=0; i < count; i++ )  
     {
   
       {
         scores[i] = getInput ("Enter exam score " + (i + 1) );
         if (scores[i] == -1)
             break;
       }
    }
   }
    return scores;
  }
 
 
 
 public int getInput (String msg)
 {
   int num = Integer.parseInt (JOptionPane.showInputDialog (msg) );
   return num;
 }
                                   
  public void display (String msg)
  {
    JOptionPane.showMessageDialog (null, msg);
  }
 
  public void findBest ( )
  {
    int [] scores = getScore ();
    int i = 0;
    int best = 0;
    for ( i = 0; i < scores.length; i ++ )
    {
       best = scores[0];
     
      if ( scores[i] > best )
      {
        best = scores [i];
      }
      best = best;
    }
   
    display ("The maximum score is " + best);
  }
   
}                                    
   
     
 
0
 
kannan_ekanathCommented:
Duta!
You need to close the previous questions :) before I think you are reliable enough to spend time :) or atleast say if it wasnt useful :)

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22000302.html
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dutaAuthor Commented:
Dear kannan:
Sorry for my procrastination.  It is my fault. I took your tip as acceptable solution.

Now is your turn to help me again :)
0
 
kannan_ekanathCommented:
Duta,
Kindly note that I am not *forcing you* into accepting my solution :). However you need to write a while loop to get outside your scores. I am showing you the getScores() method *only* to show you how it would look. I am sure you should be able to take it from there on.

       public List getScore() {
            List scores = new ArrayList();
            int score = 0, index = 1;
            while((score = getInput("Enter exam score [" + (index++ ) + "] (-1 to finish)")) != -1) {
                  scores.add(score);
            }
            return scores;
      }

Now, you have a list, just call
Collections.sort(scores);
//print scores.get(scores.size()-1) ; //this being the last element

The crux is the while loop to terminate until he pressed -1. Since this sounds like homework, kindly dont ask for full code ;)
0
 
dutaAuthor Commented:
Dear kannan:

Thank you so much for giving me tons of priceless tips and teaching me how to use this site properly :)

Every tip that you have given me  has tremendously helped me to hang on and to continue to learn things that sometimes looked too much for me (for my intelligence).

I will do my best to fix it using your valuable tip.

Thanks again!

duta
0
 
dutaAuthor Commented:
Dear kannan:

No, it is not a homework.  I am trying to learn myself by trying this code and that.

Thanks a lot for your constant help :)
0
 
dutaAuthor Commented:
TO: kannan:

I have never used List and have no idea about how to call it.

This is not a homework or anything like that. If you check my history of questions, they cover almost all subjects, and you may know that my questions have little to do with any homework.

By the way,  I just wonder whether you may show me how to use the last part of your tip:

Now, you have a list, just call
Collections.sort(scores);
//print scores.get(scores.size()-1) ; //this being the last element

Without your further help, I may have to spend many more days trying to figure it out :)

Thanks again!
0
 
dutaAuthor Commented:
TO: kannan:

Thank you so much for helping me out with such petience.

It worked great. I am going to try your tip over and again and make it my own.

Thanks a lot. Hope you may come back again to help me with others.

duta
0
All Courses

From novice to tech pro — start learning today.