我一直在网上寻求帮助,但是我找不到它,所以我要求发布。
我有一个格式如下的txt文件
1/2 0 1/6 6/11 1/6 2/10
我需要读取每个单独的分数,并在名为BigFraction(num,denom)的函数中输入分子和分母。
我打电话给读者并将其作为字符串输出。 然后我在空白上拆分,将各个分数作为字符串 将它们输入我的帮助方法strToBF, 它在前进中分裂并输入分子和分母。
但它不起作用,我们将非常感谢任何帮助
我的主要方法
String file = fileToString(fileName);
file.replaceAll("(.*)\\/(.*)", "a");
String[] splitFile = file.split(" ");
BigFraction conditionA = stringToBF(splitFile[0]);
BigFraction c1A = stringToBF(splitFile[1]);
BigFraction c2A = stringToBF(splitFile[2]);
ConditionalTwoCoinChannel CTCCA = new ConditionalTwoCoinChannel(conditionA, new TwoCoinChannel(c1A, c2A));
BigFraction conditionB = stringToBF(splitFile[3]);
BigFraction c1B = stringToBF(splitFile[4]);
BigFraction c2B = stringToBF(splitFile[5]);
ConditionalTwoCoinChannel CTCCB = new ConditionalTwoCoinChannel(conditionB, new TwoCoinChannel(c1B, c2B));
List<ConditionalTwoCoinChannel> outputList = new ArrayList<ConditionalTwoCoinChannel>();
outputList.add(CTCCA);
outputList.add(CTCCB);
return outputList;
和我的两个辅助方法
private static String fileToString(String file)
throws FileNotFoundException, IOException {
BufferedReader fileReader = new BufferedReader(new FileReader(file));
StringBuilder sBuilder = new StringBuilder();
String fileLine = fileReader.readLine();
try {
while (fileLine != null) {
sBuilder.append(fileLine);
sBuilder.append(" ");
fileLine = fileReader.readLine();
}
} catch (FileNotFoundException e1) {
throw new RuntimeException("file not found");
} catch (IOException e2) {
throw new RuntimeException("Input Error");
}
finally {
fileReader.close();
}
return sBuilder.toString();
private static BigFraction stringToBF(String str){
if (strToInt(str) == 0) {
return BigFraction.ZERO;
}
else {
String[] strSplit = str.split("(.*)a(.*)");
int numerator = strToInt(strSplit[0]);
int denominator = strToInt(strSplit[1]);
return new BigFraction(numerator, denominator);
}
}
答案 0 :(得分:1)
此
String[] strSplit = str.split("(.*)a(.*)");
无法拆分“1/2”
试
String[] strSplit = str.split("/");