Results 1 to 2 of 2
  1. #1
    mcal is offline Member
    Join Date
    Jan 2008
    Posts
    39
    Rep Power
    0

    Default For Loop problem

    I have a problem with the for loop. I can't figure it out why, but the loop is not working, since the counter isn't incremented and its not doing anything that is found in the for loop. Do i have something wrong? Thanks for the help.
    I am using a binary file. I am making a quiz, and so i stored a set of questions on this binary file ( these questions i have wrote them in the java source code using DataOutputStream.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.util.Calendar;
    import java.awt.image.*;
    import javax.imageio.*;
     
     
     
    public class GQ extends JFrame implements ActionListener {
        private static final int FRAME_WIDTH = 1024;
        private static final int FRAME_HEIGHT = 768;
        private static final int FRAME_X_ORIGIN = 0;
        private static final int FRAME_Y_ORIGIN = 0;
        
        private JLabel prompt;
        private JPanel image;
        private JLabel response;
        private JMenu  fileMenu;
        private JMenu  HelpMenu;
        private JMenu  OptionMenu;
        
        AnswerStore answerStore = new AnswerStore();
        boolean timeForMore;
      
    	
      public GQ() {
            
          
            Container contentPane;  
            contentPane = getContentPane();
     
            JButton button1, button2, button3, button4, button5;
            
            setSize (FRAME_WIDTH, FRAME_HEIGHT);
            setTitle("Quiz");
            setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
            
         
     
           button2 = new JButton("Rivers");
            
            button4 = new JButton("Quit");
            
         
          
     
         
            contentPane.add(button2);
           
            contentPane.add(button4);
        
         
            button2.addActionListener(this);
            button2.setActionCommand("b2");
          
            button4.addActionListener(this);
            button4.setActionCommand("b4");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
            
     
            
        }
        
        public static void main (String[] args) {
           
            
                GQ frame = new GQ();
                frame.setVisible(true);
               
                new GQ();
                GQ File = new GQ();
                
          }
     
           
            String ac = event.getActionCommand();
            String[] questions = null;
            String[] answers = null;
           
             if(ac.equals("b2")) {
              
    		     
                questions = readFile("rivers.txt");
                answers = answerStore.riverAnswers;
            
           
            } else if (ac.equals("b4")) {
                System.exit(0);
               }
              
            
             askQuestions(questions, answers); 
           
            }
         public void stopAndShowResults() {
            
            timeForMore = false;  // used for countdown. 
        }
           
      private String[] readFile(String path) {
            StringBuilder sb = new StringBuilder();
            String separator = "\n";
            String question1, question2, question3;
         try{
     
          File aFile  = new File( "rivers.dat" );
          // create an output stream to the file
          FileInputStream aFileInStream = new FileInputStream ( aFile );
          // create a data output stream to the file output stream
          DataInputStream aDataInStream = new DataInputStream ( aFileInStream );
     
          // read data from file
             question1   = aDataInStream.readUTF();
             question2   = aDataInStream.readUTF();
             question3   = aDataInStream.readUTF();
            
             aFileInStream.close();
           
          
            JOptionPane.showInputDialog(null,question1);
           
           JOptionPane.showInputDialog(null,question2);
           
           JOptionPane.showInputDialog(null,question3);
             
          
        }
              catch( FileNotFoundException e )
          {
             System.out.println( e.getMessage() );
             System.exit(1);
          }    
          
          catch(IOException e)
          {
             System.out.println( e.getMessage() );
             System.exit(1);
          }
               
            return sb.toString().split("\\n");
        }
     
        public void askQuestions(String[] questions, String[] answers) {
          
            int count = 0;
            int point = 0;
            
            
           for(int j = 0; j < questions.length; j++) {   // this loop isn't working
                
               timeForMore = true;
               
              
              Random generator = new Random();
              int randomIndex = generator.nextInt(questions.length); 
              String input = JOptionPane.showInputDialog(null, questions[randomIndex]); // shows questions in a dialog box together with input line
            
              if(answers[randomIndex].equalsIgnoreCase(input))
               
               count++;  // incrementing counter if entered answer is correct
               point++;
              
              
          if(!timeForMore) 
                    break; 
         }
             
         JOptionPane.showMessageDialog(null, "You answered " + count +
                                          " out of " + questions.length +
                                          " questions correctly.");
                int percent = (count*100)/questions.length;
            JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + percent + " % ");
               
                 
     class AnswerStore  { 
       
        
        String[] riverAnswers = {
            "Gorges", "Meanders", "Levees", "Yes", "Less Economic Developed Countries",
            "crescent shaped lakes", "More Economic Developed Countries", "No", "River Discharge", "No", "",
            "", ""
        };
       
    }
     
    }
    Last edited by mcal; 01-19-2008 at 08:17 AM.

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Two things i noticed (i didn't read any of your other code, just the for loop).

    Java Code:
    if(answers[randomIndex].equalsIgnoreCase(input))
               
               count++;  // incrementing counter if entered answer is correct
               point++;
    point++ is never happening because you didnt put brackets for the if statement

    Java Code:
    if(!timeForMore) 
                    break;
    Since you made it so that the loop says:
    Java Code:
    timeForMore = true;
    The break statement will never be executed because the boolean timeForMore is always true in the iteration.

    This might not have solved your problem, but this may lead to the lack of logic errors later. =]

Similar Threads

  1. Comparing Images
    By shaungoater in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 11:38 AM
  2. Problem comparing three numbers
    By gammaman in forum New To Java
    Replies: 1
    Last Post: 02-09-2008, 06:43 PM
  3. Comparing null problem
    By thirdy_veritech in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 09:46 AM
  4. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 10:40 AM
  5. Comparing JavaWebFrameworks
    By pegitha in forum Web Frameworks
    Replies: 1
    Last Post: 05-18-2007, 07:23 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
  •