Java初学者-计算句子中的单词数
我想用一些方法来计算句子中的单词数。我写了这段代码,我不太清楚为什么它不起作用。无论我写什么,我只收到一个字的计数。如果你能告诉我如何修改我写的东西,而不是给我一个完全不同的想法,那就太好了:Java初学者-计算句子中的单词数,java,methods,Java,Methods,我想用一些方法来计算句子中的单词数。我写了这段代码,我不太清楚为什么它不起作用。无论我写什么,我只收到一个字的计数。如果你能告诉我如何修改我写的东西,而不是给我一个完全不同的想法,那就太好了: import java.util.Scanner; public class P5_7 { public static int countWords(String str) { int count = 1; for (int i=0;i<=str.
import java.util.Scanner;
public class P5_7
{
public static int countWords(String str)
{
int count = 1;
for (int i=0;i<=str.length()-1;i++)
{
if (str.charAt(i) == ' ' && str.charAt(i+1)!=' ')
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = in.next();
System.out.print("Your sentence has " + countWords(sentence) + " words.");
}
}
import java.util.Scanner;
公共类P5_7
{
公共静态整型countWords(字符串str)
{
整数计数=1;
对于(int i=0;i如果是空句子,您不应该使用int count=0;
而不是int count=1;
。如果您遇到Java错误,请将它们添加到您的问题中。您需要阅读整行内容。而不是.next()中的;
使用.nextLine()中的
解决此问题的简单方法:
return str.split(" ").length;
或者更仔细地说,这是您考虑多个空格的方式:
return str.split("\\s+").length;
i请检查str.charAt(i+1)
处的边界条件。当字符串以空格结尾时,它可能导致StringIndexOutOfBoundsException
说明字符串以“”开头的情况
字符串可以是空的
“单词”的定义可能不同。例如-“12”。1,2是单词吗
我知道您不需要另一种方法,但string.split(“”.length()是一种更简单的开始方式。实际上,如果您输入如下句子:
"Hello World"
您的代码String-sensume=in.next();
将只获取句子中的第一个单词,即Hello
。
因此,您需要在.nextLine()
中使用来代替.next()
中的来获取整个句子,即,Hello World
我就是这样做的:
它也很好用
import java.util.Scanner;
public class countwords {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter your sentence:[Try to ignore space at end]");
String s=in.nextLine();
System.out.println("Size of the string is "+s.length());
int res=count(s);
System.out.println("No of words in the given String --->>"+" "+s+" "+"is"+" :"+res);
}
private static int count(String s) {
// TODO Auto-generated method stub
int count=0;
if(s.charAt(0)!=' '){
count++;
}
for(int i=0;i<s.length();i++){
if((s.charAt(i)==' ')){
count++;
}
}
return count;
}
}
步骤1:输入字符串
步骤2:将字符串拆分为数组中的单个单词存储
步骤3:返回数组的长度
public class P5_7
{
public static int countWords(String str)
{
String words[]=str.split(" ");
int count=words.length;
return count;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence =in.nextLine();
System.out.print("Your sentence has " + countWords(sentence) + " words.");
}
}尝试使用split()和参数作为空格的简单代码
int length=str.split(" ").length;
它将返回句子中的字数。使用此方法计算字符串中的字数:-
private int calculateWords(String s){
int count=0;
if(s.charAt(0)!=' ' || s.charAt(0)!=','){
count++;
}
for(int i=1;i<s.length();i++){
if((s.trim().charAt(i)==' ' && s.charAt(i+1)!=' ') || (s.trim().charAt(i)==',' && s.charAt(i+1)!=',')){
count++;
}
}
return count;
}
private int-calculateWords(字符串s){
整数计数=0;
如果(s.charAt(0)!=“| | s.charAt(0)!=”,”){
计数++;
}
对于(int i=1;i测试方法-处理所有输入
public static void countwords() {
String s = " t ";
int count = 0;
int length = s.length()-1;
char prev = ' ';
for(int i=length; i>=0; i--) {
char c = s.charAt(i);
if(c != prev && prev == ' ') {
count++;
}
prev = c;
}
System.out.println(count);
}
类词{
公共静态void main(字符串[]args){
String str=“你好,世界,这是新的”;
str=str.trim();
int n=str.length();
字符a[]=新字符[n];
str.getChars(1,n,a,0);
对于(int i=0;i而言,简单的逻辑是仅当之前没有任何空格时才计算空格。
试试这个:
public class WordCount
{
public static void main(String[] args)
{
int word=1;
Scanner s = new Scanner(System.in);
System.out.println("Enter a string: ");
String str=s.nextLine();
for(int i=1;i<str.length();i++)
{
if(str.charAt(i)==' ' && str.charAt(i-1)!=' ')
word++;
}
System.out.println("Total Number of words= "+word);
}
}
公共类字数
{
公共静态void main(字符串[]args)
{
int-word=1;
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入字符串:”);
字符串str=s.nextLine();
对于(inti=1;iimport java.util.HashMap
导入java.util.Map
导入java.util.Scanner
公共类字计数{
public static void main(final String[] args) {
System.out.println("Please Enter Your String: ");
final Map<String, Integer> hm = new HashMap<String, Integer>();
final Scanner sc = new Scanner(System.in);
final String s1 = sc.nextLine();
final String[] c1 = s1.split(" ");
for (int i = 0; i < c1.length; i++) {
if (!hm.containsKey(c1[i])) {
hm.put(c1[i], (Integer)1);
}// if
else {
hm.put(c1[i], hm.get(c1[i]) +(Integer) 1);
}// else
}// for
System.out.println("The Total No Of Words: " + hm);
}// main
publicstaticvoidmain(最终字符串[]args){
System.out.println(“请输入字符串:”);
最终映射hm=新的HashMap();
最终扫描仪sc=新扫描仪(System.in);
最终字符串s1=sc.nextLine();
最终字符串[]c1=s1。拆分(“”);
对于(int i=0;i
}//WordCount我建议使用。这是覆盖非标准语言(如日语)的最佳方法,因为日语中没有空格分隔单词
单词计数的示例。这里还有一种方法,只需使用拆分
,修剪
,等于
方法来改进代码和性能
这段代码也适用于空格
它是如何不起作用的?是否存在异常,或者只是一个不正确的输出?它以什么方式不起作用?您可以提供输入和输出的示例吗?您没有考虑一些特殊情况,例如:字符串为空;末尾有空格;字符串为空。此外,分词符是“空白”,包括制表符、行尾等。Also您的算法假设字符串中至少有一个单词。不管怎样,我最后只得到一个单词。我只是一个初学者,所以所有这些异常都是相对不相关的split(\\W+))
偶数-但op指定了,而不是给我一个完全不同的想法…仍然-只有一个单词。我认为该方法很好,可能是扫描仪输入有问题?@user2266957为了使上述方法起作用,在调用该方法之前,必须将整个文本读入一个字符串中上帝保佑!(不是真正的宗教,但仍然…)以上是很容易理解的。
class Words {
public static void main(String[] args) {
String str="Hello World this is new";
str=str.trim();
int n=str.length();
char a[]=new char[n];
str.getChars(1,n,a,0);
for (int i=0;i<str.length() ; i++){
if(a[i]==' ') count++;
}
System.out.println("No. of words = "+(count+1));
}
public class WordCount
{
public static void main(String[] args)
{
int word=1;
Scanner s = new Scanner(System.in);
System.out.println("Enter a string: ");
String str=s.nextLine();
for(int i=1;i<str.length();i++)
{
if(str.charAt(i)==' ' && str.charAt(i-1)!=' ')
word++;
}
System.out.println("Total Number of words= "+word);
}
}
public static void main(final String[] args) {
System.out.println("Please Enter Your String: ");
final Map<String, Integer> hm = new HashMap<String, Integer>();
final Scanner sc = new Scanner(System.in);
final String s1 = sc.nextLine();
final String[] c1 = s1.split(" ");
for (int i = 0; i < c1.length; i++) {
if (!hm.containsKey(c1[i])) {
hm.put(c1[i], (Integer)1);
}// if
else {
hm.put(c1[i], hm.get(c1[i]) +(Integer) 1);
}// else
}// for
System.out.println("The Total No Of Words: " + hm);
}// main
package test;
public class CommonWords {
public static void main(String[] args) {
String words = "1 2 3 44 55 966 5 88 ";
int count = 0;
String[] data = words.split(" ");
for (String string : data) {
if (!string.equals("")) {
count++;
}
}
System.out.println(count);
}
}
public int countWords(String string){
String strArr [] = string.split("\\s");
int count = 0;
for (String str: strArr) {
if(!str.trim().equals("")){
count++;
}
}
return count;
}