如何让“Split”和“wordCount”方法协同工作?

时间:2022-08-25 18:09:33

I am a beginner in java, first year computing student with very weak understanding of java and many problems with understanding its structures.

我是java的初学者,第一年是计算机学生,对java的理解很少,理解它的结构有很多问题。

This is my code I have so far:

这是我到目前为止的代码:

         import java.util.*;

public class hdtest1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);       
String s;
while(true)  
{
    System.out.println("Enter value:");
    s=sc.next();    
    if(s.equals("quit"))                  
    {
        break;
    }
  String[] s = string.split(" ");
   int wordCount = 1;

for (int i = 0; i < str1.length(); i++) 
{
    if (str1.charAt(i) == ' '&& str1.charAt(i+1)!=' ') 
    {
        wordCount++;
    } 
}
 }

 }
    System.out.println(""+s); //to Print string
    System.out.println(""+s.length());  //to print Entered string's length
    System.out.println("Word count is = " +(str1.length()- wordCount));


}

I would like my java output to show the text line entered in the text window (scanner), total number of characters and the number of characters for each word. For example when the user enters 'Hello, my name is Mark', the output I am looking for is:

我希望我的java输出显示在文本窗口(扫描仪)中输入的文本行,总字符数和每个单词的字符数。例如,当用户输入'Hello,我的名字是Mark'时,我正在寻找的输出是:

      Hello, my name is Mark
      22
      Hello = 5
      my = 2
      name = 4
      is = 2
      Mark = 4
      //Here the scanner loops,appears and is ready for more text to be entered

So to sum up my question - Please could you correct my code where the errors are to make it work correctly and output what I want (shown above)? Thank you very much for your help, any help would be much appreciated.

总结一下我的问题 - 请你纠正我的代码,错误是让它正常工作并输出我想要的东西(如上所示)?非常感谢您的帮助,非常感谢任何帮助。

At the moment my code displays many errors, they are the structural errors because I have very weak understanding of matching different methods together. So far I found many chunks of code which I thought would work together and I've tried to manipulate with the code to make the methods work together, but I only seem to make my code worse as I don't understand the meaning of many errors to be able to correct them. My code worked fine until i started trying to split the words entered in the scanner by using the method 'split'.

目前我的代码显示了许多错误,它们是结构错误,因为我对将不同方法匹配在一起的理解非常薄弱。到目前为止,我发现了很多代码,我认为这些代码可以协同工作,我试图用代码来操作,使方法协同工作,但我似乎只是让我的代码变得更糟,因为我不理解很多代码的含义错误能够纠正它们。我的代码工作正常,直到我开始尝试使用“拆分”方法拆分扫描仪中输入的单词。

Here is a quick list of errors for you to have a look:

以下是一个快速的错误列表供您查看:

line 15 - duplicate variable s,

第15行 - 重复变量s,

line 15 - string cannot be resolved,

第15行 - 无法解析字符串,

line 18 - str1 cannot be resolved,

第18行 - str1无法解析,

line 20 - str1 cannot be resolved,

第20行 - str1无法解析,

line 28 - syntax error on token(s), misplaced construct (s),

第28行 - 令牌上的语法错误,错放的构造,

line 28 - syntax error on token "println", = expected after this token,

第28行 - 令牌“println”上的语法错误,=此令牌后的预期,

line 29 - syntax error on token(s), misplaced construct (s),

第29行 - 令牌上的语法错误,错放的构造,

line 30 - syntax error on token(s), misplaced construct (s),

第30行 - 令牌上的语法错误,错放的构造,

line 30 - syntax error on token "println", = expected after this token,

第30行 - 令牌“println”上的语法错误,=此令牌后的预期,

4 个解决方案

#1


1  

Split a sentence into constituent words and then simply call length() method on each word to get the number of characters.

将一个句子拆分成组成单词,然后简单地在每个单词上调用length()方法以获得字符数。

String[] words = input.split("\\s+");  // get the individual words (separated by white space)
System.out("#words = " + words.length);
for (int i = 0; i < words.length; i++)
    System.out("word[" + i + "]: " + words[i] + " nchars = " + words[i].length());

#2


1  

It looks like you just want this:

看起来你只想要这个:

int wordCount = string.split(" ").length;

If you want to print words and their lengths:

如果要打印单词及其长度:

for (String word : string.split(" "))
    System.out.println(word + " = " + word.length());

#3


1  

Once you have input from the scanner (say it's a variable called input), try:

一旦您从扫描仪输入(比如它是一个名为输入的变量),请尝试:

// The line below splits the sentence into individual tokens by spaces, and puts them into an array
String[] allWords = input.split(" "); 
System.out.println(input);
System.out.println(input.length()); // Get number of characters in input
for (int i = 0; i < allWords.length; i++) { // Loop through each word
    String word = allWords[i].replaceAll("[^a-zA-Z]", ""); // This uses a regex to eliminate anything but letters and puts the word into a variable "word"
    System.out.println(word + " = " + word.length()); // Output the word and the length
}

Put this block of code in a loop where the scanner reads a line.

将此代码块放在扫描器读取一行的循环中。

Just a note, the length() method outputs the number of characters in a string, including spaces. That's why we remove all other characters besides letters when we output the length.

只需注意,length()方法输出字符串中的字符数,包括空格。这就是为什么我们在输出长度时删除除字母之外的所有其他字符。

#4


1  

Couple pointers:

  1. You have a String s and a String[] s, you can't do that, rename one of them.

    你有一个String和一个String [],你不能这样做,重命名其中一个。

  2. Where is "str1"? It hasn't been declared or initialized.

    “str1”在哪里?它尚未声明或初始化。

3.

    System.out.println(""+s);

"s" is already a String, ""+ is unnecessary.

“s”已经是一个字符串,“”+是不必要的。

#1


1  

Split a sentence into constituent words and then simply call length() method on each word to get the number of characters.

将一个句子拆分成组成单词,然后简单地在每个单词上调用length()方法以获得字符数。

String[] words = input.split("\\s+");  // get the individual words (separated by white space)
System.out("#words = " + words.length);
for (int i = 0; i < words.length; i++)
    System.out("word[" + i + "]: " + words[i] + " nchars = " + words[i].length());

#2


1  

It looks like you just want this:

看起来你只想要这个:

int wordCount = string.split(" ").length;

If you want to print words and their lengths:

如果要打印单词及其长度:

for (String word : string.split(" "))
    System.out.println(word + " = " + word.length());

#3


1  

Once you have input from the scanner (say it's a variable called input), try:

一旦您从扫描仪输入(比如它是一个名为输入的变量),请尝试:

// The line below splits the sentence into individual tokens by spaces, and puts them into an array
String[] allWords = input.split(" "); 
System.out.println(input);
System.out.println(input.length()); // Get number of characters in input
for (int i = 0; i < allWords.length; i++) { // Loop through each word
    String word = allWords[i].replaceAll("[^a-zA-Z]", ""); // This uses a regex to eliminate anything but letters and puts the word into a variable "word"
    System.out.println(word + " = " + word.length()); // Output the word and the length
}

Put this block of code in a loop where the scanner reads a line.

将此代码块放在扫描器读取一行的循环中。

Just a note, the length() method outputs the number of characters in a string, including spaces. That's why we remove all other characters besides letters when we output the length.

只需注意,length()方法输出字符串中的字符数,包括空格。这就是为什么我们在输出长度时删除除字母之外的所有其他字符。

#4


1  

Couple pointers:

  1. You have a String s and a String[] s, you can't do that, rename one of them.

    你有一个String和一个String [],你不能这样做,重命名其中一个。

  2. Where is "str1"? It hasn't been declared or initialized.

    “str1”在哪里?它尚未声明或初始化。

3.

    System.out.println(""+s);

"s" is already a String, ""+ is unnecessary.

“s”已经是一个字符串,“”+是不必要的。