• Post Reply Bookmark Topic Watch Topic
  • New Topic

Splitting a list of words from a file then placing into an array  RSS feed

 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I had a list of words in a file titled "words.txt" that is formatted like;
hello
how
are
you
today

And I wanted to separate those words after reading in the file using the split method, would I do something like this?




Would it work? Also how would I put this words into an Array? Thank you in advance
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Menjivar wrote:Would it work?

Did your program give you the output you expected ?

Additionally,

are you aware of what happens here ?
 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:
Andrea Menjivar wrote:Would it work?

Did your program give you the output you expected ?

Additionally,

are you aware of what happens here ?


Well, it just concatenates all of the words together ontoasinglelinelikethisthusrenderingmeconfusedandcomingonherehopingsomeonecouldshedsomelight
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you program loops through the file and reads it line by line.
This is what String words = fileScan.nextLine();  does

So, for example, in the first loop it should read "hello", next time it should read "how" and so on.
When you try to split, there is no "\n" in "hello". Additionally, the split method does not change the original content since strings are immutable (but thats a different topic altogether)
Next, the System.out.print simply prints the word.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to fill the words in an array, I would suggest that you create an ArrayList and add every word read into it within the loop.
Once the loop is complete, you could convert the ArrayList into a String array[] easily.

I would not advise loading the complete file in memory if you are dealing with large sized files.
 
Rajith Pemabandu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose you can do like this.

[Complete answer deleted]
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajith Pemabandu wrote:suppose you can do like this.


Hi Rajith, you might want to look at Dont-Code-Mill <-- Thats a link. I hope you understand that I have deleted your solution so that the original poster gets a better experience learning what should be a good solution rather than a complete baked solution.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Menjivar wrote:. . . Well, it just concatenates all of the words together ontoasinglelinelikethisthusrenderingmeconfusedandcomingonherehopingsomeonecouldshedsomelight
WelcometotheRanch Don'tworrywecanshedlotsoflightonyourproblems.

No, you are not concatenating anything; it has to do with the way you are printing the words, as Salvin has already told you.
What you were doing in the original code is to read each line in the file (in line 11).
Then you are splitting on the linefeed character (\n). Only use \n if somebody tells you they want the linefeed character. It is not the correct line end if you use Windows® (that is \r\n), and whatever nextLine returns won't have a line end sequence in anyway. Read about nextLine here; it says.
. . . the rest of the current line, excluding any line separator at the end.
So your line 13 doesn't do anything. It creates an array, yes, but since you didn't assign that array to anything, it vanishes into some sort of cyber‑limbo never to be seen again. Because you are splitting on something which isn't in the text in the first place, you will get a 1‑element array, which is hardly an improvement on the plain simple String. If you ever need the correct line end, use System.lineSeparator().
Then you are simply printing all the words without anything separating them, which is why you think you have got the concatenation.

I suggest you start by moving all that code out of the main method; reading from a file belongs in a method all by itself. Also close the Scanner after use. Always close Scanners and Formatters except those pointing to System.in System.out or System.err, which you mustn't close.
You can count the “tokens” in your file with a loop similar to what you already have. Rather than doing anything with what the Scanner returns, simply write count++; That is an alternative way to get your words into an array; once you have counted the file you will know what size the array will be and you can insert the words with
myArray[i++] = word;
or similar. Once you have done that, you will see how much better Salvin's suggestion to use a List is, assuming you know how to work a List. In fact, if you have a List working, who needs an array?

Don't attempt
System.out.println(myArray);
Use
System.out.println(Arrays.toString(myArray));
 
William Ng
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a tip: "The name a method is given is usually a hint of what it does". I want to qualify that with "Methods names that are rationally named".
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The names you have in that code also hint at some of the reasons for your confusion.

For example, on line 2, you assign the result of fileScan.nextLine() to a variable named words. Why "words" (plural)? Do the semantics of the name "words" (plural) match what the value it's assigned represents? It doesn't.  Given that each line in your input file is supposed to contain only ONE word, then the value returned by fileScan.nextLine() is just ONE word.  There's a mismatch there. It's like say "I have a cars in my garages." That confuses people and makes them wonder if you're actually very wealthy and have multiple cars in multiple garages or if you're simply grammatically-challenged and actually only have one car in one garage.

In reality, since your input file contains one word per line, then it stands to reason that every time you call fileScan.nextLine(), the line of text that you read in will contain only ONE word. Hence, a more appropriate variable name to use on line 2 would be just word or maybe nextWord.

Line 4 makes your confusion about what your program actually does even more apparent.  Calling split("\n") makes it look like you think you're reading in the entire file at one go and trying to split up all the words in the file across newline characters. We already know that's not what your program does though. Furthermore, the split() method will return an array of String but since you are not assigning that to anything, you are basically throwing away the result of the call. So you have multiple problems with line 4, the most important one being that it's not even needed for this program. The answer to the comment/question at the end of line 4 is: No, it doesn't work. It's not even needed.

On line 5, you are simply printing out the value of the variable words without adding a newline after it. This means that the next time you call print(words), the output will immediately follow the output from the previous call.  As others have already informed you, you are not actually concatenating any Strings here. You are simply printing out the values that you read from the file, one after another, without anything else to create visual separation. It's the difference you see in the following:
which prints

This is going to beon the same line

(notice there's no space between "be" and "on" in the output)

Versus this:
which prints

These will be
on different lines


Lastly, an array would be appropriate to use if you knew how many words there are in the file beforehand. However, since you're using a while-loop with a condition of (fileScan.hasNext()) to process the words you read from the file, this implies that the number of words in it can vary and is not known beforehand. Therefore, an array would not be the appropriate structure to use to store the words you read inside the loop.  A List<String> would be more appropriate.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!