Why do I have to write line = reader.readLine() in while for the second time? I thought I have already defined it above

huangapple go评论67阅读模式
英文:

Why do I have to write line = reader.readLine() in while for the second time? I thought I have already defined it above

问题

我会非常感激详细的回答我已经尝试过 `while(line != null)`,但结果是 `ArrayIndexOutOfBoundsException`。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Solution {
public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    **String line = reader.readLine();**
    int a = Integer.parseInt(line);
    int max = 0;
    int count = 0;
    int intis[] = new int [500];
    if ( a > 0 ){
        while ( (**line = reader.readLine())** != null ) {
          count++;
            intis[count] = Integer.parseInt(line.trim());

        }

        for ( int i : intis ) {
            if ( max < i ) max = i;
        }
    }
    else {

  }
    System.out.println(max);
}
}
英文:

I would appreciate detailed answer, I have tried while(line != null) but it ends up with ArrayIndexOutOfBoundsException.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Solution {
public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    **String line = reader.readLine();**
    int a = Integer.parseInt(line);
    int max = 0;
    int count = 0;
    int intis[] = new int [500];
    if ( a > 0 ){
        while ( (**line = reader.readLine())** != null ) {
          count++;
            intis[count] = Integer.parseInt(line.trim());

        }

        for ( int i : intis ) {
            if ( max < i ) max = i;
        }
    }
    else {

  }
    System.out.println(max);
}
}

答案1

得分: 1

有在定义变量和赋值变量之间存在差异,我认为你可能混淆了。

在第一行,你是完全正确的 - 你在定义 String line(并且同时赋值 reader.readLine())。这与while循环内部的调用是不同的,循环内部的调用每次都会将变量重新赋值为不同的行。

只要知道,你的循环并不能阻止数组溢出,这就是为什么当你只检查line != null时,会出现ArrayIndexOutOfBoundsException。你可能想要向其中添加类似这样的内容,以确保这一点。你的文件可能只有少于500行,或者可能有501行。这有助于防范这种情况;如果行数少于500行,读取器会生成null;如果行数更多,那么循环会停止,因为没有其他地方可以存储数据。

while(line = reader.readLine() != null && count < inits.length) {
    // 逻辑
}
英文:

There's a difference between defining the variable and assigning the variable that I think you're conflating.

On the first line, you're absolutely right - you're defining String line (and also assigning it the value reader.readLine()). This is different from the invocations inside of the while loop, which reassign the variable to a different line every time.

Just know that your loop doesn't bound you against overfilling your array, which is why when you only checked line != null, you had your ArrayIndexOutOfBoundsException. You may want to add something like this to it to be sure of that fact. Your file may only have less than 500 lines, or it may have 501 lines. This helps to guard against that; if you have less than 500 lines, the reader produces null; if you have more, then you stop because you have nowhere else to put the data.

while(line = reader.readLine() != null &amp;&amp; count &lt; inits.length) {
    // logic
}

huangapple
  • 本文由 发表于 2020年9月4日 05:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63731583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定