Results 1 to 6 of 6
  1. #1
    pratheshbv is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default How to split the integer

    Hi I am trying to split an integer for example my input is 0590
    I want the output as
    0
    5
    9
    0
    How to do this please suggest.
    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: How to split the integer

    How are you doing it without a computer? Write down rules that even an idiot could follow, and you'll have an algorithm that you can then start thinking about translating into code.

    Hint: You aren't really treating it like a number, are you? You're treating it more like a *String* of digits.

    Alternatively, you could use mod and division to extract the numbers.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pratheshbv is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default Re: How to split the integer

    Ya i have dome by using mod and division , The issue here is its not taking first digit as 0 in 0590 . Im getting the result as
    5
    9
    0

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: How to split the integer

    ints are stored in the computer's memory with lots of leading zeros. When an int is converted to a String ALL the zeros are removed. Otherwise an int could display like this: 0000000590

    Post the code you are having problems with. How are you entering the value: 0590?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    pratheshbv is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default Re: How to split the integer

    Here is the code
    Java Code:
    String Str = "abc010590";
    		int STR1 = Integer.parseInt(Str.replaceAll("[\\D]", ""));
    		System.out.println(STR1);
    
    		if (STR1 < 0)
    			STR1 = -STR1; 
    		List<Integer> digits = new LinkedList<Integer>();
    
    		while (STR1 > 0) {
    			digits.add(0, STR1 % 10);
    			STR1 = STR1 / 10;
    		}
    		int a = digits.get(0);
    		int b = digits.get(1);
    		System.out.println(a);
    		System.out.println(b);

    Result
    1
    0
    Last edited by pratheshbv; 07-09-2014 at 09:09 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: How to split the integer

    Did you understand what I said about how computers store int values and leading 0s?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2013, 08:49 PM
  2. How to split an integer into separate integers
    By jsoeung in forum New To Java
    Replies: 2
    Last Post: 04-23-2012, 08:12 AM
  3. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 09:59 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 09:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:32 PM

Posting Permissions

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