【发布时间】:2020-07-20 17:27:27
【问题描述】:
我是 Java 的初学者,所以我想知道是否有人可以帮助我解决我的问题。我正在做 USACO 青铜问题,Square Pasture。
这是我的代码。
BufferedReader in = new BufferedReader(new FileReader("square.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("square.out")),true);
StringTokenizer st = new StringTokenizer(in.readLine());
int smallestX = 10;
int largestX = 0;
int smallestY = 10;
int largestY = 0;
for (int i=0; i<2; i++) {
int xLow=Integer.parseInt(st.nextToken());
int yLow=Integer.parseInt(st.nextToken());
int xHigh=Integer.parseInt(st.nextToken());
int yHigh=Integer.parseInt(st.nextToken());
if(xLow < smallestX)
smallestX = xLow;
if(xHigh > largestX)
largestX = xHigh;
if(yLow < smallestY)
smallestY = yLow;
if(yHigh > largestY)
largestY = yHigh;
}
int ydistance=largestY-smallestY;
int xdistance=largestX-smallestX;
int maxi=Math.max(ydistance,xdistance);
int ans=maxi*maxi;
out.print (ans);
out.close();
这个问题的样本输入是
6 6 8 8
1 8 4 9
我收到一条错误消息: java.util.NoSuchElementException 在 java.util.StringTokenizer.nextToken(Unknown Source)
我相信这是因为我的代码一直在检查同一行。当输入为时,我的代码运行完美
6 6 8 8 1 8 4 9
我在 StackOverflow 上查看了类似的问题,但没有找到。
【问题讨论】:
-
我最好使用调试器来查找失败的行。或者,您可以向我们展示完整的堆栈跟踪,并向我们展示它失败的行。
-
您将文件中的一行读入分词器(有 4 个整数),然后尝试读取 4 个数字两次
标签: java file bufferedreader filereader