Results 1 to 5 of 5
  1. #1
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Can't find a problem.

    Hello, I need help, because I can't find where the problem is. I have simple GUI poll. Question, and four answers. I want to keep going with new questions, until user click wrong answer. I'm reading question and answers from the file.
    Here is main class:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    public class playmanija {
        
        
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		File f = new File("questions.txt");
    		Scanner s = new Scanner(f);
    		boolean still = true;
    		while(still){
    			antra a1 = new antra(s);
    			still = a1.still();
    		}
    		
    	}
    	
    
    }
    Here is another class:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import javax.swing.*;
    
    
    public class antra extends JFrame {
    	//Variables
    	private JPanel answers;
    	private JPanel questions;
    	private JButton b;
    	private JButton b2;
    	private JButton b3;
    	private JButton b4;
    	private JLabel question;
    	private Scanner s1;
    	private File f1;
    	private int correct;
    	private boolean still;
    	
    	
        public antra(Scanner s) {
        	//Adding title to our window
        	super(" žaidimas");
        	
        	
        	s1 = s;
        	
        	//Storing question and answers. Also correct number
        	String quest = getQuestion(s1);
        	String answer1 = getAnswer1(s1);
        	String answer2 = getAnswer1(s1);
        	String answer3 = getAnswer1(s1);
        	String answer4 = getAnswer1(s1);
        	 correct = getCorrect(s1);
        	
        	//Creating Panels and buttons
        	answers = new JPanel();
        	questions = new JPanel();
        	question = new JLabel(quest);
        	
        	button1 bt1 = new button1();
        	button2 bt2 = new button2();
        	button3 bt3 = new button3();
        	button4 bt4 = new button4();
        	
        	//Adding buttons and actionListeners for them
        	b = new JButton(answer1);
        	b.addActionListener(bt1);
        	 
        	 
        	b2 = new JButton(answer2);
        	b2.addActionListener(bt2);
        	
        	b3 = new JButton(answer3);
        	b3.addActionListener(bt3);
        	
        	b4 = new JButton(answer4);
        	b4.addActionListener(bt4);
        	
        	
        	//Adding everything to the screen
        	questions.add(question);
        	answers.add(b);
        	answers.add(b2);
        	answers.add(b3);
        	answers.add(b4);
        	
        	
        	
        	add(BorderLayout.NORTH, questions);
        	add(BorderLayout.CENTER, answers);
        	setSize(400,400);
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	setVisible(true);
        	
        	
        }
        //Getting all information
        public String getQuestion(Scanner s){
        	String question = "";
        	while(s.hasNext() && !question.contains("?")){
        		question += s.next()+" ";
        	}
        	
        	return question;
        }
       public String getAnswer1(Scanner s){
    	   String ans1 = "";
    	   ans1 = s.next();
    	   return ans1;
       }
       public String getAnswer2(Scanner s){
    	   String ans1 = "";
    	   ans1 = s.next();
    	   return ans1;
       }
       public String getAnswer3(Scanner s){
    	   String ans1 = "";
    	   ans1 = s.next();
    	   return ans1;
       }
       public String getAnswer4(Scanner s){
    	   String ans1 = "";
    	   ans1 = s.next();
    	   return ans1;
       }
       public int getCorrect(Scanner s){
    	   int correct = s.nextInt();
    	   return correct;
       }
       
       //ActionListener classes
       private class button1 extends JFrame implements ActionListener{
    		 public void actionPerformed(ActionEvent event){
    			 int button1 = 1;
    			 JLabel l;
    			 if(button1 == correct){
    				  l = new JLabel("Jūsų atsakymas teisingas!");
    				  still = true;
    				  still();
    			 }else{
    				  l = new JLabel("Deja, jūsų atsakymas neteisingas");
    				  still = false;
    			 }
    				 add(l);
    				 setSize(150,150);
    				 setVisible(true);
    				 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			 
    				 
    			 }
    		 }
       private class button2 extends JFrame implements ActionListener{
    		 public void actionPerformed(ActionEvent event){
    			 int button1 = 2;
    			 JLabel l;
    			 if(button1 == correct){
    				  l = new JLabel("Jūsų atsakymas teisingas!");
    				  still = true;
    				  still();
    			 }else{
    				  l = new JLabel("Deja, jūsų atsakymas neteisingas");
    				  still = false;
    			 }
    				 add(l);
    				 setSize(150,150);
    				 setVisible(true);
    				 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			 
    				 
    			 }
    		 }
       private class button3 extends JFrame implements ActionListener{
    		 public void actionPerformed(ActionEvent event){
    			 int button1 = 3;
    			 JLabel l;
    			 if(button1 == correct){
    				  l = new JLabel("Jūsų atsakymas teisingas!");
    				  still = true;
    				  still();
    			 }else{
    				  l = new JLabel("Deja, jūsų atsakymas neteisingas");
    				  still = false;
    			 }
    				 add(l);
    				 setSize(150,150);
    				 setVisible(true);
    				 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			 
    				 
    			 }
    		 }
       private class button4 extends JFrame implements ActionListener{
    		 public void actionPerformed(ActionEvent event){
    			 int button1 = 4;
    			 JLabel l;
    			 if(button1 == correct){
    				  l = new JLabel("Jūsų atsakymas teisingas!");
    				  still = true;
    				  still();
    			 }else{
    				  l = new JLabel("Deja, jūsų atsakymas neteisingas");
    				  still = false;
    			 }
    				 add(l);
    				 setSize(150,150);
    				 setVisible(true);
    				 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			 
    				 
    			 }
    		 }
       public boolean still(){
    	   return still;
       }
    
    
    }
    I added comments. For better understanding. My file is:
    Question - answer 1 answer 2 answer 3 answer 4 - correct answers number.

    Example:
    Whats your name ? Tom Jack John Nick 3 (3 means that John is correct answer).

    So, the problem is, that even if user clicks correct answer(I know that, because new JFrame window pop up with text, that their answer is correct) all program turns off. So, can you help me find where is the problem? Thank you.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Can't find a problem.

    If I understood your question, then answer's validation is here
    Java Code:
    public int getCorrect(Scanner s){
           int correct = s.nextInt();
           return correct;
       }
    Scanner gets number from a file.
    Then I added unique number for all buttons, in the inner class, there I compare them, and if both numbers are equal, return true.

  4. #4
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Can't find a problem.

    Quote Originally Posted by djokovic View Post
    If I understood your question, then answer's validation is here
    Java Code:
    public int getCorrect(Scanner s){
           int correct = s.nextInt();
           return correct;
       }
    Scanner gets number from a file.
    Then I added unique number for all buttons, in the inner class, there I compare them, and if both numbers are equal, return true.
    While the user is taking the quiz, where is the code that checks if the user has entered the correct answer? The code provided is defining the correct answer while creating the quiz.
    Last edited by brynpttrsn; 05-06-2012 at 11:59 PM.

  5. #5
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Can't find a problem.

    It checks here:
    Java Code:
    private class button1 extends JFrame implements ActionListener{
    		 public void actionPerformed(ActionEvent event){
    			 int button1 = 1;
    			 JLabel l;
    			 if(button1 == correct){
    				  l = new JLabel("Correct");
    				  still = true;
    				  still();
    			 }else{
    				  l = new JLabel("Wrong");
    				  still = false;
    			 }
    				 add(l);
    				 setSize(150,150);
    				 setVisible(true);
    				 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			 
    				 
    			 }
    		 }

Similar Threads

  1. Replies: 1
    Last Post: 04-14-2011, 01:48 PM
  2. Replies: 3
    Last Post: 04-12-2011, 06:44 AM
  3. Replies: 3
    Last Post: 09-26-2010, 06:29 AM
  4. Cannot find symbol problem
    By Andyj in forum New To Java
    Replies: 2
    Last Post: 04-11-2010, 03:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •