package de.codegym.task.task10.task1012;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Buchstabenanzahl
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// Alphabet
String abc = "abcdefghijklmnopqrstuvwxyz";
char[] abcArray = abc.toCharArray();
ArrayList<Character> alphabet = new ArrayList<>();
for (char buchstabe : abcArray) {
alphabet.add(buchstabe);
}
char[][] solution = new char[26][2];
for(int i=0;i<26;i++) {
solution[i][0]=abcArray[i];
solution[i][1]='0';
}
// Zeichenketten einlesen
ArrayList<String> liste = new ArrayList<>();
for (int i = 0; i < 10; i++) {
String s = reader.readLine();
liste.add(s.toLowerCase());
String temp = liste.get(i);
for(int a=0;a<temp.length();a++) {
char c = temp.charAt(a);
for(int p=0;p<abcArray.length;p++) {
if(c==abcArray[p]) {
solution[p][1]++;
}
}
}
}
int u =0;
for(int i=0;i<solution.length;i++) {
System.out.print(solution[i][u]+" ");
u=1;
System.out.println(solution[i][u]);
u=0;
}
// schreib hier deinen Code
}
}
Please Help
Gelöst
Kommentare (4)
- Beliebt
- Neu
- Alt
Du musst angemeldet sein, um einen Kommentar schreiben zu können
Pablo
22 April 2020, 10:44
I just changed the char[][] array to a int[] array for counting the amounts of letters but it still gives me the same feedback. My code now is creating a int[] array of 26 length and filling every element with 0. Later every word is checked for the inserted laters and it adds +1 to the corresponding index.
int[] solution = new int[26];
for(int i=0;i<26;i++) {
solution[i]=0;
}
ArrayList<String> liste = new ArrayList<>();
for (int i = 0; i < 10; i++) {
String s = reader.readLine();
liste.add(s.toLowerCase());
String temp = liste.get(i);
for(int a=0;a<temp.length();a++) {
char c = temp.charAt(a);
for(int p=0;p<abcArray.length;p++) {
if(c==abcArray[p]) {
solution[i]++;
}
}
}
}
for(int i=0;i<solution.length;i++) {
{
System.out.print(abcArray[i]+" ");
System.out.println(solution[i]);
}
0
dnlklnhfr
22 April 2020, 17:59
that's just because of a copy&paste-error in line 40 (of original code).
you should use "p" instead of "i" as array-index-variable
0
Pablo
23 April 2020, 10:00
Tank you very much. the I was the mistake.
0
dnlklnhfr
22 April 2020, 08:08
It might be tempting to put both, characters and counts, into the same array and at a first sight miraculously it seems to work, but this is considered to be more a kind of misuse of a datatype.
Try not to use a char for counting the occurrences of the characters (line 23), then it will work.
0