簡體   English   中英

Java:如何檢查輸入是否與RegEx定義的頻譜匹配? 字符串檢查對我有用,但我想檢查其編號是否

[英]Java: How can i check, if the Input matches a RegEx defined spectrum? String check works for me, but i want to check if its number or not

我偶然在完成之前寄了信。 這里是一個基本描述:我正在研究一個簡單的計算器(學徒IT),並且陷入以下問題:不幸的是,我可以通過另一種方式實現我的目標(與正則表達式相反的比賽),但是我想了解,為什么存在此問題。

(我想我現在認為出了什么問題。即使我輸入字母,掃描儀也會嘗試讀取下一個DOUBLE。因此,如果錯誤已經出現在比較之前,它將無法與regex進行比較。 :))

這有效:

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

如果有人可以給我一個線索,那將是超級好,為什么這不適用於Regex雙重檢查。 當我嘗試編譯時,我只是得到了一個沒有具體說的錯誤。 對不起,我的英語不好,我是瑞士人。 這里的完整代碼(OP是一個字符串,結果是一個雙精度,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);
}

}

如果下一個標記不是double,則nextDouble()將引發Exception:

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

您可以先調用hasNextDouble()以確保接下來有一個雙hasNextDouble()值。

或者,您可以將令牌讀取為String(方法next()),然后應用正則表達式檢查它是否為double。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM