必需:数组或java.lang.Iterable
我是Java的初学者。我只想计算文本文件中每个单词的出现次数。 输入格式如下所示:必需:数组或java.lang.Iterable,java,map,java.util.scanner,Java,Map,Java.util.scanner,我是Java的初学者。我只想计算文本文件中每个单词的出现次数。 输入格式如下所示: A B A C C A B C 以下是我迄今为止所做的工作: public static void main (String[] args) throws FileNotFoundException { Scanner inputFile = new Scanner(new File("test.txt")); while (inputFile.hasNextLine()) {
A B
A C
C A
B C
以下是我迄今为止所做的工作:
public static void main (String[] args) throws FileNotFoundException
{
Scanner inputFile = new Scanner(new File("test.txt"));
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
System.out.println(line);
// above is the first part, to read the file in
// below is the second part, try to count
Map<String, Integer> counts = new HashMap<>();
for (String word : line) {
Integer count = counts.get(word);
counts.put(word, count == null ? 1 : count + 1);
}
System.out.println(counts);
}
}
我在谷歌上得到了第一部分和第二部分,但不知道如何将它们结合起来。
任何建议都会很有帮助。您不能使用for each循环迭代
字符串(变量行
)。您需要首先将其拆分为以下单词:
String[] words = line.split(" ");
for(String word : words) {
// do something
}
代码中似乎也有一个bug。用于管理计数的映射需要出现在while循环之外,否则计数将是特定行的本地。更改代码如下:
public static void main (String[] args) throws FileNotFoundException
{
Scanner inputFile = new Scanner(new File("test.txt"));
Map<String, Integer> counts = new HashMap<>();
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
System.out.println(line);
// above is the first part, to read the file in
// below is the second part, try to count
String[] words = line.split(" ");
for (String word : words) {
Integer count = counts.get(word);
counts.put(word, count == null ? 1 : count + 1);
}
} // end of while
System.out.println(counts);
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException
{
扫描仪输入文件=新扫描仪(新文件(“test.txt”);
映射计数=新的HashMap();
while(inputFile.hasNextLine()){
String line=inputFile.nextLine();
系统输出打印项次(行);
//上面是第一部分,在其中读取文件
//下面是第二部分,试着数一数
String[]words=line.split(“”);
for(字符串字:字){
整数计数=计数。获取(字);
counts.put(word,count==null?1:count+1);
}
}//时间结束
系统输出打印项次(计数);
}
不能使用for each循环遍历字符串(变量行
)。您需要首先将其拆分为以下单词:
String[] words = line.split(" ");
for(String word : words) {
// do something
}
代码中似乎也有一个bug。用于管理计数的映射需要出现在while循环之外,否则计数将是特定行的本地。更改代码如下:
public static void main (String[] args) throws FileNotFoundException
{
Scanner inputFile = new Scanner(new File("test.txt"));
Map<String, Integer> counts = new HashMap<>();
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
System.out.println(line);
// above is the first part, to read the file in
// below is the second part, try to count
String[] words = line.split(" ");
for (String word : words) {
Integer count = counts.get(word);
counts.put(word, count == null ? 1 : count + 1);
}
} // end of while
System.out.println(counts);
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException
{
扫描仪输入文件=新扫描仪(新文件(“test.txt”);
映射计数=新的HashMap();
while(inputFile.hasNextLine()){
String line=inputFile.nextLine();
系统输出打印项次(行);
//上面是第一部分,在其中读取文件
//下面是第二部分,试着数一数
String[]words=line.split(“”);
for(字符串字:字){
整数计数=计数。获取(字);
counts.put(word,count==null?1:count+1);
}
}//时间结束
系统输出打印项次(计数);
}
你需要读单词,而不仅仅是台词
由于扫描仪中的默认分隔符可以正确分割每个单词,因此您可以尝试:
while (inputFile.hasNext()) {
String word = inputFile.next();
// do the same as before with word
}
你需要读单词,而不仅仅是台词
由于扫描仪中的默认分隔符可以正确分割每个单词,因此您可以尝试:
while (inputFile.hasNext()) {
String word = inputFile.next();
// do the same as before with word
}
inputFile.nextLine()
返回包含当前行单词的字符串。您要做的是将其拆分为字符串数组(您的单词),然后对其进行迭代。
看一看inputFile.nextLine()
返回一个包含当前行单词的字符串。您要做的是将其拆分为字符串数组(您的单词),然后对其进行迭代。
看看扫描仪输入文件=新扫描仪(新文件(“C:/Test/Test.txt”);
映射计数=新的HashMap();
while(inputFile.hasNextLine()){
String line=inputFile.nextLine();
for(字符串字:line.split(“”){
整数计数=计数。获取(字);
counts.put(word,count==null?1:count+1);
}
}
系统输出打印项次(计数);
使用Java7文件API,您可以按如下方式实现它
public static void main(String[] args) throws IOException{
List<String> allLines = Files.readAllLines(Paths.get("C:/Test/test.txt"), Charset.defaultCharset());
Map<String,Integer> charCount = new HashMap<String,Integer>();
for(String line:allLines){
String[] characters = line.split(" ");
for(String charac:characters){
Integer currentCount = charCount.get(charac);
charCount.put(charac, currentCount == null ? 1 : currentCount + 1);
}
}
System.out.println(charCount);
}
publicstaticvoidmain(字符串[]args)引发IOException{
列出allLines=Files.readAllLines(path.get(“C:/Test/Test.txt”)、Charset.defaultCharset();
Map charCount=new HashMap();
用于(字符串行:所有行){
String[]characters=line.split(“”);
for(字符串字符:字符){
整数currentCount=charCount.get(charac);
put(charac,currentCount==null?1:currentCount+1);
}
}
系统输出打印项次(字符数);
}
扫描仪输入文件=新扫描仪(新文件(“C:/Test/Test.txt”);
映射计数=新的HashMap();
while(inputFile.hasNextLine()){
String line=inputFile.nextLine();
for(字符串字:line.split(“”){
整数计数=计数。获取(字);
counts.put(word,count==null?1:count+1);
}
}
系统输出打印项次(计数);
使用Java7文件API,您可以按如下方式实现它
public static void main(String[] args) throws IOException{
List<String> allLines = Files.readAllLines(Paths.get("C:/Test/test.txt"), Charset.defaultCharset());
Map<String,Integer> charCount = new HashMap<String,Integer>();
for(String line:allLines){
String[] characters = line.split(" ");
for(String charac:characters){
Integer currentCount = charCount.get(charac);
charCount.put(charac, currentCount == null ? 1 : currentCount + 1);
}
}
System.out.println(charCount);
}
publicstaticvoidmain(字符串[]args)引发IOException{
列出allLines=Files.readAllLines(path.get(“C:/Test/Test.txt”)、Charset.defaultCharset();
Map charCount=new HashMap();
用于(字符串行:所有行){
String[]characters=line.split(“”);
for(字符串字符:字符){
整数currentCount=charCount.get(charac);
put(charac,currentCount==null?1:currentCount+1);
}
}
系统输出打印项次(字符数);
}
这将起作用。请注意,扫描仪会将每个单词与每行相对
public static void main (String[] args) throws FileNotFoundException
{
Scanner scanner = new Scanner("A B C D A A B C C");
Map<String, Integer> words = new HashMap<>();
String word;
// Loop through each word instead of each line
while (scanner.hasNext()) {
word = scanner.next();
// If the HashMap already contains the key, increment the value
if (words.containsKey(word)){
words.put(word, words.get(word) + 1);
}
// Otherwise, set the value to 1
else {
words.put(word, 1);
}
}
// Loop through the HashMap and print the results
for(Entry<String, Integer> entry : words.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + ": " + value);
}
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException
{
扫描仪=新扫描仪(“A B C D A B C C”);
Map words=newhashmap();
字符串字;
//循环遍历每个单词而不是每行
while(scanner.hasNext()){
word=scanner.next();
//如果HashMap已经包含该键,则增加该值
if(words.containsKey(word)){
words.put(word,words.get(word)+1);
}
//否则,将该值设置为1
否则{
字。放(字,1);
}
}
//循环浏览HashMap并打印结果
for(条目:words.entrySet()){
String key=entry.getKey();
整数值=entry.getValue();
System.out.println(键+“:”+值);
}
}
这将起作用。请注意,扫描仪会将每个单词与每行相对
public static void main (String[] args) throws FileNotFoundException
{
Scanner scanner = new Scanner("A B C D A A B C C");
Map<String, Integer> words = new HashMap<>();
String word;
// Loop through each word instead of each line
while (scanner.hasNext()) {
word = scanner.next();
// If the HashMap already contains the key, increment the value
if (words.containsKey(word)){
words.put(word, words.get(word) + 1);
}
// Otherwise, set the value to 1
else {
words.put(word, 1);
}
}
// Loop through the HashMap and print the results
for(Entry<String, Integer> entry : words.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + ": " + value);
}
}
publicstaticvoidmain(字符串[]a