Hi, everyone. So, suppose I have a String variable called str, with the value "123". I also have an array of integers, with length 3. I am trying to put each digit in the array:
This isn't working, though. IfWhen I print the number, the value 495051 is displayed. What am I doing wrong?
Code:
public class test
{
public static void main( String[] args )
{
int digits[] = new int[ 3 ];
String str = "123";
/* 'Extract' digits: */
for ( int i = str.length() - 1 ; i >= 0 ; i-- )
{
digits[ i ] = ( int ) str.charAt( i );
}
/* Print array: */
for ( int i = 0 ; i <= 2 ; i++ )
{
System.out.print( digits[ i ] );
}
}
}
Comment