This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I'm trying to print out the 1st word from the 1st line of a text file, the 2nd word from the 2nd line and so on. If the nth line has less than n words, then print out the last word on the line.
My code so far is this:
To do what I want, I know I should use the split method found in the string class, but I'm unsure how to write the regex to do what I want to do.
You may not have to use regexps at all. Depending on how the "words" of a line are separated from each other, a StringTokenizer may do. But then, you don't have to use regexps with String.split - e.g., a regexp of " " will split at each space character.
This works fine to a point! when the next line only has for example 3 words. But num is greater than 3 I get "file input error" message. How do I print out the last word of that line?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35443
9
posted
0
Well, as it is, the code expects that line [n] has at least [n+1] words. You can get the length of the returned array through "line.split(...).length".
Gary Goldsmith
Ranch Hand
Joined: Mar 06, 2007
Posts: 30
posted
0
Sorry, but this has me stumped. I've tried the following:
I've tried other if statements but still get "File input error".
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35443
9
posted
0
I don't really understand what the code is supposed to do. E.g., "num" seems to be the same as "i" since it is incremented for each line - is that by design?
In general, the code should proceed until there are no more lines in the file, so you might have code like:
[ October 13, 2007: Message edited by: Ulf Dittmer ]
Gary Goldsmith
Ranch Hand
Joined: Mar 06, 2007
Posts: 30
posted
0
numOfWords is the number of lines the code should go through, so the first 20 lines only.
"num" is the line number, the idea of the code is to print out the 1st word from the 1st line of a text file, the 2nd word from the 2nd line and so on. So I increment num on each loop, which gives me the next line, "num" is then used as the number of words in from the start, i.e. if num is 3 then its line 3 and word 3 of that line. The problem is that if line 6 has 3 words then reading in the 6th word gives an error, and I'm trying to get it to print the last word of the line.
Gary Goldsmith
Ranch Hand
Joined: Mar 06, 2007
Posts: 30
posted
0
I've worked it out now, the problem was that when I was using the length of the line as the number to be the number of the last word! I forgot that it started at 0, so if there are 5 words on a line its actually 0, 1, 2, 3, 4. And I was saying get word number 5 which didn't exist! Thanks you for your help.
You code is incredibly inefficient. You execute a split(), to get an array result, just so that you can find out how many fields there are. Then you run a split(), to get an array result, for each index of the array.
Wouldn't it be better to call split() once, store the result array, and then iterate that result set?
The problem is that "File input error" is not informative about what sort of error has occurred. You've lost important information here: the name of the Exception, the error message, and the stack trace. All three of these can be printed easily with the following:
In the future, debugging will be much easier with this sort of info at hand.
"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.