0
import java.util.Scanner;

public class MethodTestingLab {
   public static void main(String[] args) {

      int menuChoice, num1;
      Scanner keyboardReader = new Scanner(System.in);

      // Display Menu
      System.out.println("Menu\n 1.chkNextInt\n 2.twoDigitNumber\n 3.big2Digits\n 4.reverseString\n 5.max3\n");

      // User choice
      System.out.print("Enter your choice ");
      menuChoice = keyboardReader.nextInt();


      // Cases
      switch(menuChoice) {
         case 4:
            System.out.println("Given a three-lettered string, returns the string that is the reverse of its characters.");
            System.out.print("Enter a string: ");
            case4String = keyboardReader.next();
            System.out.println(reverseString(case4String));
            break;
      }
      System.out.println("End");
   }


   // Method 4
   public static String reverseString(case4String) {
      newString = case4String.charAt(2) + case4String.charAt(1) + case4String.charAt(0);
      return newString;
   }

}

// How do I fix the identify error in Method 4? I can't compile it until it is fixed, and when I tried adding String before case4String, it caused way more errors than the one before.

Scransom
  • 3,175
  • 3
  • 31
  • 51
Ashley
  • 17
  • 5
  • Possible duplicate of [Reverse a string in Java](https://stackoverflow.com/questions/7569335/reverse-a-string-in-java) – ccKep Oct 04 '17 at 22:53
  • Also, possibly [How to concatenate characters in java?](https://stackoverflow.com/questions/328249/how-to-concatenate-characters-in-java) – ccKep Oct 04 '17 at 22:55

1 Answers1

0

Looks like you are just learning Java, congratulations! ccKep was right that you are missing several identifiers. To define a variable in Java, you have to tell the compiler what type the variable is going to hold. The same way you defined menuChoice and num1 as the type int, you have to do the same for the variable case4String and newString.

Another issue is that you cannot concatenate the char type with the + operator in your reverseString method. There are a couple ways to fix that. One way you can get a String from char type is to create a new String from a char array.

// Method 4
static String reverseString(String case4String) {
    char[] chars = {case4String.charAt(2), case4String.charAt(1), case4String.charAt(0)};
    String newString = new String(chars); // and here.
    return newString;
}

Or you can use one of the suggestions from the Reverse a string answer provided by ccKep.

// Method 4
static String reverseString(String case4String) {
    StringBuilder newStringBuilder = new StringBuilder(case4String); // and here.
    return newStringBuilder.reverse().toString();
}

This is a better answer because it does not limit you to the three character limit, the way you have to do it with your charAt solution. Here's the code with the identifiers fixed and the second proposed solution.

import java.util.Scanner;

public class MethodTestingLab {
    public static void main(String[] args) {

        int menuChoice, num1;
        Scanner keyboardReader = new Scanner(System.in);

        // Display Menu
        System.out.println("Menu\n 1.chkNextInt\n 2.twoDigitNumber\n 3.big2Digits\n 4.reverseString\n 5.max3\n");

        // User choice
        System.out.print("Enter your choice ");
        menuChoice = keyboardReader.nextInt();


        // Cases
        switch(menuChoice) {
            case 4:
                System.out.println("Given a three-lettered string, returns the string that is the reverse of its characters.");
                System.out.print("Enter a string: ");
                String case4String = keyboardReader.next();
                System.out.println(reverseString(case4String));
                break;
        }
        System.out.println("End");
    }


    // Method 4
    public static String reverseString(String case4String) {
        StringBuilder newStringBuilder = new StringBuilder(case4String); // and here.
        return newStringBuilder.reverse().toString();
    }

}