Java 如何计算字符串数组中的字符串表示
我试图计算一个单词在stdin中被重复的次数 输入示例: 这是一个测试,这是 期望输出:Java 如何计算字符串数组中的字符串表示,java,arrays,string,Java,Arrays,String,我试图计算一个单词在stdin中被重复的次数 输入示例: 这是一个测试,这是 期望输出: this 2 is 3 a 1 test 1 我有一个int[]来存储wordCount但是我不确定在哪里使用它,int计数只是临时的,所以程序可以运行 以下是我的代码供参考: import java.util.Scanner; public class WCount { public static void main (String[] args) { Scan
this 2
is 3
a 1
test 1
我有一个int[]
来存储wordCount
但是我不确定在哪里使用它,int
计数只是临时的,所以程序可以运行
以下是我的代码供参考:
import java.util.Scanner;
public class WCount {
public static void main (String[] args) {
Scanner stdin = new Scanner(System.in);
String [] wordArray = new String [10000];
int [] wordCount = new int [10000];
int numWords = 0;
while(stdin.hasNextLine()){
String s = stdin.nextLine();
String [] words = s.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\\
s+"); //stores strings as words after converting to lowercase and getting rid of punctuation
for(int i = 0; i < words.length; i++){
int count = 0; //temporary so program can run
for(int j = 0; j < words.length; j++){
if( words[i] == words[j] )
count++;
System.out.println("word count: → " + words[i] + " " + count);
}
}
}
import java.util.Scanner;
公共类计数{
公共静态void main(字符串[]args){
扫描仪标准输入=新扫描仪(System.in);
字符串[]字数组=新字符串[10000];
int[]字数=新int[10000];
int numWords=0;
while(stdin.hasNextLine()){
字符串s=stdin.nextLine();
字符串[]words=s.replaceAll(“[^a-zA-Z]”,“”)。toLowerCase()。拆分(“”)\\\
s+“”;//将字符串转换为小写并去掉标点符号后存储为单词
for(int i=0;i
您可以在这里使用哈希表。我不打算为您编写代码,但这里有一个简单的伪算法:
if hashTable contains word
hashTable.put(word, words.value + 1)
else hashTable.put(word, 1)
对单词数组中的每个单词执行此操作。处理完所有单词后,只需打印哈希表中的每个关键字(单词)及其值(出现的次数)
希望这有帮助!您可以在此处使用哈希表。我不打算为您编写代码,但这里有一个简单的伪算法:
if hashTable contains word
hashTable.put(word, words.value + 1)
else hashTable.put(word, 1)
对单词数组中的每个单词执行此操作。处理完所有单词后,只需打印哈希表中的每个关键字(单词)及其值(出现的次数)
希望这有帮助!这对我来说很有效。虽然遍历完整的可能数组很愚蠢。使用ArrayList会更容易。但我不确定是否允许您使用它
import java.util.Scanner;
public class WCount {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int[] wordCount = new int[1000];
String[] wordList = new String[1000];
int j = 0;
while (stdin.hasNextLine()) {
String s = stdin.nextLine();
String[] words = s.split("\\W+");
for (String word : words) {
int listIndex = -1;
for (int i = 0; i < wordList.length; i++) {
if (word.equals(wordList[i])) {
listIndex = i;
}
}
if (listIndex > -1) {
wordCount[listIndex]++;
} else {
wordList[j] = word;
wordCount[j]++;
j++;
}
}
for (int i = 0; i < j; i++) {
System.out.println("the word: " + wordList[i] + " occured " + wordCount[i] + " time(s).");
}
}
}
}
这对我来说很有用。虽然遍历完整的可能数组很愚蠢。使用ArrayList会更容易。但我不确定是否允许您使用它
import java.util.Scanner;
public class WCount {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int[] wordCount = new int[1000];
String[] wordList = new String[1000];
int j = 0;
while (stdin.hasNextLine()) {
String s = stdin.nextLine();
String[] words = s.split("\\W+");
for (String word : words) {
int listIndex = -1;
for (int i = 0; i < wordList.length; i++) {
if (word.equals(wordList[i])) {
listIndex = i;
}
}
if (listIndex > -1) {
wordCount[listIndex]++;
} else {
wordList[j] = word;
wordCount[j]++;
j++;
}
}
for (int i = 0; i < j; i++) {
System.out.println("the word: " + wordList[i] + " occured " + wordCount[i] + " time(s).");
}
}
}
}
我会用这样的方式:
import java.util.ArrayList;
import java.util.Scanner;
public class WCount {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
String[] wordArray = new String[10000];
int[] wordCount = new int[10000];
int numWords = 0;
while (stdin.hasNextLine()) {
String s = stdin.nextLine();
ArrayList<String> noDuplicated = new ArrayList<String>();
String[] words = s.replaceAll("[^a-zA-Z ]", "").toLowerCase()
.split("\\s+"); // stores strings as words after converting
// to lowercase and getting rid of
// punctuation
//Array that contains the words without the duplicates ones
for (int i = 0; i < words.length; i++) {
if(!noDuplicated.contains(words[i]))
noDuplicated.add(words[i]);
}
//Count and print the words
for(int i=0; i<noDuplicated.size();i++){
int count = 0;
for (int j = 0; j < words.length; j++) {
if (noDuplicated.get(i).equals(words[j]))
count++;
}
System.out.println("word count: → " + words[i] + " "
+ count);
}
}
}
}
希望它有用!我会使用以下内容:
import java.util.ArrayList;
import java.util.Scanner;
public class WCount {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
String[] wordArray = new String[10000];
int[] wordCount = new int[10000];
int numWords = 0;
while (stdin.hasNextLine()) {
String s = stdin.nextLine();
ArrayList<String> noDuplicated = new ArrayList<String>();
String[] words = s.replaceAll("[^a-zA-Z ]", "").toLowerCase()
.split("\\s+"); // stores strings as words after converting
// to lowercase and getting rid of
// punctuation
//Array that contains the words without the duplicates ones
for (int i = 0; i < words.length; i++) {
if(!noDuplicated.contains(words[i]))
noDuplicated.add(words[i]);
}
//Count and print the words
for(int i=0; i<noDuplicated.size();i++){
int count = 0;
for (int j = 0; j < words.length; j++) {
if (noDuplicated.get(i).equals(words[j]))
count++;
}
System.out.println("word count: → " + words[i] + " "
+ count);
}
}
}
}
希望它有用!解决了这个问题…更简单的方法
import java.util.Vector;
public class Get{
public static void main(String[]args){
Vector<String> ch = new Vector<String>();
String len[] = {"this", "is", "this", "test", "is", "a", "is"};
int count;
for(int i=0; i<len.length; i++){
count=0;
for(int j=0; j<len.length; j++){
if(len[i]==len[j]){
count++;
}
}
if(count>0){
if(!ch.contains(len[i])){
System.out.println(len[i] + " - " + count);
ch.add(len[i]);
}
}
}
}
}
Output:
this - 2
is - 3
test - 1
a - 1
import java.util.Vector;
公共类获取{
公共静态void main(字符串[]args){
向量ch=新向量();
字符串len[]={“this”、“is”、“this”、“test”、“is”、“a”、“is”};
整数计数;
对于(inti=0;i来说,这是一种更简单的方法.)
import java.util.Vector;
public class Get{
public static void main(String[]args){
Vector<String> ch = new Vector<String>();
String len[] = {"this", "is", "this", "test", "is", "a", "is"};
int count;
for(int i=0; i<len.length; i++){
count=0;
for(int j=0; j<len.length; j++){
if(len[i]==len[j]){
count++;
}
}
if(count>0){
if(!ch.contains(len[i])){
System.out.println(len[i] + " - " + count);
ch.add(len[i]);
}
}
}
}
}
Output:
this - 2
is - 3
test - 1
a - 1
import java.util.Vector;
公共类获取{
公共静态void main(字符串[]args){
向量ch=新向量();
字符串len[]={“this”、“is”、“this”、“test”、“is”、“a”、“is”};
整数计数;
对于(int i=0;i我的实现:
public static void main(final String[] args) {
try (Scanner stdin = new Scanner(System.in)) {
while (stdin.hasNextLine()) {
Map<String, AtomicInteger> termCounts = new HashMap<>();
String s = stdin.nextLine();
String[] words = s.toLowerCase().split("[^a-zA-Z]+");
for (String word : words) {
AtomicInteger termCount = termCounts.get(word);
if (termCount == null) {
termCount = new AtomicInteger();
termCounts.put(word, termCount);
}
termCount.incrementAndGet();
}
for (Entry<String, AtomicInteger> termCount : termCounts.entrySet()) {
System.out.println("word count: " + termCount.getKey() + " " + termCount.getValue());
}
}
}
}
publicstaticvoidmain(最终字符串[]args){
try(Scanner stdin=新扫描仪(System.in)){
while(stdin.hasNextLine()){
Map termCounts=new HashMap();
字符串s=stdin.nextLine();
字符串[]words=s.toLowerCase().split([^a-zA-Z]+”);
for(字符串字:字){
AtomicInteger termCount=termCounts.get(word);
if(termCount==null){
termCount=新的原子整数();
termCounts.put(字,termCount);
}
termCount.incrementAndGet();
}
对于(条目termCount:termCounts.entrySet()){
System.out.println(“字数:“+termCount.getKey()+”+termCount.getValue());
}
}
}
}
我的实现:
public static void main(final String[] args) {
try (Scanner stdin = new Scanner(System.in)) {
while (stdin.hasNextLine()) {
Map<String, AtomicInteger> termCounts = new HashMap<>();
String s = stdin.nextLine();
String[] words = s.toLowerCase().split("[^a-zA-Z]+");
for (String word : words) {
AtomicInteger termCount = termCounts.get(word);
if (termCount == null) {
termCount = new AtomicInteger();
termCounts.put(word, termCount);
}
termCount.incrementAndGet();
}
for (Entry<String, AtomicInteger> termCount : termCounts.entrySet()) {
System.out.println("word count: " + termCount.getKey() + " " + termCount.getValue());
}
}
}
}
publicstaticvoidmain(最终字符串[]args){
try(Scanner stdin=新扫描仪(System.in)){
while(stdin.hasNextLine()){
Map termCounts=new HashMap();
字符串s=stdin.nextLine();
字符串[]words=s.toLowerCase().split([^a-zA-Z]+”);
for(字符串字:字){
AtomicInteger termCount=termCounts.get(word);
if(termCount==null){
termCount=新的原子整数();
termCounts.put(字,termCount);
}
termCount.incrementAndGet();
}
对于(条目termCount:termCounts.entrySet()){
System.out.println(“字数:“+termCount.getKey()+”+termCount.getValue());
}
}
}
}
我认为您传递给拆分
的字符串文本中的反斜杠数是错误的。但是,很难说清楚,因为行被包装了,我不知道最后一个反斜杠是否真的是源代码的一部分。请发布拆分
调用的确切文本,并且以不包装的方式发布。另请参见。Normally我想把这个问题作为重复的问题来结束,但还有其他有趣的问题需要解决。我想你传递给split
的字符串文本中的反斜杠数量是错误的。不过,很难说,因为行被包装了,我不知道最后一个反斜杠是否真的是你的问题的一部分ce.请发布split
调用的确切文本,并以不换行的方式发布。另请参阅。通常我会将此问题作为该问题的副本关闭,但还有其他有趣的问题需要解决。抱歉忘了提及,我不会大声使用散列来完成此作业。谢谢!抱歉忘了提及,我不在d使用散列来完成此作业。谢谢!