0
所以我有一个Parser类,它读入用户输入并在每个“/”处将其分开。这是我第二次做这样的类,但是这次我必须根据split [0]的值是第一个值的两种不同方式解析字符串。我不知道要为返回类型放置什么,因为所有类正在做的是取得输入的字符串,在每个“/”分裂。然后将这些输入分配给其他类中的变量。任何帮助,将不胜感激。由于在解析器类中丢失返回语句错误
这里是类
公共类DrinkParser {
public static Drink parseStringToDrink(String lineToParse){ //ERROR: missing return statement.
String regex = "[/]";
String [] split = lineToParse.split(regex);
if ("Box".equals(split[0]) || "box".equals(split[0])){
DrinkInBox dIB = new DrinkInBox(split[1], Double.parseDouble(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]), Integer.parseInt(split[5]));
return dIB;
}
if("Cylinder".equals(split[0]) || "cylinder".equals(split[0])){
DrinkInCylinder dIC = new DrinkInCylinder(split[1], Double.parseDouble(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]));
return dIC;
}
}
}
上次我写了这样的一类,我不得不这样做字符串中读取不管什么分裂[0]是...类继承人,以防万一。
公共类BankParser {
public static Bank bankParser(String lineToParse){ //parses the string and sets the bankName,bankID, and bankAddress based off the inputed String
String regex = "[/,]";
String[] split = lineToParse.split(regex);
Bank b = new Bank();
b.setBankName(split[0]); //sets the first split to the BankName
b.setBankID(split[1]); // sets the second split to the BankID
b.setBankAddress(split[2], split[3]); //set the city to the third split and the state to the fourth split in BankAddress
return b;
}
}
你需要一个返回,或者你可以只是把一个void返回类型,只是设置值? –
@AlexJohnson'parseStringToDrink'方法似乎是一个'静态'工厂方法,所以它应该返回适当的'Drink'对象。 – rgettman
在第二个“if”语句后返回null,并让调用方类在分析器返回null时决定该做什么。 – Tony