Java StringBuilder程序在解释空间时遇到问题

时间:2014-05-02 20:37:22

标签: java stringbuilder spaces

我试图通过使用两个堆栈来创建一个简单的计算器,一个用于操作数,另一个用于操作符。我遇到的问题是用户输入。我希望我的程序能够处理用户可能抛出的任何内容(例如(5 + 2)或9 * 5 * 4或(9 / 3.575))。

由于某种原因,当inputCleaner遇到空格时,代码会抛出一个SyntaxErrorException。

这是我到目前为止的代码:

import java.util.*;
import java.lang.String;
import java.lang.StringBuilder;
import java.lang.Exception;

public class InfixEvaluator
{

 public static class SyntaxErrorException extends Exception {
    /** Construct a SyntaxErrorException with the specified message.
       @param message The message
       */
    SyntaxErrorException(String message) {
        super(message);
    }
}
/** This is the stack of operands:
   i.e. (doubles/parentheses/brackets/curly braces)
*/
private Stack<Double> operandStack = new Stack<Double>();

/** This is the operator stack
 *  i.e. (+-/*%^)
 */
private Stack<Character> operatorStack = new Stack<Character>();

/** These are the possible operators */
private static final String OPERATORS = "+-/*%^()[]{}";

/** These are the precedence of each
    operator in respective order */
private static final int[] PRECEDENCE = {1, 1, 2, 2, 2, 3};

/** This is an ArrayList of all the discrete
    things (operators/operands) making up an input.
    This is really just getting rid of the spaces,
    and dividing up the "stuff" into manageable pieces.
   */
static ArrayList<String> input = new ArrayList<String>();

public static ArrayList inputCleaner(String postfix){
  StringBuilder poop = new StringBuilder();
  try {
    for (int i = 0; i < postfix.length(); i++) {
        char c = postfix.charAt(i);
        boolean isDigit = (c >= '0' && c <= '9');

        if (isDigit) {
            poop.append(c);
        } else if (c == '.') {
            for (int j = 0; j < poop.length(); j++) {
                if (poop.charAt(j) == '.') {
                    throw new SyntaxErrorException("You can't have two decimals in a number.");
                } else if (j == poop.length() - 1) {
                    poop.append(c);
                }
            }
        } else if ((c == ' ' && poop.length() != 0)) {
            input.add(poop.toString());
            poop.delete(0, poop.length());
        } else if (OPERATORS.indexOf(c)!= -1 && poop.length() != 0) {
            input.add(poop.toString());
            poop.delete(0, poop.length());
        } else {
            throw new SyntaxErrorException("Make sure your input only contains numbers, operators, or parantheses/brackets/braces.");
        }
    }
    return input;
  } catch (SyntaxErrorException ex) {
        System.out.println("That didn't work, something was wrong with the syntax.");
        return input;
  }
}


public static void main(String[] args) {
    ArrayList poop = new ArrayList();
    Scanner f = new Scanner(System.in);
    String g = "";

    System.out.println("Please insert an argument: \n");

    g = f.nextLine();

    poop = inputCleaner(g);


    for (int z = 0; z < poop.size(); z++) {
     System.out.println(poop.get(z));
    }
}
}

我希望你能原谅我不成熟的标签。

0 个答案:

没有答案