Accidentally i postet before i was finish. Here a basic description: I'm working on a simple calculater (apprenticeship IT), and am stucked in following problem: Shurly i could get to my goal in an other way (regex-opposite match), but i'd like to understand, why this problem exists.
(I think i now reckon what's the problem. Even if i type in a letter, the scanner is trying to read the next DOUBLE. Afcourse it won't be able to compare with regex, if the error already appears before it get's to comparing :))
This works:
OP = read.next();
if (OP.matches(isOperand) == false){
happend = true;
fehlerMsg();
askForOp();
}
But this doesn't work:
result = read.nextDouble();
String stResult;
stResult = String.valueOf(result);
if (!stResult.matches(isNumber)){
fehlerMsg();
}
Would be super, if some one could give me a clue, why this doesn't work with the double Regex check. I just get a nothing specific saying error when i try to compile. Sorry for my bad english, i'm swiss. Here the full Code (OP is a String, result a double, stResult a string and isOperand/isNumber are RegEx:
public class TheUltimativeBestCalculatorOnThisWorldIswear {
public static boolean happend = false;
public static String isOperand = "[\\+\\-\\*\\/\\^]";
public static String isNumber = "[\\d]";
public static Scanner read = new Scanner(System.in);
public static String answer;
static String sTresult;
static boolean weiter = true;
static double zahlX;
static double zahlY;
static double result;
static boolean fehler = true;
static String OP;
/**
* @param args the command line arguments
*/
//Start the Stuff
public static void main(String[] args) {
first();
LetTheShowBegin();
}
//First thing that happens
public static void first(){
write("Zahl1?");
result = read.nextDouble();
String stResult;
stResult = String.valueOf(result);
if (!stResult.matches(isNumber)){
fehlerMsg();
}
}
//Second thing that happens
public static void second(){
write("Zahl2?");
zahlY = read.nextInt();
}
//Thing that happens on n' on
public static void LetTheShowBegin(){
while (weiter){
askForOp();
second();
calc();
showTheStuff();
}
write("Tschüss");
}
public static void showTheStuff(){
sTresult = String.valueOf(result);
write(sTresult);
write("Weiter? (j oder n)");
answer = read.next();
if ("j".equals(answer)){
weiter = true;
}
else if ("n".equals(answer)){
weiter = false;
}
}
public static void askForOp(){
if (happend == false){
write("Welchen Operator möchten Sie benutzen?");
}
else if (happend == true){
write("Nochmal: Welchen OPERATOR möchten Sie benutzen? (+, -, *, /, ^)");
}
OP = read.next();
if (OP.matches(isOperand) == false){
happend = true;
fehlerMsg();
askForOp();
}
}
public static void fehlerMsg(){
write("Versuche es noch mal, ich bin ein Waal, habe keine Wahl, mit meinem Schaal....");
}
public static void calc(){
if (null != OP)switch (OP) {
case "+":
doPlus();
break;
case "-":
doMinus();
break;
case "*":
doTimes();
break;
case "/":
doDurch();
break;
case "^":
doPow();
break;
}
}
public static void doPlus(){
result = result + zahlY;
}
public static void doMinus(){
result = result - zahlY;
}
public static void doTimes(){
result = result * zahlY;
}
public static void doDurch(){
result = result / zahlY;
}
public static void doPow(){
result = Math.pow(result, zahlY);
}
public static void fehler(){
}
public static void write(String x){
System.out.println(x);
}
}
nextDouble() will throw an Exception if the next token is not a double :
InputMismatchException - if the next token does not match the Double regular expression, or is out of range
You may first call hasNextDouble()
to ensure that there is a double value coming next.
Or, you may read the token as a String (method next()) , then apply your regex to check if it is a double.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.