I'm writing a program that takes a number and reverse it. So far I think I was doing fairly not bad, but then I got this error massage.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Reversing.Revers(Reversing.java:9)
at Main.main(Main.java:11)
Here is my main class
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
String numstr = scan.nextLine();
Reversing rv = new Reversing();
rv.Revers(numstr);
}
}
and here is my reversing class
public class Reversing {
public String Revers(String str){
char[] charArr = str.toCharArray();//So I can know when the for loop should stop.
int [] fstVar = { };//First Variable.
int sndVar;//Second Variable.
int strint = Integer.parseInt(str);//Input as integer.
for (int x = 0; x < charArr.length - 1 ; x++){
fstVar[x] = strint % 10;
for (int i = 0; i < charArr.length - 1 - i; i++){
sndVar = (fstVar[i] *10 ) + fstVar[i];
}
}
for (int j = 0; j < charArr.length - 1; j++){
str = Integer.toString(fstVar[j]);
System.out.println(str);
}
return str;
}
}
may any one please tell me what I've done wrong this time.

New Topic/Question
Reply



MultiQuote




|