问题
The assignment for java is to write a method that accepts string objects as an argument and returns the number of words it contains. Demonstrate the method in a program that asks the user to input a string and passes it to the method. The number of words should be displayed in the screen. I know its close but there are probably some errors.
public class WordCounter
{
//Asks and gets the users input here
private static string getInput(Scanner in)
{
String input;
//Imported scanner here
Scanner in = new Scanner(System.in);
//Ask the user to enter string here
System.out.println("Enter a string here: ");
input = in.nextLine();
//Create an if/else statment to find out if the user entered input
if(input.length() > 0)
{
getInput(input); //Get word count
}
else
{
System.out.println("Error -- You must enter a string!");
System.out.println("Enter a string here: ");
input = in.nextLine();
}
return input;
} //Close public static string getInput here
//Calculates the number of words the user inputs
public static int getWordCount(String input)
{
String[] result = input.split(" ");
return result.length;
} //Close public static int getWordCount here
//Prints out the number of words from the users input in string above
public static void main(String[] args)
{
//Print out the number of words within the users string here
System.out.println("The number of words in the string are: " + counter);
} //Close public static void main string args here
回答1:
I fixed quite a few parts of you code, and commented where I changed something.
import java.util.Scanner;
public class WordCounter
{
//Asks and gets the users input here
private static String getInput(Scanner in) // java is CASE-SENSITIVE. string !=String
{
String input;
//Imported scanner here
// Scanner in = new Scanner(System.in); // cannot redefine variable in same scope, also un-needed
//Ask the user to enter string here
System.out.println("Enter a string here: ");
input = in.nextLine();
//Create an if/else statment to find out if the user entered input
if(input.isEmpty()) // better form to do this
{
System.out.println("Error -- You must enter a string!");
return getInput(in); //Get input again (it's bad form to use recursion here, but not technically wrong)
}
else
{
return input;
}
} //Close public static string getInput here
//Calculates the number of words the user inputs
private static int getWordCount(String input)
{
String[] result = input.split(" ");
return result.length;
} //Close public static int getWordCount here
//Prints out the number of words from the users input in string above
public static void main(String[] args)
{
//Print out the number of words within the users string here
String input = getInput(new Scanner(System.in)); //You needed to actually call these methods!
int counter = getWordCount(input);
System.out.println("The number of words in the string are: " + counter);
} //Close public static void main string args here
}
回答2:
First, you need to clean up your getInput()
method -
private static String getInput(Scanner in) {
// Ask the user to enter string here
do {
System.out.println("Enter a string here: ");
String input = in.nextLine();
// Create an if/else statement to find out if the user
// entered input
if (input.trim().length() > 0) {
return input.trim();
} else {
System.out.println("Error -- "
+ "You must enter a string!");
}
} while (true);
} // Close public static string getInput here
Second, in your main
method - declare and initialize your counter
-
int counter = getWordCount(getInput(new Scanner(System.in)));
Then your code worked for me.
回答3:
Where is "counter" declared? Where is the rest of your code? You declare your methods but don't call them. Also within your first if statement I believe you meant to call getWordCount(input)
and print out the result . Fix these problems update your post accordingly.
来源:https://stackoverflow.com/questions/22900641/java-write-method-that-accepts-string-object-as-argument-and-returns-word-count