Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-26-2008, 11:44 PM
Member
 
Join Date: Apr 2008
Posts: 9
hiklior is on a distinguished road
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());
}
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-27-2008, 04:21 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 372
fishtoprecords is on a distinguished road
you are not iterating through the length of the string.

Code:
while (strLine.length() >= 5) { int var1 = Integer.parseInt(strLine.substring(0, 5)); ... }
needs something like

Code:
for (int i = 0; i < strLine.length(); i += 5) { String sub = strLine.substring(i, i+4); int var1 = Integer.parseInt(sub); }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-27-2008, 05:01 AM
Senior Member
 
Join Date: Jun 2008
Posts: 472
Fubarable is on a distinguished road
Cross-post
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-27-2008, 05:11 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 372
fishtoprecords is on a distinguished road
cross to separate help sites!?!?!
Wow, what a jerk
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-27-2008, 05:48 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,604
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-27-2008, 06:19 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,191
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-27-2008, 07:40 AM
Alan-LB's Avatar
Member
 
Join Date: Jun 2008
Location: Junee, NSW, Australia
Posts: 19
Alan-LB is on a distinguished road
Send a message via Yahoo to Alan-LB
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!!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-27-2008, 11:56 AM
Member
 
Join Date: Jun 2008
Posts: 7
dlorde is on a distinguished road
Quote:
Originally Posted by Eranga View Post
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-27-2008, 12:40 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,604
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-28-2008, 07:23 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 372
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-28-2008, 07:26 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,604
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 06-28-2008, 07:36 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 372
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 06-28-2008, 07:48 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,604
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i get substring 82rathi.angara New To Java 14 06-27-2008 05:35 AM
How to use subString method Java Tip java.lang 0 04-17-2008 08:44 PM
help me with a realy easy program (substring) michcio New To Java 7 01-27-2008 01:41 AM
String substring function ravian New To Java 6 01-02-2008 08:35 PM
substring Java Tip Java Tips 0 11-11-2007 09:15 PM


All times are GMT +3. The time now is 06:51 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org