[英]Java: write method that accepts string object as argument and returns word count
The assignment for java is to write a method that accepts string objects as an argument and returns the number of words it contains. java 的赋值是编写一个方法,它接受字符串对象作为参数并返回它包含的单词数。 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
First, you need to clean up your getInput()
method -首先,您需要清理您的getInput()
方法 -
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
-其次,在您的main
方法中 - 声明并初始化您的counter
-
int counter = getWordCount(getInput(new Scanner(System.in)));
Then your code worked for me.然后你的代码对我有用。
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
}
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 .同样在您的第一个 if 语句中,我相信您打算调用getWordCount(input)
并打印出结果。 Fix these problems update your post accordingly.解决这些问题相应地更新您的帖子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.