简体   繁体   English

Java:接受字符串对象作为参数并返回字数的写入方法

[英]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.

相关问题 Java Word Counter -- 接受字符串对象作为参数并返回字数的编写方法 - Java Word Counter -- Write method that accepts string object as argument and returns word count 如何在Java中编写泛型方法以接受两个对象的任何列表并返回列表? - How to write generics method in java that accepts any list of two object and returns list? java计算字符串参数存储在对象中的次数 - java count the times String argument is stored in a Object 接受并返回对象的REST服务。 怎么写客户端? - REST service that accepts and returns object. How to write client? 如何编写一个将字符串作为参数并返回原来的字符串的递归方法? - How to write a recursive method that takes a string as an argument and returns that string turned inside out? 计算字符串Java上的每个单词 - Count each word on a string Java java方法接受对象数组,然后双精度型则返回对象数组 - java method accepts array of objects and a double then returns an array of objects 如果 java 方法接受 (Object, Object…),有没有办法将单个参数输入其中? - If a java method accepts (Object, Object…), is there a way to feed a single parameter into this? Java中以对象为参数的泛型方法 - Generic Method with object as argument in Java 无法将对象添加到jComboBox; addItem(…)方法仅接受String - Unable to add an object to a jComboBox; addItem(…) method only accepts String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM