Java:如果输入与RegEx定义的频谱匹配,我该如何检查?字符串检查适用于我,但我想检查它的号码是否

时间:2015-12-04 13:03:17

标签: java

在我结束之前,我意外地发布了。这里有一个基本的描述:我正在研究一个简单的计算器(学徒IT),并且陷入了以下问题:我可以用另一种方式(正则表达式相反的匹配)实现我的目标,但是我可以使用它。我想明白为什么会出现这个问题。

(我想我现在知道问题是什么。即使我输入一封信,扫描仪也会尝试阅读下一个DOUBLE。如果我不能与正则表达式进行比较,如果在比较之前已经出现错误:))

这有效:

    OP = read.next();
    if (OP.matches(isOperand) == false){
        happend = true;
        fehlerMsg();
        askForOp();
    }

但这不起作用:

   result = read.nextDouble(); 
    String stResult;
    stResult = String.valueOf(result);
    if (!stResult.matches(isNumber)){
        fehlerMsg();
    }

如果有人可以给我一个线索,那么为什么这不适用于双重正则表达式检查。当我尝试编译时,我只是得到一个没有具体的说法错误。抱歉我的英语不好,我瑞士。 这里是完整的代码(OP是一个String,结果是double,stResult是一个字符串,isOperand / isNumber是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);
}

}

1 个答案:

答案 0 :(得分:0)

如果下一个标记不是double,则

nextDouble()将抛出异常:

InputMismatchException - if the next token does not match the Double regular expression, or is out of range

您可以先致电hasNextDouble(),以确保接下来会有双倍值。

或者,您可以将该标记读取为String(方法next()),然后应用您的正则表达式来检查它是否为双精度。