循环遍历字符串以检查值。 (Java计算器)

时间:2013-12-16 10:37:29

标签: java arrays string loops

我正在尝试编写一个方法来查看sting,并将字符串中的任何数字添加到calcOperand数组,将任何运算符添加到calcOperator数组。这是我的Javat计算器。

以下是我坚持使用的代码。您可以看到我已尝试创建将String拆分为数组并循环遍历的方法。然后我想检查String以查看它是运算符还是操作数。这样做的最佳方式是什么?

import java.util.*;
public class stringCalculator {

private String userinput;
private int[] calcOperator;
private char[] calcOperand;

public stringCalculator(String userinput){
    this.userinput = userinput;
    //System.out.println(userinput);
}   

//This function will check the input and return true if the user enters a correct expression.
public boolean checkInput(){
    boolean show = userinput.matches("[-+/*0-9]+");
    return show;
}

//This function will add any numbers in the string to the calcOperand array.
//and any operators to the calcOperator field.
public void parseInput(){
    String[] theinput = userinput.split("");

    for (int i = 0 ; i < theinput.length ; i++){
        if(theinput.charAt(i) == "+" ){
            calcOperator.add("+");
        }
    }
}
}

4 个答案:

答案 0 :(得分:1)

将操作数存储在checkInput()方法的calcOperator数组中的calcOperand数组运算符中,如下所示:

public stringCalculator(String userinput) {
    this.userinput = userinput;
    checkInput();
}

// This function will check the input and return true if the user enters a
// correct expression.
public boolean checkInput() {
    String[] operators = userinput.split("[-+/*]"); 
    calcOperator = new int[operators.length];
    for (int i = 0; i < calcOperator.length; i++) {
        calcOperator[i] = Integer.parseInt(operators[i]);
    }
    String[] operands = userinput.split("[0-9]");
    calcOperand = new char[operands.length];
    for (int i = 0; i < operands.length; i++) {
        if (operands[i].length() > 0) {
            calcOperand[i] = operands[i].charAt(0);
        }
    }
    return true;
}

答案 1 :(得分:0)

您正在使用" "检查字符。我们对String使用" "。试试这个

if(theinput.charAt(i) == '+' )

calcOperator也是一个数组。数组没有add方法。我认为你应该使用ArrayList

答案 2 :(得分:0)

输入是一个String数组,你不能在字符串数组上调用charAt()方法。这是你的代码的问题。而是尝试下面的代码,其中数组的字符串分别与字符串“+”

进行比较
String[] theinput = userinput.split("");
for (int i = 0 ; i < theinput.length ; i++)
    {
        if(theinput[i].equals("+"))
        {
             calcOperator.add("+");
        }
    }

答案 3 :(得分:0)

我想你想把像'4 + 2 - 1/3'这样的表达分解成

  • int [] calcOperator = {4,2,1,3};
  • char [] calcOperand = {'+',' - ','/'};

正确?

你正做得差不多!浏览用户输入并从头到尾创建子串(不要拆分字符串)。

提示:使用动态数据对象而不是数组!

private ArrayList<Integer> calcOperator = new ArrayList<Integer>();
private ArrayList<Character> calcOperand = new ArrayList<Character>();

public void parseInput(){
    int start = 0;
    int end = 0;
    for (int i = 0; i < input.length(); i ++){
        if (input.charAt(i) == '+'){
            end = i;
            String operator = input.substring(start, end).trim();
            int opAsInt = Integer.parseInt(operator);
            char operand = '+'; //as defined in the if-clause

            calcOperator.add(opAsInt);
            calcOperand.add(operand);

            //set index
            start = i + 1;
        }           
    }
    //last operatore would be missing right now, so add it!
    String operator = input.substring(start, input.length() ).trim();
    int opAsInt = Integer.parseInt(operator);
    calcOperator.add(opAsInt);
}