Java populate arraylist from text file




















While the answer above me works, here's another way to do it. Make sure to import java. If you know how to read a file line by line, either by using Scanner or by using BufferedReader then reading a text file into ArrayList is not difficult for you. All you need to do is read each line and store that into ArrayList, as shown in following example:.

Just remember to close the BufferedReader once you are done to prevent resource leak, as you don't have try-with-resource statement. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Reading text file into arraylist Ask Question. Asked 5 years, 9 months ago.

Active 4 years, 7 months ago. Viewed 45k times. So basically I have a text file that looks like this: 1:John:false 2:Bob:false 3:Audrey:false How can I create an ArrayList from the text file for each line?

On the other hand, if we had only used the default delimiter whitespace , the ArrayList would look like:. Scanner has some useful functions for parsing data, such as nextInt , nextDouble , etc. Important : Calling. It will return an int value only if the next items the Scanner "scans" is a valid int value, otherwise an exception will be thrown.

An easy way to make sure an exception doesn't arise is to perform a corresponding "has" check - like. Even though we don't see that when we call functions like scanner. Very Important: An extremely common mistake with using Scanner occurs when working with files that have multiple lines and using.

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! This code seems to be logically sound - we read an integer from the file, then the following line, then the second integer.

If you try to run this code, the InputMismatchException will be thrown without an obvious reason. If you start debugging and printing what you've scanned, you'll see that int a loaded well, but that String s is empty. Why is that? Join thousands of Treehouse students and alumni in the community today. Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations. Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community.

Start your free trial today. I have an assignment to create an interactive quiz using a text file. A question is formatted like this it's actually single spaced I had to separate the lines to keep the forum from smushing the whole thing together. The first int is the number of answer options and the second one is the correct answer. I am supposed to create an array list of question objects using a file with 10 questions.

I am presuming that first I need to read the questions as 10 separate objects and form the arraylist. Then the arraylist needs to feed questions to two players.

I think I can do the second task. That makes me cringe, but since it's supposed to read input from a file I thought I would need to ask the user in the driver specific to the option they chose and then pass the file name to the addressbook since all the information that will be read from the file will then need to be passed to the Contact class' constructor in order to be made into a contact object.

So once it's passed into the AddressBook I have a method that can read the contents of the file and assemble each line into an ArrayList of type String since there's really no way of knowing how many contacts a file could actually contain. So once each line is stored into an array list then I need to use another method to somehow take each item from that ArrayList and split at a delimiting pipe space character in the text file and then somehow pass each piece of information into the contact class constructor.

Sorry to make this so long, I'm just really confused with the instruction for this extension and am not sure if I'm just not fully thinking it through of it's just worded poorly or what. Any suggestions or help getting started in the right direction or ideas as to what I should consider putting where would be greatly appreciated.

Carey Brown. Saloon Keeper. I like Regarding your menu, I'd suggest making each option into its own method which would keep your switch block nice and clean. With regards to your enum, you'd need a way to convert your enum values to and from some sort of text representation for writing and reading a file.

You could do this based on ordinal value but that seems a bit brittle because if you modify the enum list your file will be out of date. Better to write and read the enum value name.

Cross post? What are you talking about? That isn't my post if that's what you meant, unless you simply mean that it's a similar question, however I did search around to try to puzzle some of this out on my own, but with little success.

I didn't even see that stack post you mentioned. Actually that post on stack looks like its from My humble apologies. Junilu Lacar. Consider this: the previous assignment was to see if you knew how to chain constructors, which I guess you were able to demonstrate that you did. However, what you implemented doesn't make much sense.

If you create a new contact without specifying any contact information, do you really want it to default to Bill Nye the Science Guy every time?

If I were a user of that kind of program, I'd have some choice words for the programmer. If I created a new contact, I'd expect all the contact information to be empty, not initialized by default to that guy. So, wouldn't it be reasonable to change your implementation of the no-argument constructor for this assignment, since the goals are different? As well it should. Given that you have at least 10 menu options, you're looking at a ginormous switch case with well over lines of code!

In a good program, 20 lines of code is pushing the limit of reasonable size of a method. For professional work, the goal is to have about 1 to 5 lines of code on average, and definitely less than 20 lines if you have to push the limit. This is because if you have more than 20 lines, it becomes more and more difficult to read and understand what's going on in that method. For student work, I'd say that limit could be reasonably set to 30 lines of code.



0コメント

  • 1000 / 1000