Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 01-11-2008, 11:06 PM
Member
 
Join Date: Jun 2007
Posts: 14
bluekswing is on a distinguished road
Converting data types
I'm trying to convert string representations of numbers that contain commas into integers.

For example, converting

80,000
125,000
8,000,000,000


from their string representation into an int data type.

I have an ugly brute force way of doing it by breaking up the "string" into individual characters, delete the commas and concantenate it all back together and then convert to int. It's ugly - anyone know of a better way, preferably a native method in java that will perform the conversion?

Thanks a bunch!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-11-2008, 11:55 PM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Hey, i usually don't like handing out code, but i enjoyed doing this one.

Code:
import java.util.ArrayList; public class Comma { public static void main(String[] args) { String num = "5,000,000"; char[] chars = num.toCharArray(); ArrayList<Character> charList = new ArrayList<Character>(); for(int i = 0; i < chars.length; i++) { if((chars[i] + "").equals(",")) continue; else charList.add(chars[i]); } for(char e : charList) { System.out.print(e); } } }
If you are unfamiliar with arrays and ArrayLists and loops, then you should learn about them. You will be using them ALOT in programming.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-12-2008, 12:46 AM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
there is an easier way of doing that.

long number = Long.parseLong(str.replaceAll(",",""));
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-12-2008, 12:53 AM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
Quote:
Originally Posted by gibsonrocker800 View Post
Hey, i usually don't like handing out code, but i enjoyed doing this one.

Code:
import java.util.ArrayList; public class Comma { public static void main(String[] args) { String num = "5,000,000"; char[] chars = num.toCharArray(); ArrayList<Character> charList = new ArrayList<Character>(); for(int i = 0; i < chars.length; i++) { if((chars[i] + "").equals(",")) continue; else charList.add(chars[i]); } for(char e : charList) { System.out.print(e); } } }
If you are unfamiliar with arrays and ArrayLists and loops, then you should learn about them. You will be using them ALOT in programming.
why not
Code:
for(int i = 0; i < num.length(); i++) { if(num.charAt(i)!=',')) charList.add(chars[i]); }
still the overall approach is bad, but you should use the convenient ways.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-12-2008, 02:48 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
Code:
long number = Long.parseLong(str.replaceAll(",",""));
This is the best one ... . A roasted lamb ..
__________________
dont worry newbie, we got you covered.
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
Primitive data types of Java Java Tip Java Tips 0 03-28-2008 09:29 PM
Types of JDBC drivers JavaForums Java Blogs 0 12-25-2007 03:21 PM
Displaying different image types splinter64uk AWT / Swing 1 12-05-2007 10:12 AM
Using enum types (with example) JavaForums Java Blogs 0 11-17-2007 04:43 AM
Comparing types, integer with null Felissa New To Java 1 07-05-2007 08:32 AM


All times are GMT +3. The time now is 01:39 PM.


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