整数数组列表在 while 循环内拒绝打印。

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

ArrayList of Integers inside a while loop is refusing to print

问题

以下是翻译好的内容:

我正在解决《编程之圣诞日》(Advent of Code)中的一个问题,并尝试将输入文件的内容放入一个ArrayList中,以下是我的代码:

ArrayList<Integer> arrayList = new ArrayList<>();
try (Scanner s = new Scanner(new File("input.txt")).useDelimiter(",")) {
    
    while (s.hasNext()) {
        int b = Integer.parseInt(s.next());
        arrayList.add(b);
    }
    
}
catch (FileNotFoundException e) {
    // 处理可能的异常
}

System.out.println(arrayList);

但当我运行它时,它并没有打印出ArrayList。我不明白为什么,有人能告诉我我做错了什么吗?

英文:

I am solving a problem from Advent of Code, and trying to put the content of the input file into an arraylist, here's my code for that:

ArrayList&lt;Integer&gt; arrayList = new ArrayList&lt;&gt;();
    try (Scanner s = new Scanner(new File(&quot;input.txt&quot;)).useDelimiter(&quot;,&quot;)) {
        
        while (s.hasNext()) {
            int b = Integer.parseInt(s.next());
            arrayList.add(b);
        }
        
    }
    catch (FileNotFoundException e) {
        // Handle the potential exception
    }


    System.out.println(arrayList);

and when I run it, it does not print the arraylist. I can't understand why, could someone tell me what I did wrong?

答案1

得分: 0

我使用了StringTokenizer,它运行得很完美。我对使用Scanner分割项目不太熟悉,所以我将其转换为了StringTokenizer。希望您对此没有意见。

public static void main(String[] args) throws IOException {
    ArrayList<Integer> arrayList = new ArrayList<>();
    Scanner s = new Scanner(new File("input.in"));

    StringTokenizer st = new StringTokenizer(s.nextLine(), ",");

    while (st.hasMoreTokens()) {
        int b = Integer.parseInt(st.nextToken());
        arrayList.add(b);
    }
    s.close();

    System.out.println(arrayList);
}

这将使用所需的值填充您的ArrayList。

英文:

I used StringTokenizer and it works perfectly. I am not familiar with using Scanner to split items, so I converted it over into a StringTokenizer. Hope you're okay with that.

public static void main(String[] args) throws IOException {
		ArrayList&lt;Integer&gt; arrayList = new ArrayList&lt;&gt;();
		Scanner s = new Scanner(new File(&quot;input.in&quot;));

		StringTokenizer st = new StringTokenizer(s.nextLine(), &quot;,&quot;);

		while (st.hasMoreTokens()) {
			int b = Integer.parseInt(st.nextToken());
			arrayList.add(b);
		}
		s.close();

		System.out.println(arrayList);

}

This fills your ArrayList with the values you want

答案2

得分: 0

你可以验证是否能够读取文件。你的代码可以像这样进行修改。请检查是否打印出 "File found"。如果没有打印出来,这意味着你试图读取的文件不在类路径中。你可能希望参考 https://mkyong.com/java/java-how-to-read-a-file/

...
File source = new File("input.txt");
if (source.exists()) {
    System.out.println("File found");
}
try (Scanner s = new Scanner(source).useDelimiter(",")) {
...
英文:

You can validate if you are able to read the file or not. Your code can be modifed something like this. Please check if it prints "File found". If not it means that file you are trying to read is not in classpath. You might want to refer https://mkyong.com/java/java-how-to-read-a-file/

...
File source = new File(&quot;input.txt&quot;);
	    if(source.exists()) {
	    	System.out.println(&quot;File found&quot;);
	    }
		try (Scanner s = new Scanner(source).useDelimiter(&quot;,&quot;)) {
...

huangapple
  • 本文由 发表于 2020年4月11日 10:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61151287.html
匿名

发表评论

匿名网友

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

确定