java出现次数最多的字母_关于Java:查找字母中每个字母出现次数最多的单词
我编写了一个程序来读取文本文件,其中每一行都是一个单词。 代码的第一部分找到以字母的每个字母开头的最长单词,并将其存储在数组中。 我希望程序执行的第二部分是为字母表中的每个字母找到该字母出现次数最多的单词。
因此输出应如下所示:
最长的字:
a: anthropomorphologically
b: blepharosphincterectomy
c: cholecystenterorrhaphy
d: dacryocystoblennorrhea
e: epididymodeferentectomy
f: formaldehydesulphoxylate
g: gastroenteroanastomosis
h: hematospectrophotometer
i: indistinguishableness
j: jurisprudentialist
k: keratoconjunctivitis
l: laparocolpohysterotomy
m: macracanthrorhynchiasis
n: naphthylaminesulphonic
o: omnirepresentativeness
p: pathologicopsychological
q: quadratomandibular
r: reticulatocoalescent
s: scientificophilosophical
t: tetraiodophenolphthalein
u: ureterocystanastomosis
v: vagoglossopharyngeal
w: weatherproofness
x: xanthocreatinine
y: yohimbinization
z: zoologicoarchaeologist
带有最多字母的单词:
a: astragalocalcaneal
b: beblubber
c: chlorococcaceae
d: disdodecahedroid
e: electrotelethermometer
f: giffgaff
g: cuggermugger
h: choledochorrhaphy
i: impossibilification
j: ajaja
k: akiskemikinik
l: allochlorophyll
m: dynamometamorphism
n: nonannouncement
o: choledochoduodenostomy
p: aplopappus
q: equivoque
r: archcorrupter
s: possessionlessness
t: anticonstitutionalist
u: untumultuous
v: overconservative
w: bowwow
x: adnexopexy
y: dacryocystosyringotomy
z: zizz
}
基本上,我需要弄清楚该怎么做,以便输出的单词不是与第一个字母相同的单词(例如,上面的'f'[giffgaff]怎么不以'f'开头)。 我已经用谷歌搜索了很多东西,却找不到任何帮助。
/**
* @param args first String argument is the
* name of the input text file
*/
public static void main(String [] args) throws IOException {
//instance variable
String[] longestWords = new String[26];
String[] mostCharsWord = new String[26];
String currentLine = null;
int[] numCharacters = new int[26];
//because the while loop in try statement is comparing lengths in order to
//assign words, I must give each element a non-null value
//in this case, length = 0
Arrays.fill(longestWords,"");
Arrays.fill(mostCharsWord,"");
//try block
try(BufferedReader br = new BufferedReader(new FileReader(args[0]))) {
String currentLongestWord;
int index;
int indexer = 0;
int count = 0;
int counter = 0;
while((currentLine=br.readLine()) != null) {
currentLine = currentLine.toLowerCase();
index = currentLine.charAt(0)-'a';
currentLongestWord = longestWords[index];
if(currentLine.length() > currentLongestWord.length()) {
longestWords[index] = currentLine;
}
/**
* this code below is for the"AND" bit, but I know that it's not correct.
* Instead of printing out the word with the most occurrences of each
* letter, it prints out the word with the most occurrences of each letter
* THAT BEGINS WITH THAT LETTER
*/
for(char c : currentLine.toCharArray()) {
if(c == currentLine.charAt(0)) {
count += 1;
}
}
for(String currentMostCharsWord : mostCharsWord) {
indexer += 1;
for(char c : currentLine.toCharArray()) {
for( char d: currentMostCharsWord.toCharArray()) {
if(c==d) {
//hmmm....this would compare every letter, not just the one
//that I'm looking for. booooooo
}
}
}
}
if(count > numCharacters[index]) {
numCharacters[index] = count;
mostCharsWord[index] = currentLine;
}
count = 0;
}
//close file!
br.close();
}
//catch block
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//finally / do anyway statement
finally {
System.out.println("Longest Words:
");
for(int j = 0; j < 26; j++) {
System.out.printf("%s: %s
", longestWords[j].charAt(0), longestWords[j]);
}
System.out.println("------------------------------------
Words with most letters:
");
for(int j = 0; j < 26; j++) {
System.out.printf("%s: %s
", mostCharsWord[j].charAt(0), mostCharsWord[j]);
}
}
}
}
尝试将您的问题分解为一些简单的问题。 让社区解决整个难题的可能性要比问他们哪一块拼图来的可能性小。
@ Okuma.Scott抱歉,这是我第一次真正在这里提出问题,我只想提供尽可能多的信息。 我以为我比实际上更接近答案。
您可以使用以下方式:
// Map with the longest word for each letter
Map longestWordMap = new HashMap();
// Map with the word with highest occurrences of each letter
Map mostCharsWordMap = new HashMap();
while((word = br.readLine()) != null) { {
word = word.toLowerCase();
Character beginning = word.charAt(0);
String longestWord = longestWordMap.get(beginning);
// If the current word is the longest, put the word in the map
if (longestWord == null || word.length() > longestWord.length()) {
longestWordMap.put(beginning, word);
}
for (char letter = 'a'; letter <= 'z'; letter++) {
String mostCharsWord = mostCharsWordMap.get(Character.valueOf(letter));
if (mostCharsWord == null ||
characterCount(letter, word) > characterCount(letter, mostCharsWord)) {
mostCharsWordMap.put(Character.valueOf(letter), word);
}
}
}
这是用于计算单词中字母出现次数的函数:
public static int characterCount(char letter, String word) {
int characterCount = 0;
for (char c : word.toCharArray()) {
if (c == letter) {
characterCount++;
}
}
return characterCount;
}
对此可能有更直接的方法。 因此,问题本质上是您有一个单词流,并根据当前从该流中读取的单词,将其与数据存储中已知的最长单词进行比较。 如果更长,则替换单词,否则什么也不做。 您的逻辑可能是根据其他内容(例如单词的字典顺序)来替换它。 您是否需要检查是否区分大小写是一项练习。
//主要是伪代码
public class LongestWord
{
Map longestWords = new HashMap();
while(wordsStream.hasNext())
{
String currentWord = wordStream.next();
String longestWordByLetter = longestWords.get(currentWord.charAt(0));
if(null != longestWordByLetter)
{
if(longestWordByLetter.size() < currentWord.size())
{
longestWords.put(currentWord.charAt(0),currentWord);
}//else do nothing
}else{
longestWords.put(currentWord.charAt(0),currentWord);
}
}
}
java出现次数最多的字母_关于Java:查找字母中每个字母出现次数最多的单词相关推荐
- 数组重复次数最多的元素递归_使用递归计算链接列表中元素的出现次数
数组重复次数最多的元素递归 Solution: 解: Required function: 所需功能: func_occurence ( node *temp) //recursive functio ...
- java 替换字母_【Java项目】将字符串中的字母全部替换成字母的下一个字母
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 输入:aBxyZ 输出:bCyzA [java] view plain copy print? 1.package com.iotex; 2./** 3. ...
- java 查找大写字母_在Java中查找字符串的所有大写字母
因此,我试图在用户输入的字符串中查找所有大写字母,但始终出现此运行时错误: Exception in thread "main" java.lang.StringIndexOutO ...
- java英文字母_用JAVA编一个程序输出全部的英文字母
展开全部 编码思62616964757a686964616fe59b9ee7ad9431333363376439路首先考虑到,英文字母的ASCII码是按字母顺序连续的整数,所以可以采用起始字母A递增的 ...
- java gc回收堆还是栈_浅析JAVA的垃圾回收机制(GC)
1.什么是垃圾回收? 垃圾回收(Garbage Collection)是Java虚拟机(JVM)垃圾回收器提供的一种用于在空闲时间不定时回收无任何对象引用的对象占据的内存空间的一种机制. 注意:垃圾回 ...
- java 必须try catch的异常_【java基础之异常】死了都要try,不淋漓尽致地catch我不痛快!...
@ 1.异常 1.1 异常概念 异常 :简单说就是不正常运行,最终导致JVM的非正常停止. 在Java等面向对象的编程语言中,异常本身是一个类,产生异常就是创建异常对象并抛出了一个异常对象.Java处 ...
- java web开源项目源码_适合Java新手的开源项目集合——在 GitHub 学编程
作者:HelloGitHub-老荀 当今互联网份额最大的编程语言是哪一个?是 Java!这两年一直有听说 Java 要不行了.在走下坡路了.没错,Java 的确在走下坡路,未来的事情的确不好说,但是瘦 ...
- java 首字母小写_java实现将字符串中首字母转换成大写,其它全部转换成小写的方法示例...
本文实例讲述了java实现将字符串中首字母转换成大写,其它全部转换成小写的方法.分享给大家供大家参考,具体如下: public class TestSubstring { public static ...
- java 协程线程的区别_为什么 Java 坚持多线程不选择协程?
谢邀. 先说结论:协程是非常值得学习的概念,它是多任务编程的未来.但是Java全力推进这个事情的动力并不大. 先返回到问题的本源.当我们希望引入协程,我们想解决什么问题.我想不外乎下面几点:节省资源, ...
- java方法参数类型不确定_一个Java方法能有多少个参数类型?这个好奇coder做了个实验...
选自 justinblank 机器之心编译 参与:李志伟.张倩 在 JVM 中,一个 Java 方法,最多能定义多少参数呢?这是一个很无聊的问题,即使能定义一万个,十万个,谁又会真的去这么做呢.但是作 ...
最新文章
- 牛客挑战赛36 D. 排名估算( “概率论全家桶”,好题,拉格朗日插值求自然数 k 次幂之和)
- 题解——HDU 1848 Fibonacci again and again
- 【聚能聊有奖话题】今日头条公布算法原理,你认可他们的理念吗?
- SharedPointer
- 洛谷——P1781 宇宙总统
- LRU算法四种实现方式介绍
- 一线大厂为什么对免费的开源项目这么热衷?
- [(IBUF driven by I/O terminal ) is unplaced after IO placer?
- 宇宙十大不为人知的事情
- 在 Redis 上实现的分布式锁
- CSS3新增了哪些特性
- atitit.atitit.hb many2one relate hibernate 多对一关联配置..
- easyui tree json php,easyui tree json
- 《理财市场情绪监测系统》代码实现【1】之行业词库
- OpenOffice实现word转pdf
- 【数据库系统原理与应用/数据库系统概论】 期末复习手册
- SuperMap Hi-Fi 3D SDK 11i(2022) for Unity插件开发——选中对象隐藏
- Java的Integer和Integer比较相等
- ESP8266/ESP32 网络温控器监控 Web服务器-基于温度控制输出
- YOLOv5 5.0版本检测FPS