私のコードは、.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()
ますか? これは、入力行エラーを処理するための適切な設計ですか?