• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with Reading input from a text file into an arraylist

 
Greenhorn
Posts: 8
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please let me begin with: this is an assignment for school. I am NOT looking for someone to do this for me. I am looking for explanations and even for someone to point out where I am going wrong or what might be missing. (for example: Oh you will need a for loop in order to obtain all the values that you need." Not: "enter this code...") "I am trying to write code that will allow me to read text from a file into an arraylist. The main problem that I am having (and there are many) is getting the bufferedreader to read each line and split it into multiple elements. I guess what I want is to store each line as an object. I have tried tackling it two different ways...1) reading in line by line and attempting to parse the data and have it held in columns for which I get a lovely ArrayIndexOutofBounds exception. The other way which also is not working is to create a class in addition to the main class to define the object and then just get the data. Can someone please explain how the data gets into the arraylist using the String[] columns = line.split("\\s+") and the propertyList.add(columns[0]);. I just don't see how it can do it without some sort of loop like a for loop, otherwise how does it know to move to the next part of the string?


the data in the text file looks like:

100001000 520000.00 Farm 201




Thank you in advance for your assistance. I must learn Java at all costs in order to achieve my dreams. So think of your assistance as taking part in helping reach those dreams.

Nynaeve


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nynaeve Ojeda wrote:Can someone please explain how the data gets into the arraylist using the String[] columns = line.split("\\s+") and the propertyList.add(columns[0]);.


First - do you understand what the string.split() method does? And do you understand what the "\\s+" regular expression means? Implicitly in that code there will be a loop, but you don't have to write it, those who wrote the String#split() method wrote it for you.

The second part is how the results get put into the list. Do you understand what the propertyList.add(columns[0]) is doing? What does columns[0] mean?
 
Nynaeve Ojeda
Greenhorn
Posts: 8
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the following:

string.split() method = takes a line<string> and breaks it up into smaller strings according to the delimiter selected in this case "\\s+" which means all the next whitespace
propertyList.add(columns[0]) = should be adding the new smaller strings to the ArrayList propertyList beginning with the first index which is 0. If the loop is included/implied in the string.split()method then it should automatically add all of the smaller strings on the original line until it hits the new line indicator in this case the return and then continue throughout the file until there are no more lines to read or (line =null)

I am a total java noobie but I have read countless books, forums, blogs, etc. whenever I hit a problem and that is why I am so frustrated because I think I get it but obviously I am missing something because my code does not execute. Please let me know if I have correctly understood the functions listed above and if not please let me know where I have gone astray.

Thanks!
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nynaeve Ojeda wrote:I understand the following:

string.split() method = takes a line<string> and breaks it up into smaller strings according to the delimiter selected in this case "\\s+" which means all the next whitespace


That is correct.

propertyList.add(columns[0]) = should be adding the new smaller strings to the ArrayList propertyList beginning with the first index which is 0.



But that is not. The add method adds a single object to the list. columns[0] is the String at index 0 in the columns array. So it only adds that one String.

If the loop is included/implied in the string.split()method then it should automatically add all of the smaller strings on the original line until it hits the new line indicator


No, judging one one method's actions based on the actions of a completely different, unrelated method in a different class is not a good idea. Take a look at the API for List#add(): here. It says 'append the specified element to the end of the list.' Nothing about arrays, no plurals, and nothing to suggest that the action does anything more than to the single Object you pass to it. Since you pass it a single String that is at index 0 in the array, the List doesn't even know about the array, doesn't have access to it, and would find it quite impossible to add any other values from the array to the List. On the other hand, the String#split() method API: here has multiple plural references, says it creates the array, and if you follow the trail to the multiple argument split() method explains that it does the search repeatedly.

So, given that the add() method just adds the single String to the list, and you are passing it columns[0], how can you get all the values of columns int the List?
 
Nynaeve Ojeda
Greenhorn
Posts: 8
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Upon reading the API link that you sent I see that the string.split()method can also take an integer for a limit. String string.split(regex, int limit). So I should add in a for loop to capture each substring and also set the integer in the argument to let it know how many times to keep going through each line/string.

String[] columns = line.split("\\s+", 5);
for (String s : columns){
propertyNumberList.add(s);
}
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may or may not need the limit in the split - depends on how many columns you get, and how many you need. But the part that you were missing before is the for loop. So congrats for working that out!
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Nynaeve Ojeda
Greenhorn
Posts: 8
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried to implement the "for" loop various ways and every time I do, it thows errors and exceptions and crashes my program. What am I missing with this loop? Is it the location of the loop, the syntax, the variables I am using? I am using the for loop to iterate through the substrings created by the string.split("\\s+") to obtain all of the strings in the column[2] which is propertyPrice and then adding them together to get the total.


 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please UseCodeTags, it makes it a lot easier to read. You can go back and edit your post to add them.

Also, you talk about errors and exceptions. What are the exceptions you get? It is a lot easier to fix problems when you know what the problems are.
 
Nynaeve Ojeda
Greenhorn
Posts: 8
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error that I get is an ArrayIndexOutofBoundsException referring to the first line where I try to obtain data from the column[1] to add to the ArrayList propertyPriceList. Subsequently it does not like any reference to any column other than 0. It is my understanding that I have still failed in some way to access the column[1]. Once this happens nothing else is processed and my output file remains empty. As you requested I edited my previous post by surrounding the code with tags.

Thanks!
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you go back and fix the code tags please. You add both the open and close code tags before and after the code. The open tag should be before the code and the close tag should be after the code. see the link I posted above for examples.

Yes, an index out of bounds means that your array is shorter than you think it is. That means the split isn't working correctly. There could be some problems with your input (maybe a blank line, or text that doesn't follow the pattern you enter). The best thing to do is add some System.out.println() statements to show the text before you split it and show columns[0] after you split it.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He did use code tags. He also had the disable BB code box ticked. I have twice reenabled BB code in this thread and then the code was properly formatted.
 
Nynaeve Ojeda
Greenhorn
Posts: 8
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your help. As a noobie sometimes it is really difficult to figure out where we went wrong. Thank you for your guidance and the opportunity to learn more so that I was able to correct my code. Adding in the system.out.println() statements was very useful in showing me what the application was and was not doing. The application works now! Looking forward to my next assignment.
 
reply
    Bookmark Topic Watch Topic
  • New Topic