Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 January 30th, 2013, 10:25 AM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
Arrow (Java Newbie) Convert first few characters of string to double

Let's say I have a string:

Code:
String s1 = "90:39.2";


I would like to parse all the numbers up to ":" into a double, in this case 90.

I can find the index of ":" by using:

Code:
int n = s1.indexOf(":");


But I am not sure how to 'remove' the following numbers / characters before using this code to parse the string:

Code:
double result = Double.parseDouble(StringUpToColon);

Reply With Quote
  #2  
Old January 30th, 2013, 01:43 PM
stdunbar's Avatar
stdunbar stdunbar is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: May 2004
Location: Superior, CO, USA
Posts: 2,398 stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 18 h 14 m 16 sec
Reputation Power: 1660
Send a message via Yahoo to stdunbar Send a message via Google Talk to stdunbar
Building on the code you have you'd want to do something like:

java Code:
Original - java Code
  1. String s1 = "90:39.2";
  2. int n = s1.indexOf(":");
  3.  
  4. String firstPart = null;
  5. if( n > -1 )
  6.     firstPart = s1.substring( 0, n );
  7. if( firstPart != null )
  8.     double result = Double.parseDouble(firstPart);
__________________
Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.

Reply With Quote
  #3  
Old January 31st, 2013, 03:30 AM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
Substring, that's what I needed. Thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > (Java Newbie) Convert first few characters of string to double

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap