Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
4年前关闭。
因此,我必须编写一个用于分配的程序,为此,我必须接受一个字符串,确保它具有正确的句子数并打印每个单词的频率。我几乎完全正确,但是最后,当我打印单词(已存储在数组中)时,每个单词都以
由于
根据您希望
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
4年前关闭。
因此,我必须编写一个用于分配的程序,为此,我必须接受一个字符串,确保它具有正确的句子数并打印每个单词的频率。我几乎完全正确,但是最后,当我打印单词(已存储在数组中)时,每个单词都以
Ljava.lang.String; @xyznumber
开头。我不知道为什么会这样,并且在网上找不到解决方案。这是我的代码:import java.util.Arrays;
import java.io.*;
class frequency
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of sentences");
int cS = Integer.parseInt(br.readLine());
System.out.println("Enter sentences");
String s = br.readLine();
int cS1 = 0;
int cW = 0;
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if (ch=='.'||ch=='?')
{
cW++;
cS1++;
}
if (ch==' ')
{
cW++;
}
}
if (cS1!=cS)
{
System.out.println("You have entered the wrong number of sentences. Please try again.");
}
else
{
int c = 0;
int d = 0;
String a[] = new String[cW];
System.out.println("Total Number of words: "+cW);
for (int i= 0;i<s.length();i++)
{
char ch=s.charAt(i);
if (ch==' '||ch=='?'||ch=='.')
{
a[c++]=a+s.substring(d,i);
d = i+1;
}
}
int length=0;
firstFor: for(int i=0;i<a.length;i++)
{
for(int j=0;j<i;j++)
{
if (a[j].equalsIgnoreCase(a[i]))
{
continue firstFor;
}
else
{
length++;
}
}
}
String words[] = new String[length];
int counts[] = new int[length];
int k=0;
secondFor: for (int i =0;i<a.length;i++)
{
for(int j = 0; j<i;j++)
{
if (a[j].equalsIgnoreCase(a[i]))
{
continue secondFor;
}
}
words[k]=a[i];
int counter = 0;
for (int j =0;j<a.length;j++)
{
if(a[j].equalsIgnoreCase(a[i]))
{
counter++;
}
}
counts[k]=counter;
k++;
}
for (int i=0;i<words.length;i++)
{
System.out.println(words[i]+"\n"+(counts[i]));
}
}
}
}
最佳答案
问题出在这里:
a[c++]=a+s.substring(d,i);
由于
a
是一个String
数组,因此该操作将a
中的元素之一分配为等于整个数组的String
表示形式,再加上一个s
的子字符串。但是,数组没有非常有用的String
表示形式,这是您看到的Ljava.lang.String;@xyznumber
来源。根据您希望
a[c]
的第一部分是什么,使用对数组的索引或convert the array to a String