Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-17-2008, 04:47 AM
dc2acgsr99's Avatar
Member
 
Join Date: Jan 2008
Location: Pittsburgh
Posts: 26
dc2acgsr99 is on a distinguished road
Send a message via AIM to dc2acgsr99
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

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
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-17-2008, 08:38 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-19-2008, 01:19 AM
dc2acgsr99's Avatar
Member
 
Join Date: Jan 2008
Location: Pittsburgh
Posts: 26
dc2acgsr99 is on a distinguished road
Send a message via AIM to dc2acgsr99
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
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-19-2008, 06:30 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
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 parseInt // 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); }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-26-2008, 08:34 PM
dc2acgsr99's Avatar
Member
 
Join Date: Jan 2008
Location: Pittsburgh
Posts: 26
dc2acgsr99 is on a distinguished road
Send a message via AIM to dc2acgsr99
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....

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
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-27-2008, 05:44 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-27-2008, 11:21 PM
dc2acgsr99's Avatar
Member
 
Join Date: Jan 2008
Location: Pittsburgh
Posts: 26
dc2acgsr99 is on a distinguished road
Send a message via AIM to dc2acgsr99
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
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 11:00 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org