This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I can't get useDelimiter to cooperate

 
Luke Heenan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the actual output you are looking for, with above data?
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Luke Heenan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This ended up working:

s.useDelimiter("Event=|,(.)+[\\r\\n]*Event=|,(.)+[\\r\\n]*");
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic