0

私のコードは、.txt から行を読み取り、それらの入力を解析することです。

入力はこの形式です<operation> <category> <name> <price> <quantity> <weight> <optional field1> <optional field2>

電子機器には壊れやすい (F/NF) という 2 つのオプション フィールドがあり、配送先の州には、食料品には生鮮食品 (P/NP) というオプション フィールドが 1 つあります。ライン入力の例をいくつか示します

insert clothing shirt 20.50 1 1

insert electronics PS3 300 1 5.2 F NM

insert groceries cabbage 2.00 5 1 NP

legalLine()入力のエラーを処理するメソッドを書くことを考えています

class sampleClass{
          public static void isLegalLine(String lineInput){
    Scanner s = new Scanner(lineInput);
    String operation = null;
    String category = null;
    String name = null;
    float price = 0;
    int quantity = 0;
    float weight = 0;
    String opt1 = null;
    String opt2 = null;
    try{
        operation = s.next(); 
        category = s.next();
        name = s.next();
        price = Float.parseFloat(s.next());
        quantity = Integer.parseInt(s.next());
        weight = Float.parseFloat(s.next());
        if (!operation.equalsIgnoreCase("insert")
                && !operation.equalsIgnoreCase("search")
                && !operation.equalsIgnoreCase("delete")
                && !operation.equalsIgnoreCase("update")
                && !operation.equalsIgnoreCase("print")) {
            System.out.println("invalid operation");
        }
        // more validations for variables category, name, price, quantity, weight goes here
        if(category.equalsIgnoreCase("electronics")){
            try{
                opt1 = s.next();
                opt1 = s.next();
            }catch(Exception e){

            }
        }else if(category.equalsIgnoreCase("groceries")){
            opt1 = s.next();
        }
    }catch (Exception e){
        //general catch for now
        e.getMessage();
    }
}


          public static void main(String[] args) 
      {
               FileReader freader = new FileReader(args[0]);
               BufferedReader bfrReader = new BufferedReader(freader);
               isLegalLine(bfrReader.readLine());


                //program does calculation for total price of input
}

ほら...各トークンを のそれぞれの変数に設定するのに苦労しましたがisLegalLine()、どうすればそれらの情報を に戻すことができmain()ますか? これは、入力行エラーを処理するための適切な設計ですか?

4

2 に答える 2

1

ファイルを1行ずつ読む方が良いと思います。次に、各行に正規表現を適用して、パラメータを取得します (必須またはオプション)。

正規表現は次のようになります。

([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*

括弧内のすべての式は、1 つのパラメーターに対応します。コードの例は次のとおりです。

String pattern = "([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*";
Pattern p = Pattern.compile(pattern);
for(String str : strings) {
  Matcher m = p.matcher(str);
  m.find();
  if(m.matches()) {
     String operation = m.group(1);
     String category = m.group(2);
     ...
     float weight = Float.parseFloat(m.group(6));
     String opt1 = m.group(7);
     String opt2 = m.group(8);
  }
}

次に、操作にオプションのパラメーターが存在する (または存在しない) かどうかを確認し、必要に応じてエラーを生成します。値を返すことで、任意の情報を main(...) メソッドに戻すことができます。戻り値は何でもかまいません。

于 2012-11-22T23:49:52.633 に答える
1

それらも処理する必要があるため、私が見る良い解決策は次のとおりです。

衣類、電子機器、食料品の 3 つのサブクラスを持つ Product クラスを作成します。

次に、関数を作成します

public static Product isLegalLine(String lineInput)

行を解析した後、製品を返します。

于 2012-11-22T23:15:50.577 に答える