-2

I'm having trouble getting the individual digits of a number. you have simple integer variable containing 1234, How do you get the individual digits of the number for example: int s = 1234;

I need to store every digit in a separate variable like

String str1  = "1";
String str2 = "2";
String str3 = "3";
String str4 = "4";

Is it possible? how can I implement this? In above method I gave one static integer value. could you please tell me both static and dynamic please guide me

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • 1
    Is this homeworks ? It looks like this... Please show us what you tried so far. – mithrop Oct 22 '14 at 10:45
  • @mithrop i have an integer number, its come from server whether the value is 3 digit or four digit, i need to get every number and stored into separate variable this is the concept i am not getting any idea that is what i posted like this – user3728511 Oct 22 '14 at 10:52
  • possible duplicate of [How to get the separate digits of an int number?](http://stackoverflow.com/questions/3389264/how-to-get-the-separate-digits-of-an-int-number) – Kaushik Oct 22 '14 at 10:54
  • @user3728511 : please [search in google](https://www.google.co.in/search?q=split+digit+from+int+in+java) before asking any question. – Kaushik Oct 22 '14 at 10:56
  • Read the excellent Java documentation for String -- several functions there you can use. And not sure what you mean by "static" vs "dynamic" -- a number is a number. – Hot Licks Oct 22 '14 at 12:03

3 Answers3

0

use % operator

 int num1,num2,num3,num4,temp;

num1=s%10;
temp=s/10;

num2=temp%10;
temp=temp/10;

num3=temp%10;
num4=temp/10;

then you can convert num1,2,3,4 to string

Rasel
  • 5,488
  • 3
  • 30
  • 39
0

Transform it to string and get the chat at the position you want.

    int index = 2;
    int value = 1234;
    int digitAt=Integer.parseInt(String.valueOf(String.valueOf(value).charAt(index)));

so for example :

 String str1 =  String.valueOf(value).charAt(0);
 String str2 =  String.valueOf(value).charAt(1);
 String str3 =  String.valueOf(value).charAt(2);
 String str4 = String.valueOf(value).charAt(3);
Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
  • could you tell me what is the purpose of index – user3728511 Oct 22 '14 at 11:23
  • index is the position that you want to get. If you want to get the digit on second position index should be 1 and so on – Pedro Oliveira Oct 22 '14 at 11:24
  • if i am using this line String str1 = (int) String.valueOf(value).charAt(0); the error is coming like Type mismatch: cannot convert from int to String, if i change int instead of string error is not coming what can i do – user3728511 Oct 22 '14 at 11:42
  • Yes, you should remove the cast `(int)` – Pedro Oliveira Oct 22 '14 at 11:45
  • No, instead of (int) you have to call Integer.valueOf(...) – Sebastian Oct 22 '14 at 11:47
  • @Sebastian That will throw an error too. `String str1` is a string, not an Integer. And a `(int)` cast it's safe in this case. – Pedro Oliveira Oct 22 '14 at 11:48
  • @PedroOliveira The whole line should say `int digitAt= Integer.valueOf(String.valueOf(String.valueOf(value).charAt(index)));` Only calling (int) will work but give you the int value of the char. That is not wanted. – Sebastian Oct 22 '14 at 11:51
  • @Sebastian You're right about `(int)` giving the value of the char. However `Integer.valueOf` will do the same thing. What we're looking for is `Integer.parseInt` – Pedro Oliveira Oct 22 '14 at 11:56
  • @PedroOliveira See the documentation of Integer.valueOd(String). It is just like Integer.parseInt – Sebastian Oct 22 '14 at 12:03
  • Have you tested what you're saying? Because I did and I'm sure parse it's different from valueof – Pedro Oliveira Oct 22 '14 at 12:13
0

You can get the characters like this:

int s = 1234;
String t = String.valueOf(s);
String[] tArr = new String[t.length()];
for (int i = 0; i < t.length(); i++){
    tArr[0] = String.valueOf(t.charAt(i));
}

So you will get the array of individual character as String.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124