gpt4 book ai didi

java - 如何在Java中从文件中读取特定单词后的int

转载 作者:行者123 更新时间:2023-12-01 11:53:44 25 4
gpt4 key购买 nike

我正在尝试读取一个位于特定单词之后的 int 。我尝试使用分隔符和扫描仪来完成此操作,但似乎无法将 int 获取到我想要的变量。这段代码有什么提示或修复吗?

这是我尝试过的:

public void readFromFile(String resultOrNot, String fileName) {

nameOfFile = "/var/lib/tomcat7/webapps/Matrices/WEB-INF/MatrixData/";
workMe = fileName;

try {

Scanner readFile = new Scanner(new File(nameOfFile + workMe));
readFile.useDelimiter("Rows: ");

while(readFile.hasNextInt()) {
stringRow = readFile.next();

}
setRow1VIAString(stringRow);

readFile.useDelimiter("Columns: ");

while(readFile.hasNextInt()) {
stringCol = readFile.next();

}
setColumn1VIAString(stringCol);

readFile.useDelimiter("Method used: ");
while(readFile.hasNext()) {
operationName = readFile.next();
}


readFile.useDelimiter("\n");
for(int i = 0; i < row1 || readFile.hasNextDouble(); i++)
for(int j = 0; j < column1; j++)
matrix1[i][j] = readFile.nextDouble();

readFile.close();



workMe = workMe.replace("MAIN", "SECOND");
readFile = new Scanner(new File(nameOfFile + workMe));
readFile.useDelimiter("Rows: ");

while(readFile.hasNextInt()) {
stringRow = readFile.next();

}
setRow2VIAString(stringRow);

readFile.useDelimiter("Columns: ");

while(readFile.hasNextInt()) {
stringCol = readFile.next();

}
setColumn2VIAString(stringCol);

readFile.useDelimiter("\n");
for(int i = 0; i < row1 || readFile.hasNextDouble(); i++)
for(int j = 0; j < column1; j++)
matrix1[i][j] = readFile.nextDouble();

readFile.close();

if("giveMeResult".equals(resultOrNot)) {

workMe = workMe.replace("SECOND", "RESULT");
readFile = new Scanner(new File(nameOfFile + workMe));
readFile.useDelimiter("Rows: ");

while(readFile.hasNextInt()) {
stringRow = readFile.next();

}
setRow3VIAString(stringRow);

readFile.useDelimiter("Columns: ");

while(readFile.hasNextInt()) {
stringCol = readFile.next();

}
setColumn3VIAString(stringCol);

readFile.useDelimiter("\n");
for(int i = 0; i < row3 || readFile.hasNextDouble(); i++)
for(int j = 0; j < column3; j++)
matrix1[i][j] = readFile.nextDouble();

}




} catch(IOException ex) {

ex.printStackTrace();
}

}

我收到此错误:

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:454)
java.lang.Integer.parseInt(Integer.java:527)
matrixcalculator.MatrixCalculator.setRow1VIAString(MatrixCalculator.java:171)
matrixcalculator.MatrixCalculator.readFromFile(MatrixCalculator.java:728)
org.apache.jsp.choosing_jsp._jspService(choosing_jsp.java:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

最佳答案

当你这样做时:

setRow1VIAString(stringRow);

您假设 stringRow 具有从文件中读取的值。

如果这个条件永远不成立怎么办?

while(readFile.hasNextInt())

stringRow 将为 null,因此会出现错误。

这是我的最佳猜测,因为我无法知道您的文件中的内容或源的完整上下文。

编辑:尝试一下

readFile.useDelimiter("\\s*Rows:\\s*");

或者既然您知道确切的格式,这也可能有效

readFile.findInLine("Rows: (\\d+)");
MatchResult result = readFile.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i));

关于java - 如何在Java中从文件中读取特定单词后的int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28594903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com