coding compiling but no display


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: coding compiling but no display

  1. #1
    Join Date
    Jan 2008
    Posts
    29

    coding compiling but no display

    Hi all,

    i am doing a program that contains an array of 10 multiple choice questions. Each question contains 3 answers. Also I have to create an array that holds the correct answer to each question (A, B or C). When the question is displayed, I verify that the user enters only A, B, or C otherwise do nothing. At the end of the 10 questions I have to display the numbers of correct and incorrect answers. I did it simple without importing the javax.swing.OPtionPane because i am not sure how to work with this and arrays.

    I did some of the coding but I have no exit. the program compiled ok but when I run it, it looks like it works but I don't see anything on the screen.
    Could you please help me to solve this issue

    thanks.

    Code:
    import java.io.*;
    
    public class Quiz
    {
              String question;
              int correctCounter =0;    
       
    public static void main(String args[])
           {
           }
                   
          public Quiz() throws IOException
          {
           
            BufferedReader keyboard = new
            BufferedReader(new InputStreamReader(System.in));
            String seekResponse;
            String seekAnswer = null;
                           
            System.out.println("This is a 10 quiz program."
                                                  + "\nLet's begin");
                    
             String[ ] questionaire = new String[10];         
      
                 questionaire[0] = "Which country is known as the roof of the world?"
                                        + "\na)Switzerland b)Argentina c)India";
    
                System.out.println(questionaire[0]);
                   
                seekAnswer = keyboard.readLine();
                
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is a=Switzerland.");
                        }
                       
                questionaire[1] = "Which is the largest island in the world?"
                                       + "\na)Srilanka b)Australia c)Greenland";
                              
                System.out.println(questionaire[1]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("c"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is c=Greenland.");
    	   }
    
               questionaire[2] = "What is the study of curves in three-dimensional 
                                       space, such as spheres or cones, called?"
                                        + "\na)Solid Geometry b)Space Geometry 
                                             c)Abstract Geometry";
                              
                System.out.println(questionaire[2]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is a=Solid Geometry.");
    	   }
    
                 questionaire[3] = "Charles Darwin began his voyage on HMS Beagle 
                                        from what port?"
                                       + "\na)Devonport, England b)Dover, France c)New 
                                               York"; 
                                
                System.out.println(questionaire[3]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is a=Devonport.");
    	   }
    
                  questionaire[4] = "The city of Manta is located in:?" 
                                          + "\na)Egypt b)France c)Ecuador";
                              
                System.out.println(questionaire[4]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("c"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The  correct answer is c=Ecuador.");
    	   }
    
    
                  questionaire[5] = "The longest highway in the world is?" 
                                        + "\na)Trans-Canada b)Inter-State 90 USA c)E30 
                                                   Route Europe";
                              
                System.out.println(questionaire[5]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is a=trans-Canada.");
    	   }
    
    
                  questionaire[6] = "Which is the shallowest sea in the world?"
                                        + "\na)Caspian Sean b)Baltic Sea c)Sea of Azov";
                              
                System.out.println(questionaire[6]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("c "))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is c=Sea of Azov.");
    	   }
    
    
                 questionaire[7] = "Which country is known as the lady of snow?" 
                                        + "\na)Greenland b)Canada c)Pakistan";
                              
                System.out.println(questionaire[7]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("b "))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is b=Canada.");
    	   }
    
    
                    questionaire[8] = "How many feathers are used to make 
                                             badminton shuttle?"
                                           + "\na)10 to 12 b)18 to 20 c)14 to 16";
                              
                System.out.println(questionaire[ 8]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("c "))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The  correct answer is  c=14 to 16.");
    	   }
    
    
    
                 questionaire[9] = "Who developed the World Wide Web {www}?"
                                  + "\na)Tim Bernes Lee b)Charles Babbage c)Jim               
                                             Osborne";
                              
                System.out.println(questionaire[9]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is a=Tim Bernes Lee.");
    	   }
    
        }
                       
    }

  2. #2
    Join Date
    Jul 2005
    Posts
    78
    You don't have anything in your main() method!


    For what you've written, you need to put something along the lines of :

    Quiz q = new Quiz();

    in main.
    Last edited by masher; 03-23-2008 at 03:40 AM.

  3. #3
    Join Date
    Jan 2008
    Posts
    29

    coding compiling but no display

    Thanks masher for your answer.

    I followed your suggestion but when I compile the javac is giving me 1 error.

    "unreported exception java.io.IOexception; must be caughted to be thrown"
    Quiz q = new Quiz();
    ^

    I am not sure if I didn't understand or is something that I don't know (probably being that this is my 4th. program). Could you please clarify and help me with this step.
    Thanks

  4. #4
    Join Date
    Jul 2005
    Posts
    78
    oh yeah...

    Your Quiz has this on it "throws IOException".

    I'm not all that up with it and exceptions, but that would probably come from the BufferedReader you're using. Basically, it means that if the buffered reader gets an input that it doesn't understand, then it will throw an Exception.

    You need to either put your "Quiz =.." line in a try/catch block to handle the exception (good thing) or just add "throws IOException" to the end of your main declaration (bad thing).

    Alternatively, you can re-write your Quiz method to handle the error.

  5. #5
    Join Date
    Jan 2008
    Posts
    29

    coding compiling but no display

    Thank you masher for your suggestion. I tried putting the throws IOexception but no luck.
    Could you guide me with that. I am in the process of frustration with this program after trying for two weeks and no results.

    Thanks

  6. #6
    Join Date
    Jul 2005
    Posts
    78
    Code:
    public static void main(String args[]) throws IOException
           {
                  Quiz q = new Quiz();
           }

  7. #7
    Join Date
    Jan 2008
    Posts
    29

    coding compiling but no display

    Thanks masher.

    the code is running perfect. Now I am trying to do a counter where I count the number of good answer and the bad answer which I display at the end of the 10 questions but I do not find how to compare it. You can take a look at it (is in red color)



    Code:
    import java.io.*;
    
    public class Quiz
    {
              String questionaire;
              int correctCounter =0;    
              int incorrectCounter=0; 
    
    public static void main(String [ ] args) throws IOException
           {
                  Quiz q = new Quiz();
           }
                          
         public Quiz() throws IOException
         {
    
            BufferedReader keyboard = new
            BufferedReader(new InputStreamReader(System.in));
            String seekResponse;
            String seekAnswer = null;
                           
            System.out.println("This is a 10 quiz program."
                                                  + "\nLet's begin");
                    
             String[ ] questionaire = new String[10];         
      
                 questionaire[0] = "1. Which country is known as the roof of the 
                                             world?"
                                                    + "\na)Switzerland b)Argentina c)India";
    
                System.out.println(questionaire[0]);
                   
                seekAnswer = keyboard.readLine();
                
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is a=Switzerland.\n");
                        }
                       
                questionaire[1] = "2. Which is the largest island in the world?"
                                            + "\na)Srilanka b)Australia c)Greenland";
                              
                System.out.println(questionaire[1]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("c"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is c=Greenland.\n");
    					}
    
               questionaire[2] = "3. What is the study of curves in three-dimensional 
                                        space, such as spheres or cones, called?"
                                                        + "\na)Solid Geometry b)Space 
                                                         Geometry c)Abstract Geometry";
                              
                System.out.println(questionaire[2]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is a=Solid 
                                                  Geometry.\n");
    					}
    
                 questionaire[3] = "4. Charles Darwin began his voyage on HMS 
                                            Beagle from what port?"
                                                     + "\na)Devonport, England b)Dover, 
                                                              France c)New York"; 
                                
                System.out.println(questionaire[3]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is a=Devonport.\n");
    					}
    
                  questionaire[4] = "5. The city of Manta is located in?" 
                                           + "\na)Egypt b)France c)Ecuador";
                              
                System.out.println(questionaire[4]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("c"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The  correct answer is c=Ecuador.\n");
    					}
    
    
                  questionaire[5] = "6. The longest highway in the world is?" 
                          + "\na)Trans-Canada b)Inter-State 90 USA c)E30 Route 
                                  Europe";
                              
                System.out.println(questionaire[5]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is
                                                   a=trans-Canada.\n");
    		    }
    
    
                  questionaire[6] = "7. Which is the shallowest sea in the world?"
                                        + "\na)Caspian Sean b)Baltic Sea c)Sea of Azov";
                              
                System.out.println(questionaire[6]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("c"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is c=Sea of Azov.\n");
    					}
    
    
                 questionaire[7] = "8. Which country is known as the lady of snow?" 
                                        + "\na)Greenland b)Canada c)Pakistan";
                              
                System.out.println(questionaire[7]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("b"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is b=Canada.\n");
    					}
    
    
                    questionaire[8] = "9. How many feathers are used to make 
                                                badminton shuttle?"
                                           + "\na)10 to 12 b)18 to 20 c)14 to 16";
                              
                System.out.println(questionaire[8]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("c"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is  c=14 to 16.\n");
    					}
    
    
    
                 questionaire[9] = "10. Who developed the World Wide Web {www}?"
                                  + "\na)Tim Bernes Lee b)Charles Babbage c)Jim 
                                            Osborne";
                              
                System.out.println(questionaire[9]);
                        
                seekAnswer = keyboard.readLine();
                        
                    if( seekAnswer.equals("a"))
                    {
                    System.out.println("\n Correct!\n");
                    }
                        else
                        {
                        System.out.println("The correct answer is a=Tim Bernes 
                                                   Lee.\n");
                         }
    
    			if (questionaire[9] == correct)
                    {   
                    correctCounter = correctCounter + 1;
                    }
                    else
                    { 
                    incorrectCounter = incorrectCounter + 1;
                    }
                           
    
                    System.out.println("The number of correct answers is:" 
                                      + correctCounter +"\nThe number of incorrect
                                         answers is:" 
                                      +  incorrectCounter);
        
    
    
        }
                      
    }
    Just for curiosity, I was playing with the program and I enter different letters than A, B, C and the program just gives me the right answer, but if I want that the program recognizes only A, B, or C how should I limit that situation so the program keeps prompting the user until a valid response is entered.

    Thanks again.

  8. #8
    Join Date
    Feb 2008
    Posts
    3
    You could say:

    if(input == "a" || input == "b" || input =="c")
    {
    keep going
    }
    else
    {
    try again
    }

  9. #9
    Join Date
    Jan 2008
    Posts
    29

    coding compiling but no display

    Thanks thebomb551,

    Excellent idea. I will work on it and if need it be back.

  10. #10
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Remember "A, "B", and "C", too; also you can't compare strings for equality using Boolean operators - need to use equals("a"), etc.
    Last edited by nspils; 03-30-2008 at 06:10 PM.

  11. #11
    Join Date
    Jan 2008
    Posts
    29

    coding compiling but no display

    Thanks nspils. You are right, I learned in a hard way. now the program is running perfect. I will keep that in mind when I use strings.

  12. #12
    Join Date
    Jan 2008
    Posts
    29

    coding compiling but no display

    Thanks. Program running perfectly.

Similar Threads

  1. i need coding for this question...help me
    By usher_nesh in forum C++
    Replies: 8
    Last Post: 08-20-2007, 11:32 AM
  2. Replies: 1
    Last Post: 12-07-2005, 09:44 AM
  3. MSFlexGrid display issue.
    By BOOGIEMAN in forum VB Classic
    Replies: 2
    Last Post: 09-28-2005, 03:35 PM
  4. curved bargraph type display
    By thebluearcher2 in forum VB Classic
    Replies: 0
    Last Post: 03-03-2005, 12:38 PM
  5. Display XML element attributes in CSS or XSL
    By Allen Pitts in forum XML
    Replies: 0
    Last Post: 10-31-2002, 04:02 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center