views:

92

answers:

3

I'm trying to loop an exception, but for some reason its not giving me the option to re write my scanner file:

I don't know how to use BufferedReader so that's why I'm using this. Any clues?

Here's my standard class with my method

package arrayExceptionsWithInput;
import java.util.*;
public class GetThoseNumbersBaby {

    int firstInt;
    int secondInt;
    boolean error;
    Scanner yourNumbers = new Scanner(System.in);

    public void findNumbers()
    {
        System.out.println(" please enter your first number");
        firstInt = yourNumbers.nextInt();
        System.out.println(" pleas enter your second number");
        secondInt = yourNumbers.nextInt();
        int finalInt = (firstInt+secondInt);
        System.out.println("total is: " + finalInt);
    }
}

And here's my main class with the exception being implemeted with a loop:

package arrayExceptionsWithInput;
import java.util.*;

public class InputException
{
    public static void main(String[] args)
    {
        boolean error = false;
        GetThoseNumbersBaby zack = new  GetThoseNumbersBaby();


        {
            do {
                try
                {
                    zack.findNumbers();
                }
                catch(InputMismatchException Q)
                {
                    System.out.println(" one of your integers was incorrect, please try again");
                    Q.printStackTrace();
                    error = true;
                } 
            } while (error == true);
        }
        error = false;
    }
}

If anyone has any ideas on how to loop this differently I'm all ears.

+1  A: 

Set error false before the action. That way you have the correct exit condition if the user gets it right.

error = false;
zack.findNumbers();
Ewan Todd
What's the reason for the downvote here?
bruno conde
sorry, still getting used too stacks interface. i didnt know i had enough rep to vote on posts
OVERTONE
A: 

i decided too get rid of the method class and just put the method into the try exception area.

used a .nextLine after scanner and it seems fixed.

this looks ok?

package arrayExceptionsWithInput;
import java.util.*;
public class InputException
{
public static void main(String[] args)
{
boolean error = false;
int firstInt;
int secondInt;
Scanner yourNumbers = new Scanner(System.in);
{
 do{
   try
   {

     System.out.println(" please enter your first number");
     firstInt = yourNumbers.nextInt();
     yourNumbers.nextLine();
     System.out.println(" pleas enter your second number");
     secondInt = yourNumbers.nextInt();
     yourNumbers.nextLine();
     int finalInt = (firstInt+secondInt);
     System.out.println("total is: " + finalInt);
     yourNumbers.nextLine();

   }
   catch(InputMismatchException Q)
   {

    Q.printStackTrace();
    System.out.println(" one of your integers was incorrect, please try again");
    error = true;
    yourNumbers.nextLine();
   } 
  }while (error == true);
 }error = false;
}
}
OVERTONE
Scanner keeps the answer and doesnt rests unless i put in yourNumnbers.NextLine()
OVERTONE
It's more usefull if you edit your question with updates instead of adding updates by way of answers.
rsp
You still need to set error = false inside the loop, otherwise if you do hit an exception, error will be set to true and never cleared.
martin clayton
wait, i see my mistake and what you mean but why when i set it too false inside the loop, then i realisd im setting it inside and just after the loop too the same thing. sorry for not updating, but if i wrote the answer in my original question, it wouldnt be a question. and would get closed by power hungry mods.
OVERTONE
A: 

You loop while 'error' variable has value 'true'. However, it becomes 'true' only when the is thrown (i.e. when 'catch' block is executed). Control flow doesn't reach it.

denis.zhdanov