5

Let's say I have following number:

36702514

I want to separate the above number into 3 parts, which is 36, 702, 514.
Below is the code that I tried:

int num = 36702514;
int num1 = num.substring(0, 2);
int num2 = num.substring(2, 3);
int num3 = num.substring(5, 3);

Am I writing correct?

Cœur
  • 37,241
  • 25
  • 195
  • 267
KKL Michael
  • 795
  • 1
  • 13
  • 31
  • modulo 1000 or 100 would be the easiest way. – SomeJavaGuy Jul 16 '15 at 06:03
  • 2
    Lets say if you would have tried something before asking? – SMA Jul 16 '15 at 06:03
  • @KKLMichael Are we talking about this specific example or any number split into any number of permutation combinations? If you mean the later, one quick way to do it would be to use `String` manipulation. – Chetan Kinger Jul 16 '15 at 06:05
  • It depends on what the data type is. If its a string look up using the string split operation if its an int you could use the % operator to separate it. – Ittociwam Jul 16 '15 at 06:07
  • search can help http://stackoverflow.com/questions/14495712/how-to-split-numbers-in-java – xedo Jul 16 '15 at 06:09
  • 1
    @KKLMichael Did you just copy paste someones answer into your question and claim that you tried it? This is absurd :) – Chetan Kinger Jul 16 '15 at 06:14

4 Answers4

7
List<String>  myNumbers = Arrays.asList(
    NumberFormat.getNumberInstance(Locale.US).format(36702514).split(","));

My solution builds a comma-separated String of the input in US locale:

36,702,514

This String is then split by the comma to give the desired three pieces in the original problem.

List<Integer> numbers = new ArrayList<>();
Integer n = null;
for (String numb : myNumbers)
{
    try
    {
        n = new Integer(numb);
    }
    catch (NumberFormatException e)
    {
        System.out.println("Wrong number " + numb);
        continue;
    }
    numbers.add(n);
}
System.out.println(numbers);
  • 1
    I voted up your answer because it is clean and concise. The only thing I don't like is that you end up with a collection of strings which potentially have to be converted to numeric. – Tim Biegeleisen Jul 16 '15 at 06:38
  • 1
    I also voted up because this is a method you can use if String is workable for you. I would urge you to flesh it out a little and explain how it works so the people using it can understand what it does. – Gemtastic Jul 16 '15 at 06:41
5

You need to use substring() on a Java String, not a primitive int. Convert your number to a String using String.valueOf():

int num = 36702514;
String numString = String.valueOf(num);

int num1 = Integer.parseInt(numString.substring(0, 2));
int num2 = Integer.parseInt(numString.substring(2, 5));
int num3 = Integer.parseInt(numString.substring(5, 8));
R9J
  • 6,605
  • 4
  • 19
  • 25
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You can use the modulus logic to extract the digits from an integer.

int num1 = num % 10;
int num2 = num / 10 % 10;
int num3 = num /100 % 10;

System.out.print(num1);
System.out.print("\n" + num2);
System.out.print("\n" + num3);

Edit : Store the digits in an array instead of seperate variables.

HRG
  • 182
  • 5
  • 16
-3

Am I writing correct?

No you aren't , you'll be getting compile time error as

error: int cannot be dereferenced

num is a primitive here , You can not call a method on primitive .

Correct way is :

int no = 36702514;
String num=String.valueOf(num);
int num1 = Integer.parseInt(num.substring(0, 2));
int num2 = Integer.parseInt(num.substring(2, 3));
int num3 = Integer.parseInt(num.substring(5, 3));
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62