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.
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. I printed the size of the ArrayList at the end but my program keeps saying 0. What am I doing wrong?
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());
java file arraylist
add a comment |
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.
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. I printed the size of the ArrayList at the end but my program keeps saying 0. What am I doing wrong?
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());
java file arraylist
I am more concerned with the business logic behind your problem. If the input file only has movies and the years they were released, how you can relate one movie with another/another actor?
– Tim Biegeleisen
Nov 12 '18 at 1:06
add a comment |
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.
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. I printed the size of the ArrayList at the end but my program keeps saying 0. What am I doing wrong?
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());
java file 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.
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. I printed the size of the ArrayList at the end but my program keeps saying 0. What am I doing wrong?
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());
java file arraylist
java file arraylist
asked Nov 12 '18 at 0:49
Deevena Sista
1
1
I am more concerned with the business logic behind your problem. If the input file only has movies and the years they were released, how you can relate one movie with another/another actor?
– Tim Biegeleisen
Nov 12 '18 at 1:06
add a comment |
I am more concerned with the business logic behind your problem. If the input file only has movies and the years they were released, how you can relate one movie with another/another actor?
– Tim Biegeleisen
Nov 12 '18 at 1:06
I am more concerned with the business logic behind your problem. If the input file only has movies and the years they were released, how you can relate one movie with another/another actor?
– Tim Biegeleisen
Nov 12 '18 at 1:06
I am more concerned with the business logic behind your problem. If the input file only has movies and the years they were released, how you can relate one movie with another/another actor?
– Tim Biegeleisen
Nov 12 '18 at 1:06
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254737%2fread-text-file-with-scanner-and-store-to-arraylist%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 12 '18 at 0:56
Scary Wombat
35k32252
35k32252
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254737%2fread-text-file-with-scanner-and-store-to-arraylist%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I am more concerned with the business logic behind your problem. If the input file only has movies and the years they were released, how you can relate one movie with another/another actor?
– Tim Biegeleisen
Nov 12 '18 at 1:06