0

I am working on a project and I dont know how to properly parse some input for example a1 = ("hello" + "World"). a1 is a cell that I made and i am trying to at the end put helloworld into that cell. This is one of my classes that I use parse input. When I use numbers it works fine but not with Strings. Im just trying to be able to parse an input like ("hi" + "man") to himan. I just want to be able to parse out the spaces and the plus sign and make himan into a single string.

import java.util.Scanner;


public class ParseInput {
    private static String inputs;
    static int col;
    private static int row;
    private static String operation;
    private static Value field;


    public static void parseInput(String input){
         //splits the input at each regular expression match. \w is used for letters and \d && \D for integers
        inputs = input;
        Scanner tokens = new Scanner(inputs);
        String[] strings = input.split("=");
        String s1 = strings[0].trim(); // a1
        String s2 = strings[1].trim(); // "Hello" + "World"

        strings = s2.split("\\+");
        String s3 = strings[0].trim().replaceAll("^\"", "").replaceAll("\"$", ""); // Hello
        String s4 = strings[1].trim().replaceAll("^\"", "").replaceAll("\"$", ""); // World
        String field = s3 + s4;

        String colString = s1.replaceAll("[\\d]", ""); // a
        String rowString = s1.replaceAll("[\\D]", ""); // 1
        int col = colString.charAt(0) - 'a'; // 0
        int row = Integer.parseInt(rowString);
        TextValue fieldvalue = new TextValue(field); 

        Spreadsheet.changeCell(row, col, fieldvalue);

        String none0 = tokens.next();
        @SuppressWarnings("unused")
        String none1 = tokens.next();
        operation = tokens.nextLine().substring(1);
        String[] holder =  new String[2];
        String regex = "(?<=[\\w&&\\D])(?=\\d)";   
        holder = none0.split(regex);
        row = Integer.parseInt(holder[1]);
        col = 0;
        int counter = -1;
        char temp = holder[0].charAt(0);
        char check = 'a';
        while(check <= temp){
            if(check == temp){
                col = counter +1;
            }
            counter++;
            check = (char) (check + 1);
        }
         System.out.println(col);
         System.out.println(row);
         System.out.println(operation);
         setField(Value.parseValue(operation));

         Spreadsheet.changeCell(row, col, fieldvalue);

    }

    public static Value getField() {
        return field;
    }

    public static void setField(Value field) {
        ParseInput.field = field;
    }
}
4

2 に答える 2

0

スペースを削除しようとしている場合、それはワンライナーです...

input = input.replaceAll("[\\s+\"]", "");

これを行うもう 1 つの方法は、すべての「単語以外」の文字 (0-9、az、AZ、およびアンダースコアとして定義) を単純に削除することです。

input = input.replaceAll("\\W", "");
于 2012-04-17T05:17:34.717 に答える
-1

Use a StringBuilder. String is immutable in Java, which means you will be creating a new instance every time you want to combine two strings. Use the append() method (it accepts just about anything) to add something onto the end.

StringBuilder input = "";
input.append("hel").append("lo");

Here is the documentation for it. Take a look.

于 2012-04-17T05:23:32.240 に答える