简体   繁体   English

获取用户输入,放入数组并打印出每个字母的使用次数

[英]Take user input, put into an array and print out how many times each letter is used

I am trying to write this program so that when the user inputs a line of text they are given a chart showing how many times each letter is used. 我正在尝试编写这个程序,以便当用户输入一行文本时,会给出一个图表,显示每个字母的使用次数。 I broke it up into an array but I kept getting an error for "counts[letters[a] == 'a']++;" 我把它分成了一个数组,但我不断收到“计数[字母[a] =='a'] ++;”的错误 saying i can't convert a string to a char or a boolean to a int, depending on the way I put it. 说我不能将字符串转换为char或布尔值转换为int,具体取决于我的方式。 I can't figure out why it's not all char. 我无法弄清楚为什么它不是所有的char。

import java.util.*;

public class AnalysisA { //open class

public static String input;
public static String stringA;

public static void main (String args []) { //open main 

    System.out.println("Please enter a line of text for analysis:");

    Scanner sc = new Scanner(System.in);
    input = sc.nextLine();
    input = input.toLowerCase();

    System.out.println("Analysis A:");//Analysis A
    System.out.println(AnalysisA(stringA)); 

} // close main 

public static String AnalysisA (String stringA) { // open analysis A

   stringA = input;

   char[] letters = stringA.toCharArray();

   int[] counts = new int[26];

   for (int a = 0; a < letters.length; a++) { //open for 
     counts[letters[a] == 'a']++;
     System.out.print(counts);
   } //close for 
}

The expression letters[a] == 'a' results in a boolean answer (1 or 0), but you have that indexing an array, which must be an int. 表达式letters[a] == 'a'产生一个布尔答案(1或0),但你有一个索引数组,它必须是一个int。

What you're basically telling Java is to do counts[true]++ or counts[false]++ , which makes no sense. 你基本上告诉Java的是做counts[true]++counts[false]++ ,这是没有意义的。

What you really want is a HashMap that maps each character to the amount of times you saw it in the array. 你真正想要的是一个HashMap ,它将每个字符映射到你在数组中看到它的次数。 I won't put the answer here, but look up HashMaps in Java and you'll find the clues you need. 我不会在这里给出答案,但是在Java中查找HashMaps,你会找到你需要的线索。

counts[___] expect an Integer index, whereas your expression letters[a] == 'a' return a boolean count [___]期望一个Integer索引,而你的表达式letters[a] == 'a'返回一个布尔值

Im guessing you're trying to increment your 'Dictionary' value by 1 each time a letter is met. 我猜你每次收到一封信时都试图将'Dictionary'值增加1。 You can get the index by making letters[a] - 'a' 您可以通过制作letters[a] - 'a'来获取索引

Because of the order in the ASCII table , letter 'a' which equal 97 if subtracted to another letter, say 'b' which is 98, will produce the index 1, which is the correct position for your base26 'Dictionary' 由于ASCII表中的顺序,如果减去另一个字母,则字母'a'等于97,如果'b'为98,则将产生索引1,这是你的base26'字典'的正确位置

Extra: 额外:

  • You should use for (int i = ... for indexing ( i instead of a, its easy to mixed variables up if you name your i ndex like that) 您应该使用for (int i = ...如果你的名字你 ndex像索引( 不是,它很容易混合变量上)
  • You must make sure all the characters are lower-case before you start doing this, because as you can see in the table above 'B' - 'a' and 'b' - 'a' are 2 very different things. 在开始这样做之前,你必须确保所有的字符都是小写的,因为你可以在上面的表格中看到'B' - 'a''b' - 'a'是两个截然不同的东西。

If you use a Map you can do this easily without complicating.. 如果你使用地图,你可以轻松地做到这一点,而不会复杂化..

Map<Character, Integer> map = new HashMap<Character, Integer>();


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class AnalysisA { // open class

    public static String input;

    public static void main(String args[]) { // open main

        System.out.println("Please enter a line of text for analysis:");

        Scanner sc = new Scanner(System.in);
        input = sc.nextLine();
        input = input.toLowerCase();
        sc.close();
        System.out.println("Analysis A:");// Analysis A
        System.out.println(Analysis());

    } // close main

    public static String Analysis() { // open analysis A

        Map<Character, Integer> map = new HashMap<Character, Integer>();
        char[] letters = input.toCharArray();
        Integer count;
        for (char letter : letters) {
            count = map.get(letter);
            if (count == null || count == 0) {
                map.put(letter, 1);
            } else {
                map.put(letter, ++count);
            }
        }
        Set<Character> set = map.keySet();
        for (Character letter : set) {
            System.out.println(letter + " " + map.get(letter));
        }
        return "";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何输入并让程序在新行上打印出每个字母,同时遇到每个空格以打印出“ <space> “? - How do I take an input and make the program print out each letter on a new line while making every space encounter to print out “<space>”? 我将如何接受TextField输入,接受每个字母并将其放入字符中? - How would I take a TextField input, take each letter, and put it into chars? 如何多次获取用户输入并将其存储在同一个数组列表中? - How to take user input multiple times and store it in the same array list? 确定每个整数出现在输入(数组)中的次数 - determining how many times each integer appears in the input (array) 如何将多行 txt 添加到数组中并将每个字母与用户输入的字母进行比较? - How do I add multiple lines of txt into an array and compare each letter to a user-input letter? 如何创建一组空数组并用用户输入填充并打印出来? - How to create a empty set of array and fill it with user input and print it out? 如何从用户输入日期中抽出时间? - How to take time out of user input date? 如何获取输入并保存到数组中,然后打印数组输入? - How to take input and save into an array and then print the array input? 使用一维数组编写一个程序,该数组具有来自随机生成器的值,以打印出每个组合被一对骰子滚动多少次 - Write a program using 1-D arrays with values from a random generator to print out how many times each combination was rolled by a pair of dice 编写程序以打印每个字母以及用户输入中出现了多少个字母 - Write a program to print each and every alphabet with how many occured in a user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM