hey guys, i was wondering if anyone could tell me how to separate individual digits of an integer in order to store those individual digits into an array? I need to create a circular shift method so I was just going to separate the digits of the number, put them in an array, and then manipulate that array to get the circular shift.
I was thinking of using a tokenizer, however; I don't know what to set the delimiter to because the digits are obviously one after another.
This is a visual representation of what I'm talking about if you can't follow my babbling.

number(int): 1234

array with individual digits: {[1][2][3][4]}

so like I said, any help with separating the individual digits to store in an array would be greatly appreciated.

Recommended Answers

All 5 Replies

number something = 1234;
String s = something + "";
char[] chars = s.toCharArray();

I'm not sure if that will work; you may want to try it though.

number something = 1234;
String s = something + "";
char[] chars = s.toCharArray();

I'm not sure if that will work; you may want to try it though.

everything seems to work fine, thnx a bunch man.

This instruction

Integer i=123+"";

is wrong and it will cause an incompatible type exception

What you mean, with number ? did you mean the numeric type? In that case did you mean the Integral Type (byte, short, int, long and char) or the Floating Point Type (float and double).
Or are you talking about the corresponding wrappers? whose are class and not primitive : Byte, Short, Integer, Long and Character; and Float and Double. All of them derive from the base class Number that is an abstract class.
Let consider you are talking about the Integral type in that case you have to consider the radix: the Integral type may be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8). And what if the number is a negative?

And there is a many constraint in manipulating numbers!!!

You can try to use toString method to convert the number to string and then use the toCharArray function.
but try this code!!!:

char[] ch;
        String s;

        Integer integer = -1234;
        Double d = 1234.0;
        s = integer.toString();
        ch = s.toCharArray();
//here you will have the sign “-”(dot minus that is not a digit)  in ch[0]
        for (int i = 0; i < ch.length; ++i) {
            System.out.println(ch[i]);
        }
////here you will have the sign “.” (dot that is not a digit) in ch[4]

        s = d.toString();
        ch = s.toCharArray();
        for (int i = 0; i < ch.length; ++i) {
            System.out.println(ch[i]);
        }

Hope it helps.

Thanks for the help guys, I actually just used the methods that my compiler NetBeans had for integers. Ran into a little trouble because for our bigger project, which is a calculator, our teacher only wanted 16 bit max and the compiler did 32 so I had to work around that. Like I said though, I appreciate the suggestions and feedback.

Moutanna:

My example is legal Java code. It's more robust than your example because my example makes use of Java's autoboxing features. Number being an abstract class doesn't matter because I never tried to instantiate 'Number'. The example instantiates some class in Number's inheritance hierarchy.

Number n = -2.9;
System.out.println(n.getClass());
char[] chars = (n + "").toCharArray();
for (Character c: chars) System.out.print(c);

Sammich: Mark solved threads as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.