Results 1 to 5 of 5
  1. #1
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default 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!

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Hey, i usually don't like handing out code, but i enjoyed doing this one.

    Java 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.

  3. #3
    afsina is offline Member
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    there is an easier way of doing that.

    long number = Long.parseLong(str.replaceAll(",",""));

  4. #4
    afsina is offline Member
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by gibsonrocker800 View Post
    Hey, i usually don't like handing out code, but i enjoyed doing this one.

    Java 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
    Java 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.

  5. #5
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    long number = Long.parseLong(str.replaceAll(",",""));
    This is the best one ... :). A roasted lamb ..
    dont worry newbie, we got you covered.

Similar Threads

  1. Primitive data types of Java
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:29 PM
  2. Displaying different image types
    By splinter64uk in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2007, 09:12 AM
  3. Comparing types, integer with null
    By Felissa in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •