[英]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: 额外:
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像索引( 我不是,它很容易混合变量上) '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.