-3

I know you can do this using characters with the .charAt() line but I'm wondering if there is anything like that for an Integer? This is in java.

EDIT:

This is the code im tring to do this to:

for(int j=0; j<lines; j++){ 
        for (int k=0; k<quizKey.length; k++){
        String lane = userAnswers   
            userIndividualAnswers[k][0] = userAnswers[0].IntAt(k);

        }//end for loop
    }//end for loop

obviously this is incorrect but using the math method how would i be able to convert the userAnswers ints separately? the userAnswers aray has like 5 ints in it.

Joseph Mindrup
  • 65
  • 1
  • 3
  • 10

2 Answers2

0

A multi-digit number is only multi-digit in the sense that the String representation requires multiple digits. With that in mind, your best option will be to use toString and pull out the individual components as Integers.

Or, as a commenter above mentioned, use integer division and % 10 to do it with math.

0

If you have an integer I and let's say I = 19478. if we use I%10, we get 8. % is used as modulos. It returns the remainder of the division. A good reference to modulos is -> Wikipedia. An implementation of mod in your situation is the following.

  int target = 104978;
  int[] ara = new int[6];         

  for(int i=0;i<ara.length;i++)
  {
         ara[i]=target%10; 
         target=target/10;
  }

First time through it will mod 104978 % 10 and return 8. It will then do integer division on 104978/10 and set the target to 10497. Second round through it mods 10497 % 10 = 7, etc. I hope this was helpful.

robbert229
  • 464
  • 5
  • 12
  • Also if you wanted to be a bit inefficient you could cast to string, and then perform char at, but that is not as cool as the super smart math way! – robbert229 Mar 15 '13 at 00:34