File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

beginner question

 
taha khalid
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

this is my first question in javaranch.. I'm new here.
I'm trying to design a project in java as a test project. i.e. you create account in the program and enter to answer few questions. the problem I have when creating the Jframe for the questions.
it suppose to have a question (as JLabel) that change whenever I click next to take the other question from MS database. and the radio button selected will be saved in an array to be compared later at the end of questions with the right answers ..

the code I wrote for the button action is






the code is not working before I added the if the code change the question whenever I hit the button but after adding the if it stopped from working .. any advice ... thanks a lot for help

[Added code tags - see UseCodeTags for details]
java.JPG
[Thumbnail for java.JPG]
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome,

Look at what you are doing,

No matter what is selected rs.next() is called and then question.setText(rs.getString("question"));

This will always return the same result!

WP
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Taha. Welcome to the Ranch!

I've got an idea what the problem might be (though that might just be because of code that's missing from the bit you've posted), but the first thing you need to fix is this:
Never do this! At the moment you are catching any exceptions that happen, and completely ignoring them. It's likely that there's an exception being thrown that would tell you exactly what's gone wrong and what line it's happened on, but with an empty catch block you're deliberately hiding this useful information. At the very least print out the stack trace.

Change that, and let us know if you get details of any error.
 
taha khalid
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:Welcome,

Look at what you are doing,

No matter what is selected rs.next() is called and then question.setText(rs.getString("question"));

This will always return the same result!

WP




yes this is my plan whatever radio button is selected the next question will be shown but the answer will be saved in an array for checking later....
 
taha khalid
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Hi Taha. Welcome to the Ranch!

I've got an idea what the problem might be (though that might just be because of code that's missing from the bit you've posted), but the first thing you need to fix is this:
Never do this! At the moment you are catching any exceptions that happen, and completely ignoring them. It's likely that there's an exception being thrown that would tell you exactly what's gone wrong and what line it's happened on, but with an empty catch block you're deliberately hiding this useful information. At the very least print out the stack trace.

Change that, and let us know if you get details of any error.



I will try to put some exceptions handling and check it ... thanks a lot for your help
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another way to approach this (easier perhaps) is to create a collection of Question objects,
and read all the db stuff into those objects when the program opens.

the Question object could have:
the question to display
the choices to display
the user's answer/selection
the correct answer if a quiz-type question

click 'next':
the user's selection is recorded in the currently displayed Question object
the radioButton selection is cleared
the text of the label and radioButtons change to that of the next Question object
(you could even disable the 'next' button until a selection is made)

at the end you iterate the Question object collection for all the user's answers/selections
 
taha khalid
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:another way to approach this (easier perhaps) is to create a collection of Question objects,
and read all the db stuff into those objects when the program opens.

the Question object could have:
the question to display
the choices to display
the user's answer/selection
the correct answer if a quiz-type question

click 'next':
the user's selection is recorded in the currently displayed Question object
the radioButton selection is cleared
the text of the label and radioButtons change to that of the next Question object
(you could even disable the 'next' button until a selection is made)

at the end you iterate the Question object collection for all the user's answers/selections







yes this is really a better way to do it ... but the problem im not sure that i will manage to make it as an executable code (i.e. I doubt I will know how to write it without errors) but thanks a lot for this idea
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic