开发者

Looping Forever

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.


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

error = false;
zack.findNumbers(); 


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;
}
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜