Radio Button Unchecked mark didn't reduced using Java Mysql - java

I am creating a simple exam system in Java and Mysql. When I start the exam all the question displayed from the database successfully, if I read the question and answer it then click next button it will be moving to the next question. It is working fine. If the answer is clicked correctly marks added 1. unless unchecked the answer answer become 0 I want it. my problem is if I click correct answer on the radio button marks changes as one. The same question again I select as wrong answer it didn't changes 0, it still mark become 1. How to solve the problem I clear the radio buttons it won't clear. What I tried so far I attached below.
Click Correct answer . mark displayed 1 correctly
enter image description here
after that same question answer change as wrong answer. Mark displayed
1.I need to display if we select wrong answer second time marks become decrement. I need to change as 1 become 0
enter image description here
Code what tried so far I attached below.
Click the Answer
public void answercheck()
{
String studentanswer="";
if(r1.isSelected())
{
if(r1.isSelected() == true)
{
studentanswer = r1.getText();
r2.setSelected(false);
r3.setSelected(false);
r4.setSelected(false);
}
else
{
studentanswer="";
r1.setSelected(false);
r2.setSelected(true);
r3.setSelected(true);
r4.setSelected(true);
}
}
else if(r2.isSelected())
{
if(r2.isSelected() == true)
{
studentanswer = r2.getText();
r1.setSelected(false);
r3.setSelected(false);
r4.setSelected(false);
}
else
{
studentanswer="";
r2.setSelected(false);
r1.setSelected(true);
r3.setSelected(true);
r4.setSelected(true);
}
}
else if(r3.isSelected())
{
if(r3.isSelected() == true)
{
studentanswer = r3.getText();
r1.setSelected(false);
r2.setSelected(false);
r4.setSelected(false);
}
else
{
studentanswer="";
r3.setSelected(false);
r1.setSelected(true);
r2.setSelected(true);
r4.setSelected(true);
}
}
else if(r4.isSelected())
{
if(r4.isSelected() == true)
{
studentanswer = r4.getText();
r1.setSelected(false);
r2.setSelected(false);
r4.setSelected(false);
}
else
{
studentanswer="";
r4.setSelected(false);
r1.setSelected(true);
r2.setSelected(true);
r4.setSelected(true);
}
}
if(studentanswer.equals(answer))
{
marks = marks + 1;
String marks1 = String.valueOf(marks);
txtc.setText(marks1);
}
}
Load all question from the database
public void Connection()
{
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/onlineex","root","");
stat = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stat.executeQuery("select id,question,option1,option2,option3,option4,answer from questions order by id DESC");
while(rs.next())
{
txtqu.setText(rs.getString("id"));
txtques.setText(rs.getString("question"));
r1.setText(rs.getString(3));
r2.setText(rs.getString(4));
r3.setText(rs.getString(5));
r4.setText(rs.getString(6));
answer = rs.getString(7);
}
} catch (ClassNotFoundException ex) {
} catch (SQLException ex) {
}
}
Next Click Button
if(!rs.isFirst())
{
rs.previous();
txtqu.setText(rs.getString("id"));
txtques.setText(rs.getString("question"));
r1.setText(rs.getString(3));
r2.setText(rs.getString(4));
r3.setText(rs.getString(5));
r4.setText(rs.getString(6));
answer = rs.getString(7);
answercheck();
}
Previous Click Button
if(!rs.isLast())
{
rs.next();
txtqu.setText(rs.getString("id"));
txtques.setText(rs.getString("question"));
r1.setText(rs.getString(3));
r2.setText(rs.getString(4));
r3.setText(rs.getString(5));
r4.setText(rs.getString(6));
answer = rs.getString(7);
}

Related

can't delete from getContentResolver().delete

I can't delete a conversation from getContentResolver, I don't know in which part am doing mistakes, as I also searched about these but can't help myself and I also tried different sols which were given on stackoverflow but same result & thanks a lot in advance.
Here is the code:
public static boolean deleteSmsofContact(Context context, String number,
boolean deleteLocked)
{
int result;
if (deleteLocked) {
//changes values
String[] selectionArgs=new String[]{number};
String selection= ""+"address=?";
//
result = context.getContentResolver().delete(Uri.parse("content://sms/"),selection,selectionArgs);
// Log.d("UF","WOW "+result+" " +number);
} else {
result = context.getContentResolver().delete(Constants.URI_SMS,
"address=? AND locked=?", new String[] { number, "1" });
}
if (result > 0) {
return true;
}
return false;
}
Here is the method from which I am calling:
boolean result = Utils.deleteSmsofContact(InboxActivity.this, sms.getNumber(), true);
if (result) {
dataList.remove(threadPosition);
iAdapter.notifyDataSetChanged();
Toast.makeText(InboxActivity.this,"Removed",Toast.LENGTH_LONG).show();
}else
{
Toast.makeText(InboxActivity.this,"cant removed",Toast.LENGTH_LONG).show();
}
Well I posted it but did not get the answer so finally I searched a lot on this and the correct answer is until or unless your app is not set a default you can't delete any sms or whole conversation.
Follow this link it will make your app set a default OR you will be able to delete.

How to get value of selected radioButton of buttonGroup

How to get value of selected radioButton?
I tried using buttonGroup1.getSelection().getActionCommand() (as posted in some of answers here) but it is not working.
Also, i am temporarily using this code but i want to know is this a good practice or not?
//Consider that maleRButton and femaleRButton are two radioButtons of
//same buttonGroup
String getGender()
{
if(maleRButton.isSelected())
{
return "Male";
}
else if(femaleRButton.isSelected())
{
return "Female";
}
else
{
return null;
}
}
I tried using buttonGroup1.getSelection().getActionCommand()
That approach will work, but for some reason it looks like you manually need to set the action command when you create the button. For example:
JRadioButton maleButton = new JRadioButton( "Male" );
maleButton.setActionCommand( maleButton.getText() );
This acutally seems like a bit of a bug to me since usually the action command defaults to the text if the action command is not set.
If you have several buttons you probably should do it this way :
String getSelectedButton()
{
for (Enumeration<AbstractButton> buttons = buttonGroup1.getElements(); buttons.hasMoreElements();) {
AbstractButton button = buttons.nextElement();
if (button.isSelected()) {
return button.getText();
}
}
return null;
}
String gender=group.getSelection().getActionCommand();
It will work but it show null value.
int selectedRadioButtonID = radio_group.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if (selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
answerList.add(selectedRadioButtonText);
} else {
Toast.makeText(this, "select radio button", Toast.LENGTH_SHORT).show();
return false;
}
For Deatils, check this

Error when login to mysql from java code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I want to login from mySql database.Everything is ok but when i login come my error message 2 times(Username or Password does not match.). I think my Loop is problem. Please help me. My method is below.....
private void loginProcess() {
String username = jTextFieldLoginUsername.getText().toUpperCase();
String password = jPasswordFieldLoginPassword.getText();
List<LoginModel> list = new ArrayList<LoginModel>();
LoginDao loginDao = new LoginDao();
list = loginDao.doLogin();
if (list.size() != 0) {
for (LoginModel model : list) {
String uName = model.getUsername();
String pWord = model.getPassword();
String userType = model.getUserType();
// System.out.println("uName::" + uName);
if (username.equals(uName) && password.equals(pWord)) {
if (userType.equals("1")) {
dispose();
Admin admin = new Admin();
admin.setVisible(true);
} else if (userType.equals("2")) {
dispose();
Purchase purchase = new Purchase();
purchase.setVisible(true);
} else if (userType.equals("3")) {
dispose();
Sales sales = new Sales();
sales.setVisible(true);
}
} else {
JOptionPane.showMessageDialog(rootPane, "Username or Password does not match.");
jTextFieldLoginUsername.requestFocus();
jTextFieldLoginUsername.selectAll();
}
}
} else {
}
}
String username = jTextFieldLoginUsername.getText().toUpperCase();
and
String uName = model.getUsername();
should be the same? Try adding .toUpperCase() to uName. Does that help?
Well you go through a list of possible logins, every time the credentials you got do not match, you print the message. So if you have three items in that list, two of them will cause the message to be printed. Even if you already found a matching user before.
I would recommend somewhat changing the logic around your for-loop:
boolean success = false
for (LoginModel model : list) {
...
if (username.equals(uName) && password.equals(pWord)) {
success = true;
...
} // no else here
}
if(!success) {
// do the error-message stuff
}

How to find the number of vote up or vote down for a youtube comment through API

I am fetching the comments for a video using Youtube's Java API. I want to know can I find the number of up votes or down votes for all the comment. If yes then how. Currently I am using the code given below. I am getting totalRating for each comment to find upvotes but every-time it outputs 0. I know this is wrong but how do I get the vote up and down for comments.Any pointers in the right direction will be appreciated. Thanks.
private void AddComments(YouTubeVideo ytv,VideoEntry videoEntry,YouTubeService service)
{
try
{
//Get Comments
String commentUrl = videoEntry.getComments().getFeedLink().getHref();
LinkedList<YouTubeComment> commentsLinkedList = new LinkedList<YouTubeComment>();
if(commentUrl!= null && commentUrl.length() > 0)
{
CommentFeed commentFeed = service.getFeed(new URL(commentUrl), CommentFeed.class);
if(commentFeed != null)
{
for(CommentEntry comment : commentFeed.getEntries())
{
YouTubeComment youtubeComment = new YouTubeComment();
if(comment.getTotalRating()!=null)
**//comment.getTotalRating() is always equal to 0.**
youtubeComment.setLike(comment.getTotalRating());
else
youtubeComment.setLike(0);
youtubeComment.setSpamStatus(comment.hasSpamHint());
String commentinVideo = comment.getPlainTextContent();
if(commentinVideo != null)
youtubeComment.setComment(comment.getPlainTextContent());
else
youtubeComment.setComment(" ");
commentsLinkedList.add(youtubeComment);
}
ytv.setComments(commentsLinkedList);
}
else
ytv.setComments(commentsLinkedList);
}
else
{
ytv.setComments(commentsLinkedList);
}
}
catch(Exception ex)
{ // This means that "Comments are disabled for this video."
LinkedList<YouTubeComment> comments = new LinkedList<YouTubeComment>();
ytv.setComments(comments);
System.out.println("Could not add comments for video := " + videoUrl);
System.out.println("This happens when comments are disabled for the video");
System.out.println("Exception in function AddComments : " + ex.toString());
}
}
Unfortunately, those values are not exposed via the API, and there are no plans to add them.

Why won't this method call work?

I'm creating a method to take an input by a user and validate it to make sure it's correct. If it's correct it will call a method and input the user input in to it. But for some reason, the method call is not working. It doesn't produce any errors, it just simply doesn't do it. I placed a print statement at the end of the code to make sure it actually reaches there and it does, but for some reason it's just not calling the method like it's supposed to. The other method works fine if I call it by itself and input a string via the parameters.
The code is:
public void getGetScheduledShowByFilmInput()////new - omar////
{
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
String filmInput;
filmInput = "";
boolean foundFilm;
foundFilm = false;
System.out.println("Here is a list of films that are currently showing:");
for(Film film : films){
System.out.println(film.getFilmName());
}
System.out.println("");
System.out.println("Please type the film name that you wish to view the corresponding shows for and press enter.");
System.out.println("Type 'exit' and press enter to exit this process.");
while(foundFilm == false){
try{
filmInput = reader.readLine();
}
catch (IOException e){
System.out.println("Error");
}
//If user enters "exit" then return.
if(filmInput.equals("exit")){
return;
}
//Check to see if the film name input by the user corresponds to any film showing.
for(Film film : films){
if(film.getFilmName() == filmInput){
foundFilm = true;
break;
}
}
if(foundFilm = true){
System.out.println("Film found.");
}
else{
System.out.println("The film name you entered has not been recognised. Please try again.");
}
}
//Call the function and input the film name input by the user.
getScheduledShowsByFilm(filmInput); ////This is the code that seems to be the problem.
System.out.println("reached bottom");
}
and the second method is:
public void getScheduledShowsByFilm(String inputFilmName)
{
ArrayList<Show> scheduledShows;
scheduledShows = new ArrayList<Show>();
for(Film film : films){
if(inputFilmName == film.getFilmName()){
for(Schedule schedule : schedules){
scheduledShows.add(schedule.getShowsOfFilm(film));
if(scheduledShows.get(scheduledShows.size() - 1) == null){
scheduledShows.remove(scheduledShows.size() - 1);
}
}
}
}
for(Show show : scheduledShows){
System.out.println("**********************************");
show.getShowDetails();
System.out.println("**********************************");
}
}
The second method works perfectly when I call it on its own and enter parameters manually though.
It's probably something extremely simple that I'm not understanding! haha, thank you for your help :)
foundFilm can never be false because you always assign true to it:
if(foundFilm = true){
System.out.println("Film found.");
}
try changing it to this:
if(foundFilm)
{
System.out.println("Film found.");
}
In getGetScheduledShowByFilmInput() and getScheduledShowsByFilm(String) avoid doing string comparison using the equality operator (==). The == operator tests for object equality, but you want to test whether two strings contain the same sequence of characters. Therefore, use equals instead:
//Check to see if the film name input by the user corresponds to any film showing.
for(Film film : films){
if(film.getFilmName().equals(filmInput)){
foundFilm = true;
break;
}
}
and
for(Film film : films){
if(inputFilmName.equals(film.getFilmName())){
for(Schedule schedule : schedules){
scheduledShows.add(schedule.getShowsOfFilm(film));
if(scheduledShows.get(scheduledShows.size() - 1) == null){
scheduledShows.remove(scheduledShows.size() - 1);
}
}
}
}

Categories

Resources