Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 29th, 2003, 05:18 PM
mr_mcoy mr_mcoy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 4 mr_mcoy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
extracting int from string

Hi im tryin to extract int values from a StringBuffer looking something like this for instance:
"|5|56|789|4|21"
would this code work?
-----------------------------------------------------------------
//Buff is the StringBuffer where the string is stored

int start=0;
int end=0;
int invalues[]=new int[5];
for(int i=0; i<invalues.length; i++) {
start=buff.indexOf("|",start);
end=buff.indexOf("|",start);
String mini=buff.substring((start+1),end);
int convInt= new Integer(mini).intValue();
invalues[i]=convInt;

}


----------------------------------------------------------------------

Reply With Quote
  #2  
Old April 30th, 2003, 01:18 AM
Pixel_Damage Pixel_Damage is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 6 Pixel_Damage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Why dont you just use StringTokenizer???

Reply With Quote
  #3  
Old May 2nd, 2003, 03:37 PM
mr_mcoy mr_mcoy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 4 mr_mcoy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by Pixel_Damage
Why dont you just use StringTokenizer???

because i dont know how to use it and im in a hurry!
however my question was if the code i wrote would work properly and i would apriciate if someone would correct it

Reply With Quote
  #4  
Old May 6th, 2003, 05:53 PM
FReAK La Marsch FReAK La Marsch is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 6 FReAK La Marsch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb maybe that way?

First, why are you askin if this code would work? Just test it.

Well, I just played a littlebit with your code and thats my result

PHP Code:
 StringBuffer buff = new StringBuffer().append("|5|56|789|4|21|");
int invalues[]=new int[5];
int start=0;
int end=0;
for(
int i=0i<invalues.lengthi++)
{
    
start buff.indexOf("|",end);
    
end buff.indexOf("|",start+1);
    
String mini buff.substring((start+1),end);
    
int convInt Integer.parseInt(mini);
    
invalues[i]=convInt;



Note that I have added an | at the end of the buffer to prevent a -1 for the last value of end.
I guess you ar using Java 1.4 because you are using StringBuffer.indexOf so why don't you String.split ?

Have a nice day,
FReAK

Reply With Quote
  #5  
Old May 8th, 2003, 08:01 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
heh , i was in a hurry too , for something similar ...
it took about 5 minutes through a book , thanks a lot pixel

Reply With Quote
  #6  
Old May 9th, 2003, 02:04 AM
walter76 walter76 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 18 walter76 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
O.k., you said you're in a hurry, but if you instead want to use StringTokenizer, try this:

Code:
StringTokenizer tok = new StringTokenizer(buff, "|");
int i=0;
int intvalues[] = new int[5];

while(tok.hasMoreTokens() {
  intvalues[i]=Integer.parseInt(tok.nextToken());
  i++;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > extracting int from string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway