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']++;" saying i can't convert a string to a char or a boolean to a int, depending on the way I put it. I can't figure out why it's not all 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.
What you're basically telling Java is to do counts[true]++
or counts[false]++
, which makes no sense.
What you really want is a HashMap
that maps each character to the amount of times you saw it in the array. I won't put the answer here, but look up HashMaps in Java and you'll find the clues you need.
counts[___] expect an Integer index, whereas your expression letters[a] == 'a'
return a boolean
Im guessing you're trying to increment your 'Dictionary' value by 1 each time a letter is met. You can get the index by making 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'
Extra:
for (int i = ...
for indexing ( i instead of a, its easy to mixed variables up if you name your i ndex like that) 'B' - 'a'
and 'b' - 'a'
are 2 very different things. 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 "";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.