Hallo zusammen
Ich habe ein try-Block, in dem der User per Scanner eine Int-Variable eingibt. Dazu habe ich einen catch-Block, um InputMismatchException abzufangen (alles, was nicht int ist). So weit, so gut.
Nun will ich aber bei bestimmten, eigentlich falschen, User-Eingaben, eine If-Schleife einbauen. Konkret; falls der User "quit" eingibt, soll abgebrochen werden.
Dazu 2 Fragen:
Danke für eure Tipps.
Gruss aus dem regnerischen Bern,
Markus
Ich habe ein try-Block, in dem der User per Scanner eine Int-Variable eingibt. Dazu habe ich einen catch-Block, um InputMismatchException abzufangen (alles, was nicht int ist). So weit, so gut.
Nun will ich aber bei bestimmten, eigentlich falschen, User-Eingaben, eine If-Schleife einbauen. Konkret; falls der User "quit" eingibt, soll abgebrochen werden.
Dazu 2 Fragen:
- Ist es sinnvoller, diese IF-Schleife direkt nach der User-Eingabe einzubauen oder besser erst im catch-Block?
- Ich habe eine funktionierende Lösung: Anstatt int-Variablen werden String-Variablen abgefragt. Die werden anschliessend in Ints umgewandelt. Das ist mir zu unelegant. Gibt es bessere Lösungen, mit denen ich int-Variablen behalten und trotzdem zusätzlich "quit" abfangen kann?
Danke für eure Tipps.
Gruss aus dem regnerischen Bern,
Markus
Java:
import java.util.*;
public class kapitel80a1 {
public static void main(String[] args) {
Scanner scan = new Scanner( System.in );
String num="", div="" ;
int numInt=0, divInt=0;
boolean allesOk=false;
while (allesOk==false){
try{
System.out.print("Enter the numerator: ");
num = scan.next();
if (num.charAt(0)=='q'||num.charAt(1)=='Q'){
System.out.println("na dann tschuess");
System.exit(0);
}
numInt=Integer.parseInt (num);
System.out.print("Enter the divisor : ");
div = scan.next();
divInt=Integer.parseInt(div);
System.out.println( numInt + " / " + divInt + " is " + (numInt/divInt) + " rem " + (numInt%divInt) );
allesOk=true;
} catch (ArithmeticException ex ){
System.out.println("You can't divide " + num + " by " + div);
} catch (InputMismatchException ie) {
System.out.println("Hoppla, da ist wohl ein Buchstabe oder so reingerutscht.");
System.out.println("Probiers nomau");
} catch (NumberFormatException ne){
System.out.println("Hoppla, da ist wohl ein Buchstabe oder so reingerutscht.");
System.out.println("Probiers nomau");
} catch (StringIndexOutOfBoundsException se){
System.out.println("check mal deine Eingabe fuer den teiler");
} finally {
System.out.println("If something went wrong, you entered bad data." );
}
System.out.println("------------------------------------------------------");
}
System.out.println("I han fertig!");
}
/*
//Original-Code:
import java.util.* ;
public class SquareUser
{
public static void main ( String[] a )
{
Scanner scan = new Scanner( System.in );
int num = 0 ;
boolean goodData = false;
while ( !goodData )
{
System.out.print("Enter an integer: ");
try
{
num = scan.nextInt();
goodData = true;
}
catch (InputMismatchException ex )
{
System.out.println("You entered bad data." );
System.out.println("Please try again.\n" );
String flush = scan.next();
}
}
System.out.println("The square of " + num + " is " + num*num );
}
Enhance the DivisionPractice program from the chapter:
Put in a loop so that the user is repeatedly asked for the numerator and the divisor.
For each set of data, the program prints out the result,
or an informative error message if there is a problem (division by zero or poor input data).
The program continues looping, even if there is a problem
Exit the loop when data entered for the numerator start with characters "q" or "Q".
Don't print out an error message in this case.
Don't ask for the divisor if the user just asked to quit.
Here is sample output from one run:
Enter the numerator: 12
Enter the divisor: 4
12 / 4 is 3
Enter the numerator: 12
Enter the divisor : 0
You can't divide 12 by 0
Enter the numerator: glarch
You entered bad data.
Please try again.
Enter the numerator: quit
You will need to use the method charAt() from the String class.
Click here to go back to the main menu.
*/
}