简体   繁体   English

使用扫描仪读取文本文件并存储到ArrayList

[英]Read text file with Scanner and store to ArrayList

I have an assignment to create a program that calculates an actor's Bacon Number. 我分配了一个程序来计算演员的培根数。 Here is the contents of my given ActorMovieTist.txt file which is supposed to be read in my program. 这是应该在我的程序中读取的给定ActorMovieTist.txt文件的内容。

Adolf Hitler Der Ewige Jude 1940 
Angelina Jolie Mr. & Mrs. Smith 2005
Anne Hathaway Valentine's Day 2010
Benedict Cumberbatch The Hobbit: An unexpected Journey 2012
Benedict Cumberbatch Black Mass 2015
Brad Pitt Mr. & Mrs. Smith 2005
Brad Pitt Sleepers 1996
Brad Pitt Ocean's Thirteen 2007
Brad Pitt Beyond All Boundaries 2009
Brad Pitt Se7en 1995
Christian Bale The Fighter 2010
Curt Bois Der Ewige Jude 1940
Curt Bois The Great Sinner 1949
Denzel Washington 2 Guns 2013
Denzel Washington The Equalizer 2014
David Struffolino The Equalizer 2014
David Struffolino Knight and Day 2010
Hugh Jackman Flushed Away 2006
Hugh Jackman X Men First Class 2011
Ian McKellen The Hobbit: An unexpected Journey 2012
Julia Roberts Valentine's Day 2010
Julia Roberts Flatliners 1990
Kate Winslet Flushed Away 2006
Kenneth Tobey The Great Sinner 1949
Kenneth Tobey Hero at Large 1980
Kevin Bacon Sleepers 1996
Kevin Bacon Beyond All Boundaries 2009
Kevin Bacon Flatliners 1990
Kevin Bacon Patriots Day 2016
Kevin Bacon Black Mass 2015
Kevin Bacon X Men First Class 2011
Kevin Bacon Hero at Large 1980
Kevin Spacey Se7en 1995
Kevin Spacey Patriots Day 2016
Kevin Spacey Austin Powers in Goldmember 2002
Mark Wahlberg 2 Guns 2013
Mark Wahlberg Patriots Day 2016
Mark Wahlberg The Fighter 2010
Matt Damon Ocean's Thirteen 2007
Tom Cruise Austin Powers in Goldmember 2002

Here is my program which is supposed to use Scanner to read this file and save each Line as an element of an ArrayList but it doesn't. 这是我的程序,应该使用Scanner读取此文件并将每行保存为ArrayList的元素,但事实并非如此。 I printed the size of the ArrayList at the end but my program keeps saying 0. What am I doing wrong? 我在最后打印了ArrayList的大小,但是我的程序一直说0。我在做什么错?

import java.util.*;
import java.io.File;
import java.io.IOException;



public class KevinBacon
{
    public static void main(String args[]) throws Exception{ 
        Formatter output = new Formatter("ActorMovieList.txt");
        File file = new File("ActorMovieList.txt");
        Scanner input = new Scanner(file);
        String line = "";

    ArrayList <String> list = new ArrayList <String>(); 

    while (input.hasNextLine()) {
        list.add(input.nextLine());
    }

    System.out.println(list.size());
}

} }

You are overwriting your input file when you do 执行此操作时,您将覆盖输入文件

Formatter output = new Formatter("ActorMovieList.txt");

If the file exists then it will be truncated to zero size; 如果文件存在,那么它将被截断为零大小; otherwise, a new file will be created 否则,将创建一个新文件

as you do not seems to be using this output object, simply delete this line and recreate your input file. 由于您似乎未使用此output对象,因此只需删除此行并重新创建输入文件即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM