Domanda

Sto cercando di loop di un'eccezione, ma per qualche motivo non me la sua dando la possibilità di ri scrivere il mio file di scanner:

Non so come usare BufferedReader è per questo che sto usando questo. Eventuali indizi?

Ecco la mia classe standard con il mio metodo

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

E qui sta la mia classe principale con l'eccezione di essere implemeted con un ciclo:

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

Se qualcuno ha qualche idea su come ciclo in modo diverso Sono tutto orecchi.

È stato utile?

Soluzione 2

ho deciso anche sbarazzarsi della classe metodo e appena messo il metodo nella zona eccezione tentativo.

usato un .nextLine dopo scanner e sembra risolto.

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

Altri suggerimenti

Imposta errore false prima l'azione. In questo modo si ha la condizione di uscita corretta se l'utente ottiene il diritto.

error = false;
zack.findNumbers(); 

È ciclo while 'errore' variabile ha valore 'vero' . Tuttavia, diventa 'vero' solo quando la si butta (vale a dire quando 'catturare' blocco viene eseguito). Il flusso di controllo non ci arriva.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top