I have a scanner which needs to read a recipe text file, specifically line 3.
I want to read all of the words in that line using a delimiter (which I already know how to use).
Though I don't know how to make the scanner stop before going to the next line.

Sample text file.


I'm basically trying to make a counter of how many ingredients I have.

6 Years
Discussion Span
Last Post by Aviras

If the things you want are always on the third line, you can just let the scanner read the first two lines without saving it in a String, then just put the third line in a string, and there you have it? Then you just split the string using your delimiter, and count the String array length.

Personally I don't have much experience using a Scanner as a filereader since I usually use BufferedReader, but it shouldn't be a problem really.

Your Scanner/BufferedReader always has to read the file top to bottom, so you can't jump to say line X without reading the previous ones.

Edited by Aviras: n/a


Your Scanner/BufferedReader always has to read the file top to bottom, so you can't jump to say line X without reading the previous ones.

Thanks for the great explanation, though I would like to ask you for a bit of guidance doing this.
I am basically using two delimiters inside of the file, and one line instead of 4, etc.. ( more will be added).

The text file

Cereal%author%strawberry$grapes%3$2.5$3.0$4$1%Place the amount of cereal specified in a bowl, now ...%

I'm reading the name of the recipe, the author, and then I read the ingredientsName, and I'm assinging it to a string.
From this string I receive strawberry$grapes

I now made a temporary string array which is null, then I split the ingredientsName String and assign it to the temporary string array but I still get the same result.

The way I'm printing it

        for(int i=0;i<tempIngr.length;i++)

Edited by mike_2000_17: Fixed formatting


First, you don't have to make a new String array and initialize it for null.
Let me give you a brief overview in pseudo code

String fullLine = in.readLine(); // with in being your scanner/BufferedReader
String[] delim1 = fullLine.split(/*your first delimiter*/);
String[] delim2 = delim1[/*the index you want].split(/*your second delimiter*/); 
// I'm not exactly sure what you want to do here, split the full or the already previously splitted.

Is printthis(String str) a method you created yourself? Assuming it's a console application, you can just use

for(int i=0;i<yourArray.length;i++){

As a side note, a nice addition to Java a couple versions back is another version of the for loop, being like this:

for(String s:yourArray){

It's equivalent to the one posted above, it's just a little less cumbersome with the indices, handy when there's more complex situations.

It's possible I haven't fully understood what you wanted to do, if so, I'll try to better understand it next time. It's getting a bit late and I'm growing a bit tired, but I hope I helped you in some way.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.