• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read specified portion text, from text file  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This kind of scanners, scans everything.




However I need to modify this code somehow.
Text file "bag" contains.
On tree grows fruit: apples 11
In the shop lays fruit: bananas 6
Outside grows fruit: berries 5


And this is what I want get to console.
apples 11
bananas 6
berries 5


** Not modifying text file, there should be some kind of code settings.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could just do a split on  ":" after reading a line.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or find the index of ":" and take a substring.
Or use scanner1 to read each line. Pass the line to scanner2 using this constructor. Set scanner2's delimiter to ":" and call next() twice. Make both the Scanners local variables, then there will be no garbage collection issues. The delimiter is a regular expression. If you look in the Java™ Tutorials and scroll to the bottom of the page, you find that : is not a metacharacter, so you can use ":" without a \.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The console shows error:
Return Value :Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -9
at java.lang.String.substring(Unknown Source)
at sprendimai.ScanerisIrprinteris.main(ScanerisIrprinteris.java:27)

 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the documentation for substring and see whether it says anything about that sort of Exception.
Don't use Capital Letters for variables. Not Str but str.
Don't use new String(":"). Use = ":"
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:

Problem: Whenever you use a 'next....' method, you should use its associated 'hasNext...' method.
What do you think that might be?

Winston
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Problem: Whenever you use a 'next....' method, you should use its associated 'hasNext...' method.
What do you think that might be?

Winston


???
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should only call the next method if the thing you are iterating through actually has a next item to get.

Also, i would suggest that variables like "om" and "str" are poor choices for variable names.  Your not IMing someone. You have a full keyboard. Use descriptive names so you know what the variable represents.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:You should only call the next method if the thing you are iterating through actually has a next item to get.

Also, i would suggest that variables like "om" and "str" are poor choices for variable names.  Your not IMing someone. You have a full keyboard. Use descriptive names so you know what the variable represents.


Yet I dont write projects, nor doing some kind of homewok plan.

Im learning and writing short codes. They are common and generic, in overall expired of imagination.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem: Whenever you use a 'next....' method, you should use its associated 'hasNext...' method. 

It's not very clear from the documentation, but you need to pair the hasNextXXX() method with its corresponding nextXXX() method.  So if you check for hasNext(), you need to get with next().  If you check with hasNextLine() you need to get with nextLine() and so on.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
fred rosenberger wrote:Also, i would suggest that variables like "om" and "str" are poor choices for variable names.  Your not IMing someone. You have a full keyboard. Use descriptive names so you know what the variable represents.


Yet I dont write projects, nor doing some kind of homewok plan.

Im learning and writing short codes. They are common and generic, in overall expired of imagination.

Even if this is a personal project, you should get in the habit of using descriptive variables names.  It may even help yourself in the future!
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet I struggle to learn basic codes to write, its too far for me to start project. And the porblem is that no one solved my task.
Java docs provide inacurate codes, or somehow complex algorith with "try.catch" what I have not learned.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, what's the problem now?  You're having trouble with a try/catch?  Can you post the code?
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Okay, what's the problem now?  You're having trouble with a try/catch?  Can you post the code?


http://docs.oracle.com/javase/tutorial/essential/io/file.html

I have not learned it yet.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
Knute Snortum wrote:Okay, what's the problem now?  You're having trouble with a try/catch?  Can you post the code?


http://docs.oracle.com/javase/tutorial/essential/io/file.html

I have not learned it yet.


Then you shouldn't be writing programs which use files, because they need to use exceptions. Perhaps you're getting ahead of yourself in whatever you're learning from?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you're using File() just fine in your program. 
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:But you're using File() just fine in your program. 


If I run the program, console will display:

On tree grows fruit: apples 11
In the shop lays fruit: bananas 6
Outside grows fruit: berries 5


What I want to get?
This is what I want to get in console, without modify text file:

apples 11
bananas 6
berries 5


------------------------------------------
Text file contains text:

On tree grows fruit: apples 11
In the shop lays fruit: bananas 6
Outside grows fruit: berries 5



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:If I run the program, console will display:
On tree grows fruit: apples 11
....

What I want to get? This is what I want to get in console, without modify text file:
apples 11
...

OK, that's a good description of what you want to see, but you actually have TWO problems:
1. You need to read lines from a text file.
2. You need to extract the above information from EACH line.

It appears that you've solved the first problem, but you're having problems with the second one. So, eliminate the first one and concentrate on the second.

My suggestion: Write a completely new program that looks something like this:Do you see the idea?

Now you have a program that deals specifically with the problem you want to solve, and you can test it with any "sample" line you like by simply changing the literal for 'line' and running it.

You've already been given several suggestions as to how to do it, so I suggest you re-read the previous posts in this thread

And as soon as this program works - every time - you can take its logic and copy it into your other one.

It's called "problem isolation", and its a technique we all use when we have problems.

HIH

Winston
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Providing current stance.

And how result should look like.
first.jpg
[Thumbnail for first.jpg]
2nd.jpg
[Thumbnail for 2nd.jpg]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john (assumed via image) wrote:I need some kind of arguments / parameters. To make scanner scan only desired part of text file.


Since you are currently learning Java, I recommend following what others have already stated. Treat the task as two separate tasks. Use the Scanner to read in a single line, and then parse that line for the part that you want.


Yes, the scanner class is capable of doing what you want, as it is built with access to the regular expression engine (that is being used to scan for the tokens). However, regexes is *not* a beginner topic. So, unless Java is not your first language, and you already know regular expressions from previous experience, using the scanner to do this is not recommended here.

Henry
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You could just do a split on  ":" after reading a line.

Campbell Ritchie wrote:Or find the index of ":" and take a substring.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Knute Snortum wrote:You could just do a split on  ":" after reading a line.

Campbell Ritchie wrote:Or find the index of ":" and take a substring.




I have Inserted ":" in 16th line. Now console is completely empty, no errors too.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
split() returns a result that you are throwing away. I suggest reading up on split() in the API javadocs.
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:split() returns a result that you are throwing away. I suggest reading up on split() in the API javadocs.
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-


I dont understand, not even single example.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does this return?

public String[] split(String regex)

In your case the regex would be ": ". This will split the string when it encounters a colon followed by a space.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:What does this return?

???


public String[] split(String regex)

In your case the regex would be ": ". This will split the string when it encounters a colon followed by a space.

Where?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See: http://www.homeandlearn.co.uk/java/java_methods.html

From this you should get the basic layout for specifying a method. Specifically you should note how the return type is declared. From that you should be able to say what the return type of this is...

public String[] split(String regex)

I just clipped this out of the Java API documentation. As you go forward you'll need to be able to interpret these documents, maybe not all of it, but certainly the signature of a method (i.e. what parameters it takes and what type it returns).

Have you learned arrays yet? Perhaps you're not up for this yet.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much have you looked at the methods of the String class? Another approach would be to use the indexOf() and substring() methods.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a simple code that I wrote in about ten minutes (I know it's a lot, but I'm still a beginner). It doesn't solve your problem, but if you understand it, and you can apply the logic, I'm sure you can figure out the solution to your problem.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:Here is a simple code that I wrote in about ten minutes (I know it's a lot, but I'm still a beginner). It doesn't solve your problem, but if you understand it, and you can apply the logic, I'm sure you can figure out the solution to your problem.

My hero.

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really need to correct your formatting, especially your while loop.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!