This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Learn Java with Math: Using Fun Projects and Games and have Ron Dai on-line!
See this thread for details.
Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

reading a user-specified text file into an ArrayList of Strings using Java?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am advised to use a while loop the Scanner method hasNextLine() and
in the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings. I am new to Java so please keep that in mind. I'm not exactly sure if this is right, but this is what I have gotten so far:

 
Marshal
Posts: 66467
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you get an output from that code? No, you won't because you haven't given a valid file name. You can't pass "" as a file name. You have to use a name of a real file. If you are using Window®, remember the file separator is \ and you can't write \ so you have to write \\ in the String for the file name instead.
Try a real file name and see what happens. I think that will work nicely if you get a real file name.

Once you are printing the lines, you know you have got your hands on them and you can add them to the List easily in that loop. Also make sure you close the Scanner after you have finished reading. Use what the Java® Tutorials suggest to make sure it is closed.

You didn't quite get the code tags right, but I'll correct that for you.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good so far.

Obviously you need to give it some real input, but once you do that, it looks like you are accomplishing your goal of reading the input line by line into the String variable "line"
 
Shawn Harvey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks you both! So, this is what I have so far, but where is it checking for the file at (location wise, ie C drive)?:

 
Stefan Evans
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way to find that out is to get the program to tell you itself:
 
Shawn Harvey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, makes sense. Now if I wanted to search for a user specific word or phrase in the file, would I use:



then do a for loop and an if:



and a system out print that says whether it was there or there is no word in the file.
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks logically sound to me.
The only bit you haven't demonstrated is how to get from a list of lines in the file to "words"
Or is each "line" in the file one word?
 
Campbell Ritchie
Marshal
Posts: 66467
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you try a file chooser to find your file. Much more reliable than typing the file name by hand.Check the link for the different options you can apply to file choosers. Also for whether I have spelt any of the methods wrongly.
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!