I'm trying to learn Java on my own; however this book(for lack of a better word) sucks. It only has integer examples; therefore, I am unsure of how to script names, I'm trying "cha"; however it doesn't work. I want to write a program that reads: the total number of students, students name and score then outputs the students name with the highest score.

Here is what I have so far any assistance in helping me get on the right path would be appreciated.

public static void main(String[] args) {
int
// Prompt the user to enter the number of Students, Each Students Name and Score
String s1 = JOptionPane.showInputDialog(null,
"Enter the number of students",
"Example 32 Input", JOptionPane.QUESTION_MESSAGE);
int n1 = Integer.parseInt(s1);


String s2 = JOptionPane.showInputDialog(null,
"Enter the students score",
"Example 89 Input",
JOptionPane.QUESTION_MESSAGE);
int n2 = Integer.parseInt(s2);


String s3 = JOptionPane.showInputDialog(null, "Enter the students name",
"Example Shane, Kevin, Sara, Anna",
JOptionPane.QUESTION_MESSAGE);


int shs = 0;
while (s1 > 0) {
if (n2 > shs)
shs = n2;
s1 = s1 - 1;
JOptionPane.showMessageDialog(null, "The Student and High Score is " + shs,
"Example Shane 92", JOptionPane.INFORMATION_MESSAGE;
else exit;
}


}
}

Does anyone know how to write Java Source Code?

Hi everyone,

We do know java but you have to do things sequentially. First you need a loop because of the number of students. Don't run everthing in main but have classes with specific uses so everything dosen't seem clutered. Next you would probably need an array list to store the studen't name and values in a certain format that you like and comapre them only at the end.

Richard West

And for Mercer's sake get a better book :)

HeadFirst Java 2nd edition is the one to get.

Why not start with a simple example that uses nothing but the command line.

class test
{
   public static void main(String[] args)
   {
        int grade1 = Integer.parseInt(args[0]);
        int grade2 = Integer.parseInt(args[1]);
        int average = (grade1+grade2)/2;
        System.out.println("average --> " + average);
   }
}

That will give you some experience with command line args. You could also use a BufferedReader to read input rather than arguments, and practice with "if" statements in deciding what letter grade.

I have modified the code and now the only thing that does not work is the Highname:

Instead of being output with the Highscore, it is the last name entered.

public static void main(String[] args) {


        // Prompt the user to enter the number of Students
        String s1String = JOptionPane.showInputDialog(null, "Enter the number of students",
        "Example 32 Input", JOptionPane.QUESTION_MESSAGE);
        int s1 = Integer.parseInt(s1String);
        int highscore = 0;
              
        // Keep reading until s1 = 0
        while (s1 != 0) {

         //Read the next data strings
            String s2 = JOptionPane.showInputDialog(null,
                    "Enter the student's name",
                    "Example Jeff Input", JOptionPane.QUESTION_MESSAGE);

            // Prompt the user to enter the Students Score
            String s3 = JOptionPane.showInputDialog(null,
                    "Enter the students score",
                    "Example 80 Input", JOptionPane.QUESTION_MESSAGE);
            int n3 = Integer.parseInt(s3);

            if (n3 > highscore)
              highscore = n3;
              highname = s2;
                      
            s1 = s1 - 1;            

        }
        JOptionPane.showMessageDialog(null, "Student scored the hightest is " + highscore + " " + highname,
        "Example Shane 92", JOptionPane.INFORMATION_MESSAGE);
    }
}

FOUND THE ANSWER! I can't believe none of you Java gurus picked up on it. I forgot to place brackets after the if statement. If you have JBuilder give it a go, it works like a charm.

public static void main(String[] args) {


        // Prompt the user to enter the number of Students
        String s1String = JOptionPane.showInputDialog(null, "Enter the number of students",
        "Example 32 Input", JOptionPane.QUESTION_MESSAGE);
        int s1 = Integer.parseInt(s1String);
        int highscore = 0;
        String highName = "";
        
        // Keep reading until s1 = 0
        while (s1 != 0) {

         //Read the next data strings
            String s2 = JOptionPane.showInputDialog(null,
                    "Enter the student's name",
                    "Example Jeff Input", JOptionPane.QUESTION_MESSAGE);

            // Prompt the user to enter the Students Score
            String s3 = JOptionPane.showInputDialog(null,
                    "Enter the students score",
                    "Example 80 Input", JOptionPane.QUESTION_MESSAGE);
            int n3 = Integer.parseInt(s3);

            if (n3 > highscore)
              
                {highscore = n3;
                                highName = s2;}

            s1 = s1 - 1;

        }
        JOptionPane.showMessageDialog(null, "Student scored the hightest is " + highName + highscore,
        "Example Shane 92", JOptionPane.INFORMATION_MESSAGE);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.