my dog learned polymorphism*
The moose likes Beginning Java and the fly likes I can't get useDelimiter to cooperate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I can Watch "I can New topic
Author

I can't get useDelimiter to cooperate

Luke Heenan
Greenhorn

Joined: Dec 08, 2013
Posts: 2
I am working on a Java project and I am trying to get useDelimiter to remove everything except the text in between "=" and ",". For example, on the first line of the file I would like to keep "ThermostatNight".

This is what the text file looks like:

Event=ThermostatNight,time=0

Event=LightOn,time=2000


I've been able to do the exact opposite using this code:

s.useDelimiter("=(.*?),");

Is there any way I can tweak this to do the opposite?

Edit: The "s" is a scanner.
Krishna Kanth
Ranch Hand

Joined: Oct 23, 2006
Posts: 49
    
    1

What is the actual output you are looking for, with above data?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Luke Heenan wrote:I am working on a Java project and I am trying to get useDelimiter to remove everything except the text in between "=" and ",". For example, on the first line of the file I would like to keep "ThermostatNight".

Why useDelimiter? That sounds like you've decided HOW to do it, before you've decided WHAT you want to do, which is almost always a bad methodology.

So: WHAT do you want to do? And explain in English, not Java.

However, just as a kick-start, you might look want to look at String.indexOf().

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Luke Heenan
Greenhorn

Joined: Dec 08, 2013
Posts: 2
This ended up working:

s.useDelimiter("Event=|,(.)+[\\r\\n]*Event=|,(.)+[\\r\\n]*");
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Well done, and welcome to the Ranch

There are probably more elegant ways to divide that text than useDelimiter. Try indexOf("=") and substring() and similar, as Winston has already told you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: I can't get useDelimiter to cooperate
 
Similar Threads
Counting one character at a time from a file
Trouble with jr.jar
How to Avoid Redundant State Changes in the Observer Pattern
display contents of a text file in a new window
Events: onblur events kills onclick event some time vice versa