Results 1 to 9 of 9
- 06-05-2010, 06:26 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 42
- Rep Power
- 0
what is wrong with my code?? URGENT, i have to finish this tonight :S
let me tell you about my program first, the program is to ask the user to enter a number of students. then the same number of textfields must be displayed. the user has to enter students names, and grades for 6 subjects.
when i run this code and add the number of students, the textfields doesn't show up, i don't know why :confused:
by the way, I'm a beginner so there is alot of things that i don't know yet :o
=================================================
Moderator Edit: Code tags addedJava Code:import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class students_grades extends Applet implements ActionListener { Label title = new Label("<{This program calculate students grades in 6 subjects}>"); Label number_of_students = new Label("<{Inter the number of the students}>"); TextField number_of_students_ = new TextField(5); Button View_Table = new Button ("View Table"); public void init( ) { //setLayout(); add (title); add (number_of_students); add (number_of_students_); add (View_Table); number_of_students_.addActionListener(this); View_Table.addActionListener(this); } public void actionPerformed(ActionEvent event) { // to get the number of students entered by user // String titles [8] = [No. , Student Name, Math, Comp, Salam, Arab, MGT, ORG]; String number_of_students_text = number_of_students_.getText(); int number_of_students_int = Integer.parseInt(number_of_students_text); // define two arrays of textfield TextField txtname []= new TextField [number_of_students_int]; TextField txtgrade [][] = new TextField [number_of_students_int][6]; // define two arrays ===> one for student names, and the other one for students grade String students_names [] = new String [number_of_students_int]; float students_grades [][] = new float [number_of_students_int][6]; // add name text fields for (int i=0; i<number_of_students_int; i++) { this.add (txtname[i]); } // add grade text fields in 2D array for (int i=0; i<number_of_students_int ;i++) { for (int j=0; j<6 ; j++) { add (txtgrade[i][j]); } } // get text from text fields and save it in the array for (int i=0; i<number_of_students_int; i++) { students_names [i] = txtname[i].getText(); } for (int i=0; i<number_of_students_int ;i++) { for (int j=0; j<6 ; j++) { String Container = txtgrade[i][j].getText(); int intContainer = Integer.parseInt(Container); students_grades[i][j] = intContainer; } } } // action performed } // classLast edited by Fubarable; 06-05-2010 at 06:53 PM. Reason: Moderator Edit: Code tags added
-
OP, I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.
To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luckJava Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 06-05-2010, 07:12 PM #3
What happens when you execute it? Do you get any error messages in the browser's Java console? Please copy and paste them here if you need help.
- 06-05-2010, 07:17 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 42
- Rep Power
- 0
can you see the codes now?
Java Code:import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class students_grades extends Applet implements ActionListener { Label title = new Label("<{This program calculate students grades in 6 subjects}>"); Label number_of_students = new Label("<{Inter the number of the students}>"); TextField number_of_students_ = new TextField(5); Button View_Table = new Button ("View Table"); public void init( ) { //setLayout(); add (title); add (number_of_students); add (number_of_students_); add (View_Table); number_of_students_.addActionListener(this); View_Table.addActionListener(this); } public void actionPerformed(ActionEvent event) { // to get the number of students entered by user // String titles [8] = [No. , Student Name, Math, Comp, Salam, Arab, MGT, ORG]; String number_of_students_text = number_of_students_.getText(); int number_of_students_int = Integer.parseInt(number_of_students_text); // define two arrays of textfield TextField txtname []= new TextField [number_of_students_int]; TextField txtgrade [][] = new TextField [number_of_students_int][6]; // define two arrays ===> one for student names, and the other one for students grade String students_names [] = new String [number_of_students_int]; float students_grades [][] = new float [number_of_students_int][6]; // add name text fields for (int i=0; i<number_of_students_int; i++) { this.add (txtname[i]); } // add grade text fields in 2D array for (int i=0; i<number_of_students_int ;i++) { for (int j=0; j<6 ; j++) { add (txtgrade[i][j]); } } // get text from text fields and save it in the array for (int i=0; i<number_of_students_int; i++) { students_names [i] = txtname[i].getText(); } for (int i=0; i<number_of_students_int ;i++) { for (int j=0; j<6 ; j++) { String Container = txtgrade[i][j].getText(); int intContainer = Integer.parseInt(Container); students_grades[i][j] = intContainer; } } } // action performed } // class
- 06-05-2010, 07:24 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 42
- Rep Power
- 0
no there is no errors
when i execute it and add the number of the students and press the button, nothing happen!!!!
- 06-05-2010, 07:53 PM #6
Are you sure? When I execute it I get Null Pointer exceptions.
Comments:
Defining an array does not put values into the elements of the array. You must do that for every element in the array.
TextField[] tf = new TextField[10]; // defines an array of empty slots
tf[0] = new TextField(); // fill slot 0 the the array with ref to a tf
Your code gets into some advanced layout management. You add components to the applet display and then wait for user input. When you get the input, you want to change the display. To do that you must use the removeAll() method to get rid of the old stuff,
add the new stuff and then call validate() to tell the system to do another layout of your components.
You'll need a new button for the user to use AFTER he has filled in the fields so you can get the data he has entered.
- 06-05-2010, 09:51 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 42
- Rep Power
- 0
i defined the array as you said, but it shows error in the array!!
- 06-05-2010, 10:04 PM #8
Sorry, I don't have my long range glasses on. I can't see either the code or the error.
You'll have to post both of them.
- 06-06-2010, 03:10 PM #9
with the code above, you create arrays of TextField, but the reference of the array is pointing to null! in order to fix that use
Java Code:for (int j=0; j<6 ; j++) { TextField gradefield = new TextField(); txtgrade[i][j] = gradefield; add (txtgrade[i][j]); }
now, each element of the array is referencing an object of type TextField. apply the same to the other TextFields and for the layout of these fields i would use a gridlayout.
Similar Threads
-
Assignment due tonight, please help me!
By twiggy62 in forum New To JavaReplies: 4Last Post: 02-09-2010, 06:07 AM -
What's going wrong with this code?
By Suurisa in forum New To JavaReplies: 5Last Post: 10-19-2009, 11:59 PM -
what is wrong in dis code?
By jitun2004 in forum New To JavaReplies: 8Last Post: 04-15-2009, 09:30 AM -
What is wrong with this code
By rosh72851 in forum New To JavaReplies: 13Last Post: 10-31-2008, 01:50 AM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks