• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI adding extra value to variable at runtime  RSS feed

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...So i have this program.it has four classes; Student, Module, Test,and a GUI class. Now in the Module class, i have a variable called numOfStudentsRegistered,when i run the program from the Test class it gives me 3 as expected.But when i run the GUI version of the program, it starts to add one every time i hit the submit button...here are the classes.I need help




 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's really kind of important that you get the details right. You do not have a variable named "numOfStudentsRegistered" anywhere in the code you listed.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad, it's int numOfRegisteredStudents; it's in the Module class.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but you have a load of code with that illegible output you get from NetBeans/Matisse.
Why are you adding Strings in the Module class when you want to add a Student. Not addStudent(String name, String id), but addStudent(Student s).

I think you have gone about the exercise quite the wrong way. you should not be setting up a GUI to display Students. You should be setting up a student application with a public API (interface), and calling that interface from the GUI. You should test the app much more intensively before adding the GUI.
I think you are counting Student objects every time you display the output, and not resetting the number to 0. The toString method is the wrong place to count elements.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Student application with an interface? how do you mean?
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to be able to run this sort of code from the command line. Obviously removal methods are also called for.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it different from what I already have?
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code you are printing it twice. I think there is an error to do with your counting things in the toString method, when the add() method would be a better place to count them.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&%£%"* it....i moved the numberOfRegisteredStudents++ statement to the addStudent method and removed the For loop around it,and Voila!!! it worked...If you ever run for mayor,you got my vote.Thanks
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I’d hate to be mayor, but “you’re welcome
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...It's Oliver Twist.am back could you have a look at my code once again? now the courseworkAverage, overallMarkAverage, examAverage variables are acting exactly the same way the numOfRegisteredStudent++ statement was acting...The values are adding up double each time i run the program...i knw i need to take the code out of the toString() method,but i just can't figure out where to...spent the whole day working on this...help
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no point in anyone looking at your code from two days ago. If you want help with your current code, you need to SHOW us your current code.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks...here is the current code:
When you click "submit" in the GUI, the values of courseworkAverage,examAverage,and overallMarkAverage are calculated twice instead of once for each entery....




A whole lotta code..sorry.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see no point in posting lots of GUI code, least of all with GroupLayout code, because so few of us can actually read it.

Back to the command line. There is no point in putting a GUI atop an app until it is working at the command line.
How are you calculating the average? Is it a field or a local variable? Go through the code and the flow of control d you should find the error. It is probably something similar to the error with counting students.
And why does your add method not take a Student object as its parameter? That is poor design. It requires the Module class to “know” about the workings of the Student class, and take responsibility for something which should happen entirely within the student class (i.e. creating an instance).
Why are you using MAX_NUMBER_etc++? If it is written in upper‑case, then it is a constant. In which case you ought not to change it. If you mean how many students have enrolled, then the name and case of the variable should make that obvious.
Never use \n unless you have been specifically told to use the LF character. Use %n in a format method.
Don’t use the + operator on Strings in more than one line. Use a StringBuilder and its append method. The + operator only gives good performance on Strings when used several times in the same statement.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!