0

I want to get it to loop at each method/section, but I'm getting errors. When I comment out each do while, they work perfectly, however as a whole I get:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at RecursionTester.main(RecursionTester.java:25)

My code:

import java.util.Scanner;

public class RecursionTester {

    public static void main(String[] args) {

        int power;
        int base;
        String sentence;
        int digit;
        int num;
        char answer;

        Scanner sc = new Scanner(System.in);
        do {
            System.out.print("What base would you like? ");
            base = sc.nextInt();

            System.out.print("What power would you like? ");
            power = sc.nextInt();

            System.out.println(base + " to the " + power + " power is " + raiseToPower(base, power));

            System.out.print("\nWould you like raise another number to a power (y or n)? ");
            answer = sc.nextLine().charAt(0);
        } while (answer == 'Y' || answer == 'y');

        do {
            System.out.print("Type a sentence and check if it's a palindrome: ");
            sentence = sc.nextLine().trim();

            if (palindrome(sentence))
                System.out.println(sentence + " is a palindrome");
            else
                System.out.println(sentence + " is not a palindrome");

            System.out.print("\nWould you like to reverse another text string (y or n)? ");
            answer = sc.nextLine().charAt(0);
        } while (answer == 'Y' || answer == 'y');
        sc.close();
    }

    //private methods here
}
Bryan
  • 13,244
  • 9
  • 62
  • 114

0 Answers0