Java 如何在不使用数组的情况下读入字符串并存储到不同的变量中

Java 如何在不使用数组的情况下读入字符串并存储到不同的变量中,java,string,store,calculator,Java,String,Store,Calculator,我有一个家庭作业,我应该读一个字符串,格式是[1+2/3+4],然后我必须存储5个变量,它们是前3个数字和前2个运算符。我的问题是如何循环字符串并将值存储到不同的变量中。这就是我目前所拥有的 公共类扩展评估{ static String expression; static double o1; static double o2; private static double o3; static char operator1; private static char operator2; pu

我有一个家庭作业,我应该读一个字符串,格式是[1+2/3+4],然后我必须存储5个变量,它们是前3个数字和前2个运算符。我的问题是如何循环字符串并将值存储到不同的变量中。这就是我目前所拥有的

公共类扩展评估{

static String expression;
static double o1;
static double o2;
private static double o3;
static char operator1;
private static char operator2;

public static  String getOperand(String s) {
    s = s.trim();
    String num = "";



    while (s.length() > 0 && s.charAt(0) >= '0' && s.charAt(0) <= '9') {



     num = num + s.charAt(0);
     s = s.substring(1);

    }

    expression = s;
    return (num);
}

public static char getOperator(String s) {
    s = s.trim();
    char r = 0;
    int i = 0;
    while (s.length() > 0 && s.charAt(0) >= '0' && s.charAt(0) <= '9') {

        r = s.charAt(i);
        s = s.substring(i+1);

        i++;
    }



    return (r);
}


public static double add(double a, double b) {

    return (a + b);

}

public static double sub(double a, double b) {

    return (a - b);

}

public static double mult(double a, double b) {

    return (a * b);

}

public static double div(double a, double b) {

    return (a / b);

}

public static double solveExpresion(String e) {

    double answer = 0;

    for(int i = 0;i< e.length();i++){

    String operand1;
    String operand2;

    operand1 = getOperand(e);
    o1 = Double.parseDouble(operand1);

    operand2 = getOperand(expression);
    o2 = Double.parseDouble(operand2);

    operator1 = getOperator(expression);





    }

    return (answer);


}
}

每次我运行它时,它都表示我有一个空字符串。

下面是我的快速解决方案,使用
匹配器
/
模式
,按预期检索5个变量:

    public static void main(String[] args) throws ParseException {
        String operation = getOperationFromInput();
        List<Integer> firstThreeNumbers = retrieveFirstThreeNumbers(operation);
        List<Character> firstTwoOperators = retrieveFirstTwoOperators(operation);
        printNumbersAndOperators(firstThreeNumbers, firstTwoOperators);
    }

    private static String getOperationFromInput() {
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        return scanner.next();
    }

    private static List<Integer> retrieveFirstThreeNumbers(String operation) {
        List<Integer> firstThreeNumbers = new ArrayList<>();
        Pattern patternNumber = Pattern.compile("\\d");
        Matcher matcher = patternNumber.matcher(operation);
        for (int i = 0; i < 3 && matcher.find(); i++) {
            firstThreeNumbers.add(Integer.valueOf(matcher.group()));
        }
        return firstThreeNumbers;
    }

    private static List<Character> retrieveFirstTwoOperators(String operation) {
        List<Character> firstTwoOperators = new ArrayList<>();
        Pattern patternOperator = Pattern.compile("[+-/*]");
        Matcher matcher = patternOperator.matcher(operation);
        for (int i = 0; i < 2 && matcher.find(); i++) {
            firstTwoOperators.add(matcher.group().charAt(0));
        }
        return firstTwoOperators;
    }

    private static void printNumbersAndOperators(List<Integer> firstThreeNumbers, List<Character> firstTwoOperators) {
        System.out.println(firstThreeNumbers + " " + firstTwoOperators);
    }
publicstaticvoidmain(字符串[]args)引发异常{
字符串操作=getOperationFromInput();
List firstThreeNumbers=检索firstThreeNumbers(操作);
List firstTwoOperators=检索firstTwoOperators(操作);
打印号码和运算符(前三个号码,前两个运算符);
}
私有静态字符串getOperationFromInput(){
扫描仪=新的扫描仪(System.in);
scanner.useDelimiter(“\n”);
返回scanner.next();
}
私有静态列表检索FirstThreeNumber(字符串操作){
List firstThreeNumbers=new ArrayList();
Pattern Pattern=Pattern.compile(\\d”);
Matcher Matcher=patternNumber.Matcher(操作);
对于(int i=0;i<3&&matcher.find();i++){
add(Integer.valueOf(matcher.group());
}
返回前三个数字;
}
私有静态列表检索FirstTwoOperators(字符串操作){
List firstTwoOperators=new ArrayList();
Pattern Pattern操作符=Pattern.compile(“[+-/*]”);
Matcher Matcher=patternOperator.Matcher(操作);
对于(int i=0;i<2&&matcher.find();i++){
add(matcher.group().charAt(0));
}
返回前两个运算符;
}
私有静态void printNumbersAndOperators(列出firstThreeNumbers,列出firstTwoOperators){
System.out.println(firstThreeNumber+“”+firstTwoOperators);
}
在这段代码中,我将前三个数字存储在
列表中,将前两个运算符存储在
列表中


有多种方法可以实现一个解决方案,因此作为家庭作业,试着找到其他方法:)

这是不可能的
,因此,一旦使用字符串,您就在使用数组。问题是,我不能使用数组或它必须存储到变量中的任何排序的列表进行计算,然后从输入字符串中再存储3个变量
    public static void main(String[] args) throws ParseException {
        String operation = getOperationFromInput();
        List<Integer> firstThreeNumbers = retrieveFirstThreeNumbers(operation);
        List<Character> firstTwoOperators = retrieveFirstTwoOperators(operation);
        printNumbersAndOperators(firstThreeNumbers, firstTwoOperators);
    }

    private static String getOperationFromInput() {
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        return scanner.next();
    }

    private static List<Integer> retrieveFirstThreeNumbers(String operation) {
        List<Integer> firstThreeNumbers = new ArrayList<>();
        Pattern patternNumber = Pattern.compile("\\d");
        Matcher matcher = patternNumber.matcher(operation);
        for (int i = 0; i < 3 && matcher.find(); i++) {
            firstThreeNumbers.add(Integer.valueOf(matcher.group()));
        }
        return firstThreeNumbers;
    }

    private static List<Character> retrieveFirstTwoOperators(String operation) {
        List<Character> firstTwoOperators = new ArrayList<>();
        Pattern patternOperator = Pattern.compile("[+-/*]");
        Matcher matcher = patternOperator.matcher(operation);
        for (int i = 0; i < 2 && matcher.find(); i++) {
            firstTwoOperators.add(matcher.group().charAt(0));
        }
        return firstTwoOperators;
    }

    private static void printNumbersAndOperators(List<Integer> firstThreeNumbers, List<Character> firstTwoOperators) {
        System.out.println(firstThreeNumbers + " " + firstTwoOperators);
    }