-1

I'm working on Java homework, which is divided into several parts. I'm stuck on part 2. This is not meant to be a homework dump, I'm just stuck on part 2, I included part 1 and 3 for context. I'd really appreciate any help or feedback. Thanks!

(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)

Ex:

Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

(2) Implement a printMenu() method, which outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option. Each option is represented by a single character.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call printMenu() in the main() method. Continue to call printMenu() until the user enters q to Quit. (3 pts)

Ex:

MENU c - Number of non-whitespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit

Choose an option:

(3) Implement the getNumOfNonWSCharacters() method. getNumOfNonWSCharacters() has a string as a parameter and returns the number of characters in the string, excluding all whitespace. Call getNumOfNonWSCharacters() in the main() method. (4 pts)

Ex:

Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

MENU c - Number of non-whitespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit

Choose an option: c Number of non-whitespace characters: 181

My code:

import java.util.Scanner;

public class AuthoringAssistant {

   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      String userInput = sc.nextLine();
      System.out.println("Enter a sample text: ");
      System.out.println(userInput);

      return printMenu;
   }

   public static int printMenu(Character[] menu) {
      Scanner in = new Scanner(System.in);
      Character userChar = in.nextCharacter();
      System.out.println("MENU");
      System.out.println("c - Number of non-whitespace characters");
      System.out.println("w - Number of words");
      System.out.println("f - Find text");
      System.out.println("r - Replace all !'s");
      System.out.println("s - Shorten spaces");
      System.out.println("q - Quit");
      System.out.println("Choose an option: ");

      if (userChar == 'q') {
         System.out.println("q"); 
         System.out.println("Quit");
      }
      else if (userChar == 'w') {
         System.out.println("w");
         System.out.println("Number of words: ");
      }
      else if (userChar == 'f') {
         System.out.println("f");
         System.out.println("Find text: ");
      }
      else if (userChar == 'r') {
         System.out.println("r");
         System.out.println("Replace all !'s: ");
      }
      else if (userChar == 's') {
         System.out.println("s");
         System.out.println("Shorten spaces: ");
      }
      else if (userChar == 'c') {
         System.out.println("c");
         System.out.println("Number of non-whitespace characters: ");
      }  
      else {
         System.out.println("Please select a valid character.");
      }

      return char in.nextCharacter();
   }
}

I'm getting the following errors:

AuthoringAssistant.java:55: error: '.class' expected
      return char in.nextCharacter();
              ^
AuthoringAssistant.java:55: error: illegal start of expression
      return char in.nextCharacter();
                ^
AuthoringAssistant.java:55: error: ';' expected
      return char in.nextCharacter();
                              ^
3 errors
Daniel
  • 11
  • 1
  • 3

2 Answers2

0

Firstly, Scanner has no nextCharacter method. See here.

Secondly, casting is done with 2 parentheses surronding the type, eg.

(char) someValue

Edit: see Will Potato's answer

SuperStormer
  • 3,554
  • 4
  • 16
  • 29
0

You need to change your return statement.

1) You are trying to return a char, but your method returns an int.

public static int printMenu(...){
   ...
   return char ... ;
}

2) ".nextCharacter()" doesn't exist. You need to use ".nextLine()" and then ".charAt(x)"

Eg.:

 in.nextLine().charAt(0); //To get the first character from the line

3) Even if your method did return chars, you only declare the type when declaring a variable or casting to a different type.

So instead of:

return char myVariable;

You would just write:

return myVariable;