|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-26-2008, 11:44 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 9
|
|
|
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
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 372
|
|
you are not iterating through the length of the string.
while (strLine.length() >= 5) {
int var1 = Integer.parseInt(strLine.substring(0, 5));
...
}
needs something like
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:01 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 472
|
|
|
|
|

06-27-2008, 05:11 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 372
|
|
|
cross to separate help sites!?!?!
Wow, what a jerk
|
|

06-27-2008, 05:48 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,604
|
|
You can use String Tokernizer class too. I really like that. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-27-2008, 06:19 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,191
|
|
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
|
 |
Member
|
|
Join Date: Jun 2008
Location: Junee, NSW, Australia
Posts: 19
|
|
|
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
|
|
Member
|
|
Join Date: Jun 2008
Posts: 7
|
|
Originally Posted by Eranga
You can use String Tokernizer class too. I really like that. 
Sun now discourage the use of StringTokenizer and recommend String.split(..) or the regex package. See the StringTokenizer JavaDocs.
The best way to get a good idea is to get a lot of ideas...
L. Pauling
|
|

06-27-2008, 12:40 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,604
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-28-2008, 07:23 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 372
|
|
|
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
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,604
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-28-2008, 07:36 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 372
|
|
|
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
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,604
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|