我一直在互联网上寻求帮助,但我找不到它,所以我正在求助于发布。
我有一个格式如下的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);
}
}