Results 1 to 13 of 13
Thread: Help with substring
- 06-26-2008, 11:44 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
Help with substring
Example
000520000000023000210000000089
I am fairly new to java and i would appreciate if somebody could help me with this. I have series to numbers just like above . I need to divide this series of numbers into chunk of 5 numbers and find a chunk that has 5 zeros preceeded by or followed by chunk of five numbers greater than or equal to 50.
Lets divide the above example into chunk
00052 00000 00023 00021 00000 00089
As you could see 00052 ( which is greater than 50 ) is followed by 00000 it meets our criteria so print 00000
Also 000089 has 00000 in front of it also meets our criteria.
I would appreciate if anybody could help me with this.
Hik
This is what i have upto now and i get error message saying "out of bound". I would also like to credit Chris.Brown SPE for this code
import java.util.regex.*;
import java.io.*;
class readlinebyline2
{
public static void main(String args[])
{
try{
FileInputStream fstream = new FileInputStream("c:/input.doc");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
while (strLine.length() >= 5)
{
int var1 = Integer.parseInt(strLine.substring(0, 5));
strLine = strLine.substring(5);
int var2 = Integer.parseInt(strLine.substring(0, 5));
if (var1 > 50 & var2 == 0)
{
System.out.println(var1);
}
else
{
if (var1 == 0 & var2 > 50)
{
System.out.println(var2);
}
}
}
}
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
- 06-27-2008, 04:21 AM #2
you are not iterating through the length of the string.
Java Code:while (strLine.length() >= 5) { int var1 = Integer.parseInt(strLine.substring(0, 5)); ... }
Java Code:for (int i = 0; i < strLine.length(); i += 5) { String sub = strLine.substring(i, i+4); int var1 = Integer.parseInt(sub); }
-
- 06-27-2008, 05:11 AM #4
cross to separate help sites!?!?!
Wow, what a jerk
- 06-27-2008, 05:48 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
You can use String Tokernizer class too. I really like that. :)
- 06-27-2008, 06:19 AM #6Java Code:
public class SubstringTest { public static void main(String[] args) { String s = "000520000000023000210000000089"; String[] blocks = new String[s.length()/5]; for(int i = 0, pos = 0; i < blocks.length; i++) { blocks[i] = s.substring(pos, pos+5); pos += 5; } for(int i = 0; i < blocks.length; i++) { if(qualifies(i, blocks)) { System.out.printf("blocks[%d] = %s%n", i, blocks[i]); } } } private static boolean qualifies(int index, String[] blocks) { boolean before = (index == 0) ? false : Integer.parseInt(blocks[index-1]) >= 50; boolean after = (index == blocks.length-1) ? false : Integer.parseInt(blocks[index+1]) >= 50; return before || after; } }
- 06-27-2008, 07:40 AM #7
The posting in the other forum is different!!!
Alan.There are 10 types of people - those who understand binary and those who don't!!
Today is the Beta version of Tomorrow!!
- 06-27-2008, 11:56 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 13
- 06-27-2008, 12:40 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
As far as I know it's used around 1.0 and now I'm working on 1.6. But still not found such thing on that. I used in more cases. Major reason is it's much simpler in simple applications.
- 06-28-2008, 07:23 AM #10
StringTokenizer is expected to be deprecated soon, so String.split is much prefered, and its not much more complex, split() also has the advantage of not needing the enumeration pointer, which is a bit klunky.
- 06-28-2008, 07:26 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Do you know any reason to deprecate it? As said, use of split() is not complex. What I want to say is, depend on the application complexity I choose one of them. StringTotkenizer is easy to use in simple cases.
- 06-28-2008, 07:36 AM #12
Sorry, no. Perhaps it doesn't work well in parallel threads, this is just a guess, I don't try to keep up with the JSR process.
Its a bit ugly, and enumerations are 'disfavored' and the iterator is preferred these days.
- 06-28-2008, 07:48 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
I agreed. In few months back I try to do it an application, which are working with parallel threads. It freeze the application because of the heap size issues and so. I think may be it have keep all tokens to workout.
Similar Threads
-
How can i get substring
By 82rathi.angara in forum New To JavaReplies: 14Last Post: 06-27-2008, 05:35 AM -
How to use subString method
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 08:44 PM -
help me with a realy easy program (substring)
By michcio in forum New To JavaReplies: 7Last Post: 01-27-2008, 01:41 AM -
String substring function
By ravian in forum New To JavaReplies: 6Last Post: 01-02-2008, 08:35 PM -
substring
By Java Tip in forum Java TipReplies: 0Last Post: 11-11-2007, 09:15 PM
Bookmarks