Results 1 to 7 of 7
  1. #1
    dc2acgsr99's Avatar
    dc2acgsr99 is offline Member
    Join Date
    Jan 2008
    Location
    Pittsburgh
    Posts
    26
    Rep Power
    0

    Default I need some help please.

    Heres whats going on, I need a program for class that...

    1. asks for number of students.
    2. give an input box for their name.
    3. give an input box for student score.
    4. repeat steps 3 and 4 as many times as inputed in step 1.
    5. show the students scores in highest to lowest order with the average.
    6. must check for errors.
    7. if a number is put where a letter should be or vise versa it must be caught until user quits or enters right response.

    now here is where I stand and I will be standing for a long time because I just don't know how to get all that to work. He said something about "arrays" but did not say much about them. I am lost and need much o help here

    Java Code:
     	import javax.swing.*;
     
     public class kthLab3
     {
     	
     	public static void main(String [] args)
     	{
     		progra();
     		System.exit(0);
     	}
     	
     	public static void progra()
     	{	
     		intro();
     		numberOf();	
     	}
     
     	public static void intro()
     	{
     		JOptionPane.showMessageDialog(null, "This program will take a given number of students and show you the user their scores from highest to lowest with the class average.");
     	}
     	
     	public static void numberOf()
     	{
     		String response;
     		
     		response = JOptionPane.showInputDialog("Enter number of students.");
     		
     		if (response == null){JOptionPane.showMessageDialog(null, "You have chose to quit using the program.");
    			System.exit (0);}
     	
     		else 
    	    	{response = JOptionPane.showInputDialog 
    	    	("Please enter a number.");}
     	
     		response = JOptionPane.showInputDialog("What is the students name?");
    Hey I'm still learning... Thats my story and I'm sticking to it :)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Arrays;
    import javax.swing.JOptionPane;
     
     public class Lab3
     {
         public static void main(String[] args)
         {
             intro();
             int numberOfStudents = 0;
             String response = null;
             do
             {
                 response =
                     JOptionPane.showInputDialog("Enter number of students.");
                 numberOfStudents = Integer.parseInt(response);
             }
             while(response == null || response.length() == 0);
             String[] names = new String[numberOfStudents];
             int[] scores = new int[numberOfStudents];
             getInput(names, scores);
             System.out.printf("names = %s%n", Arrays.toString(names));
             // Now you can process the collected data.
         }
    
        public static void intro()
        {
            JOptionPane.showMessageDialog(null, "This program will take " +
                      "a given number of students and show you the user " +
                      "their scores from highest to lowest with the " +
                      "class average.");
        }
     
        public static void getInput(String[] names, int[] scores)
        {
            // Fill the arrays with user input.
            int count = 0;
            do
            {
                String s =
                    JOptionPane.showInputDialog("What is the students name?");
                names[count] = s;
                s = JOptionPane.showInputDialog("Please enter a number.");
                scores[count] = Integer.parseInt(s);
            }
            while(count++ < names.length-1);
        }
    }

  3. #3
    dc2acgsr99's Avatar
    dc2acgsr99 is offline Member
    Join Date
    Jan 2008
    Location
    Pittsburgh
    Posts
    26
    Rep Power
    0

    Default

    Wow thank you so much for that. I have been studying the code you presented me with and am starting to grasp the concept. I am just stuck on figuring where to put a "catch (Exception e)" so the program will catch when a letter is put in place of a number or vice versa. Problem is I can not find where to put the "try." I am thinking that two try catches are needed since there is an input for the names and the numbers. Am I right in that assumption? Also I am trying to get the results to show on a "JOptionPane.showMessageDialog" but as with the catch phrase I keep getting many errors when I place it in there. Can someone give me a general idea of where the try catch needs to be?
    Hey I'm still learning... Thats my story and I'm sticking to it :)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
        public static void main(String[] args)
        {
            intro();
            int numberOfStudents = 0;
            boolean succeded;
            do
            {
                succeded = true;
                String response =
                    JOptionPane.showInputDialog("Enter number of students.");
                try
                {
                    numberOfStudents = Integer.parseInt(response);
                }
                catch(NumberFormatException e)
                {
                    JOptionPane.showMessageDialog(null, "NFE: " + e.getMessage());
                    succeded = false;
                }
            }
            while(!succeded);
            String[] names = new String[numberOfStudents];
            ...
        public static void getInput(String[] names, int[] scores)
        {
            ...
                String s =
                    JOptionPane.showInputDialog("What is the students name?");
                names[count] = s;
                s = JOptionPane.showInputDialog("Please enter a number.");
                // You can do the same here as above: guard this [i]parseInt[/i]
                // method call with a try/catch statemnt and JOptioPane
                // notification. Incrementing the "count" variable may
                // need some attention/help.
                scores[count] = Integer.parseInt(s);
            }
            while(count++ < names.length-1);
        }

  5. #5
    dc2acgsr99's Avatar
    dc2acgsr99 is offline Member
    Join Date
    Jan 2008
    Location
    Pittsburgh
    Posts
    26
    Rep Power
    0

    Default

    Its been a while since I been here, just started a job and haven't had much time to work on my Java class work between work and other classes. Anyways below is my code as it stands but my problem is with the average number for the project. For some reason it is not showing the average of the numbers I input.. I am at a complete loss here. So here is the code, a second set of eyes would be great, Thanks....

    Java Code:
    import java.util.Arrays;
    import javax.swing.*;
     
     public class Lab3
     {
         public static void main(String[] args)   //start main
         {
         	program();
         	System.exit(0);
         }   //end main
         
         public static void program()   //start program
         {   
             intro();
             
             int numberOfStudents = 0;
             String response = null;
             
             do
             {
                 response =
                 JOptionPane.showInputDialog("Enter number of students.");
                 numberOfStudents = Integer.parseInt(response);
             }
             
             while(response == null || response.length() == 0);
             String[] names = new String[numberOfStudents];
             int[] scores = new int[numberOfStudents];
             
             getInput(names, scores);
             System.out.printf("names = %s%n", Arrays.toString(names));
             System.out.printf("scores = %s%n", Arrays.toString(scores));
             
             // get max
             int myMax;
       	     myMax = max (scores);
       	     System.out.println ("MyMax is: " + myMax);
       	  
       	     // get min
             int myMin;
       	     myMin = min (scores);
       	     System.out.println ("MyMin is: " + myMin);
       	 
       	 	 // get average
       	 	 int myAvg;
       	     myAvg = doAvg (scores);
       	     System.out.println ("MyAvg is: " + myAvg);
       	  }   //end program
    
        public static void intro()   //start msg
        {
            JOptionPane.showMessageDialog(null, "This program will take " +
            "a given number of students and show you the user their scores "+
            "from highest to lowest with the class average.");
        }  //end msg
     
        public static void getInput(String[] names, int[] scores)   //start info grab
        {
            // Fill the arrays with user input.
            int count = 0;
            do
            {
                String s =
                JOptionPane.showInputDialog("What is the students name?");
                names[count] = s;
                s = JOptionPane.showInputDialog("Please enter a number.");
                scores[count] = Integer.parseInt(s);
            }
            while(count++ < names.length-1);
        }   //end infor grab
       	  
        public static int max(int[] t)   //start max num
        {
       	
        int maximum = t[1];
        
          for (int i = 0; i < t.length; i++)
          {
            if (t[i] > maximum) 
            {
                maximum = t[i];
            }
          }
        return maximum;
        }   //end max num
        
        
        public static int min(int[] t) 
        {
        int minimun = t[1];   // start min num
        
          for (int i = 0; i < t.length; i++)
          {
            if (t[i] < minimun) 
            {
                minimun = t[i];   
            }
          }
        return minimun;
        }   //end end min num
        
        public static int doAvg(int[] scores)   //start avg num
    	{
        int  sum = 0;
        
        	for (int ctr = 0; ctr < scores.length; ctr++) 
        	{
            	sum += scores[ctr];
        	}
        return sum / (scores.length - 1);
    	}   //end avg num
        
     }
    Hey I'm still learning... Thats my story and I'm sticking to it :)

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
       	 	 // get average
       	 	 double myAvg;
    ...
        public static double doAvg(int[] scores)   //start avg num
    	{
        double  sum = 0;
        
        	for (int ctr = 0; ctr < scores.length; ctr++) 
        	{
            	sum += scores[ctr];
        	}
        return sum / (scores.length - 1);
    	}   //end avg num

  7. #7
    dc2acgsr99's Avatar
    dc2acgsr99 is offline Member
    Join Date
    Jan 2008
    Location
    Pittsburgh
    Posts
    26
    Rep Power
    0

    Default

    I see the difference here is the use of doubles. When I compile the code with doubles in the doAvg I get error messages. What reason would there be for it to be getting wrong averages? Also I am trying to stick to integers.
    Hey I'm still learning... Thats my story and I'm sticking to it :)

Posting Permissions

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