How to find out if a long String has a "subString" twice or more.

I need to find out if a long String has the same number twice or more.

I need to look matches for numbers running from 000, 001....999 and if a number is found twice or more, return that number and lines there were found.

example String:

-;000 ; 1 ; 2006-12-11 ; -; job;

x;001 ; 2 ; 2006-12-11 ; 2006-12-12; do this

-;002 ; 3 ; 2006-12-11 ; -; work

-;003 ; 0 ; 2006-12-11 ; -; some

-;004 ; 2 ; 2006-12-11 ; -; thing

x;005 ; 1 ; 2006-12-11 ; 2006-12-11; reads

-;003 ; 0 ; 2006-12-11 ; -; here

Should return from example String:

003 at lines 4 and 7

Any ideas?

[638 byte] By [Jakepaloa] at [2007-11-26 13:11:17]
# 1
one way would be to have a StringTokenizer that uses the substring in question as the delimiter
georgemca at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 2
> Any ideas?Go through the file line by line adding the (number, line number) pairs you find to a Map. If when you go to put a number in the Map, if it is already there than you can print out both pairs.
sabre150a at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 3
You could use regular expressions as well I think. The matcher class has overloaded find() methods that may do the trick.
Tillermana at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 4
Nevermind, sabre150's idea is better.Message was edited by: hunter9000
hunter9000a at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 5
> > Go through the file line by line adding the (number,> line number) pairs you find to a Map. If when you go> to put a number in the Map, if it is already there> than you can print out both pairs.thanks for ideas I try the one sabre150
Jakepaloa at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 6
You can use indexOf and lastIndexOf and check if those return different. If so, then there are two or more of the same strings in the input.
atoxica at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 7
Use the StringTokenizer class for this low-level string manipulation process.
GhostRadioTwoa at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 8
> You can use indexOf and lastIndexOf and check if> those return different. If so, then there are two or> more of the same strings in the input.Thanks!this idea works. but I don't have line information.
Jakepaloa at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 9
> one way would be to have a StringTokenizer that uses> the substring in question as the delimiterThis won't work! The delim parameter of StringTokenizer is treated as a set of separator chars, each of them acting as a delimiter!
Horst_Ma at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 10

So there are newlines in the String?

You could use a StringTokenizer to break the String into lines, then searching on each line if it contains any of the search strings. (You need to clarify if a line can contain more than one search string).

Probably you should use a Map<String, Integer> to record the searchcounts.

Or an int[] Array if you are really sure that the Strings you search for really are numbers.

Another option is to use:

LineNumberReader lnr = new LineNumberReader(new StringReader(searchString));

This will save you from explicitly having to take care for the line number.

In any case your example looks like the individual lines are semicolon separated fields and the numbers you search for always are in column two.

So after breaking up the original String in lines, you could use another StringTokenizer to break up the line in fields.

Horst_Ma at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 11
> Probably you should use a Map<String, Integer> to> record the searchcounts.Well, this works if you are only interested in the counts.if you want to record the line number as well, you needMap<String, List><Integer>>.
Horst_Ma at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 12
Bloody code tag!What i posted wasMap<String, List><Integer>>.but it came out as above!
Horst_Ma at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 13
Well it wasn't the code tag!Let's try this:Map<String,List<Integer>>
Horst_Ma at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...
# 14
> Map<String,List<Integer>>That came out ilike i orginally intented it. Crappy forum!
Horst_Ma at 2007-7-7 17:26:34 > top of Java-index,Java Essentials,Java Programming...