Welcome to Dream.In.Code
Become a Java Expert!

Join 149,486 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,329 people online right now. Registration is fast and FREE... Join Now!




Looping error

 
Reply to this topicStart new topic

Looping error

dbrine
25 Apr, 2007 - 05:27 PM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
Good Day. I am new this forum and to Java as well. I must admit I know absolutely nothing about Java. I have a problem that I am asking for help on. I have a multiplication problem whereas the user choice a mulitple (ex 2, and the pc ask 2x0=?, 2x1=?, etc) The program works unless I try to loop it. I am trying to have the program loop if the user enters a wrong format, the program will automatically start again asking questions. I thought (think) the loop is correct. The loop is using While(!done) and done = true. inside of try/catch statement. Any help would be appreciated. I looked at the javadocs and everywhere else I could. here is the code I have. I get done = true "Unreachable"???
CODE

import java.io.*;

public class Multiply
{
  public static void main (String[] args ) throws IOException
  {
    //Declaring variables
    int multiplier;
    int correct;
    boolean done = false;

    //Opening messages
    System.out.println("\t\tWelcome to the Multiplication Quiz");
    System.out.println("");

    //loop while not done
    while(!done)
    {

        try
        {
            //Calling the user-defined methods
            multiplier = getNumber();
            correct = takeQuiz(multiplier);
            System.out.println("\t\tYou got "+correct+ " correct!" );
            throw new NumberFormatException();
            done = true;
        }
        catch(NumberFormatException e)
        {
            System.out.println("Your entry was not in the proper format");
        }
    }
  }

   public static int getNumber() throws IOException
   {
      //Declaring variables
      BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
      String inputData;
      int multiplier;

      //Get a value from user
      System.out.println("Enter the multiplication table you wish to practice:");
         inputData = dataIn.readLine();
         multiplier = Integer.parseInt( inputData );

      //Return a value to main
      return multiplier;

   }

   public static int takeQuiz(int multiplier) throws IOException
   {
     //Delcaring variablaes
      BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
      String inputData;
      int answer;
      int count = 0;
      int correct = 0;

      while ( count <= 12)
      {

         //Display question and get answer
         System.out.println( "What is "+count + " times " + multiplier +"?" );
            inputData = dataIn.readLine();
            answer     = Integer.parseInt( inputData );

         if (answer == count * multiplier)
         {
            System.out.println("\tCorrect!");
        correct = correct + 1;
         }

         else
         {
           System.out.println("\tIncorrect");
         }

         count = count + 1;

      }

      return correct;
   }
}


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Looping Error
25 Apr, 2007 - 06:54 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
The statement is unreachable because if an error is thrown it will never execute.

You can fix this by moving the done = true; before the throw NumberFormatException.

To keep it looping include another statement reassigning the value back to false inside the catch, if there should be an error.
CODE

//loop while not done
    while(!done)
    {

        try
        {
            //Calling the user-defined methods
            multiplier = getNumber();
            correct = takeQuiz(multiplier);
            System.out.println("\t\tYou got "+correct+ " correct!" );
            done = true;
            throw new NumberFormatException();
            
            
        }
        catch(NumberFormatException e)
        {
            System.out.println("Your entry was not in the proper format");
            done = false;
        }        
    }

User is offlineProfile CardPM
+Quote Post

dbrine
RE: Looping Error
25 Apr, 2007 - 07:15 PM
Post #3

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
thanks for the quick reply. I was looking at another example that doesn't include the NumberFormatException in the try statement and it works.? Why?


QUOTE(jayman9 @ 25 Apr, 2007 - 07:54 PM) *

The statement is unreachable because if an error is thrown it will never execute.

You can fix this by moving the done = true; before the throw NumberFormatException.

To keep it looping include another statement reassigning the value back to false inside the catch, if there should be an error.
CODE

//loop while not done
    while(!done)
    {

        try
        {
            //Calling the user-defined methods
            multiplier = getNumber();
            correct = takeQuiz(multiplier);
            System.out.println("\t\tYou got "+correct+ " correct!" );
            done = true;
            throw new NumberFormatException();
            
            
        }
        catch(NumberFormatException e)
        {
            System.out.println("Your entry was not in the proper format");
            done = false;
        }        
    }



User is offlineProfile CardPM
+Quote Post

Jayman
RE: Looping Error
25 Apr, 2007 - 07:49 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Go back and look at the post I made in that topic.

As a suggestion, in the future you should try writing your own code. Instead of taking somebody else's work. You will learn more in the long run.
User is offlineProfile CardPM
+Quote Post

dbrine
RE: Looping Error
26 Apr, 2007 - 05:21 AM
Post #5

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
First, I never copied anyones code. The code comes preformated from the class we are taking. SO of course the codes will look alike.


QUOTE(jayman9 @ 25 Apr, 2007 - 08:49 PM) *

Go back and look at the post I made in that topic.

As a suggestion, in the future you should try writing your own code. Instead of taking somebody else's work. You will learn more in the long run.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 04:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month