Go through String with a for loop and count the letters, doubts, Java

1

I'm stuck in an exercise and it does not work out. I put the statement and where I got stuck.

The statement is as follows: "Make a program that asks me for a string (String) and take me by console all the words that have that string, in addition to saying how many times each of the characters that have the string appear. Use the subString, charAt and length methods for this ".

I have the following code:

Scanner sc = new Scanner(System.in);
        sc.useDelimiter("\n");
        System.out.println("Introduce una frase");
        String cadena = sc.next();
        int a = 0;
        int A = 0;
        int b = 0;
        int B = 0;
        int c = 0;
        int C = 0;
        int d = 0;
        int D = 0;
        int e = 0;
        int E = 0;
        int f = 0;
        int F = 0;
        int g = 0;
        int G = 0;
        int h = 0;
        int H = 0;
        int i = 0;
        int I = 0;
        int j = 0;
        int J = 0;
        int k = 0;
        int K = 0;
        int l = 0;
        int L = 0;
        int m = 0;
        int M = 0;
        int n = 0;
        int N = 0;
        int ñ = 0;
        int Ñ = 0;
        int o = 0;
        int O = 0;
        int p = 0;
        int P = 0;
        int q = 0;
        int Q = 0;
        int r = 0;
        int R = 0;
        int s = 0;
        int S = 0;
        int t = 0;
        int T = 0;
        int u = 0;
        int U = 0;
        int v = 0;
        int V = 0;
        int w = 0;
        int W = 0;
        int x = 0;
        int X = 0;
        int y = 0;
        int Y = 0;
        int z = 0;
        int Z = 0;

        for (int contador = 0; contador < cadena.length(); contador++) {
            if (cadena.charAt(i) == 'a') {
                a++;
            }
            if (cadena.charAt(i) == 'A') {
                A++;
            }
            if (cadena.charAt(i) == 'b') {
                b++;
            }
            if (cadena.charAt(i) == 'B') {
                B++;
            }
            if (cadena.charAt(i) == 'c') {
                c++;
            }
            if (cadena.charAt(i) == 'C') {
                C++;
            }
            if (cadena.charAt(i) == 'd') {
                d++;
            }
            if (cadena.charAt(i) == 'D') {
                D++;
            }
            if (cadena.charAt(i) == 'e') {
                e++;
            }
            if (cadena.charAt(i) == 'E') {
                e++;
            }
            if (cadena.charAt(i) == 'f') {
                f++;
            }
            if (cadena.charAt(i) == 'F') {
                F++;
            }
            if (cadena.charAt(i) == 'g') {
                g++;
            }
            if (cadena.charAt(i) == 'G') {
                G++;
            }
            if (cadena.charAt(i) == 'h') {
                h++;
            }
            if (cadena.charAt(i) == 'H') {
                H++;
            }
            if (cadena.charAt(i) == 'i') {
                i++;
            }
            if (cadena.charAt(i) == 'I') {
                I++;
            }
            if (cadena.charAt(i) == 'j') {
                j++;
            }
            if (cadena.charAt(i) == 'J') {
                J++;
            }
            if (cadena.charAt(i) == 'k') {
                k++;
            }
            if (cadena.charAt(i) == 'K') {
                K++;
            }
            if (cadena.charAt(i) == 'l') {
                l++;
            }
            if (cadena.charAt(i) == 'L') {
                L++;
            }
            if (cadena.charAt(i) == 'm') {
                m++;
            }
            if (cadena.charAt(i) == 'M') {
                M++;
            }
            if (cadena.charAt(i) == 'n') {
                n++;
            }
            if (cadena.charAt(i) == 'N') {
                N++;
            }
            if (cadena.charAt(i) == 'ñ') {
                ñ++;
            }
            if (cadena.charAt(i) == 'Ñ') {
                Ñ++;
            }
            if (cadena.charAt(i) == 'o') {
                o++;
            }
            if (cadena.charAt(i) == 'O') {
                O++;
            }
            if (cadena.charAt(i) == 'p') {
                p++;
            }
            if (cadena.charAt(i) == 'P') {
                P++;
            }
            if (cadena.charAt(i) == 'q') {
                q++;
            }
            if (cadena.charAt(i) == 'Q') {
                Q++;
            }
            if (cadena.charAt(i) == 'r') {
                r++;
            }
            if (cadena.charAt(i) == 'R') {
                R++;
            }
            if (cadena.charAt(i) == 's') {
                s++;
            }
            if (cadena.charAt(i) == 'S') {
                S++;
            }
            if (cadena.charAt(i) == 't') {
                t++;
            }
            if (cadena.charAt(i) == 'T') {
                T++;
            }
            if (cadena.charAt(i) == 'u') {
                u++;
            }
            if (cadena.charAt(i) == 'U') {
                U++;
            }
            if (cadena.charAt(i) == 'v') {
                v++;
            }
            if (cadena.charAt(i) == 'V') {
                V++;
            }
            if (cadena.charAt(i) == 'w') {
                w++;
            }
            if (cadena.charAt(i) == 'W') {
                W++;
            }
            if (cadena.charAt(i) == 'x') {
                x++;
            }
            if (cadena.charAt(i) == 'X') {
                X++;
            }
            if (cadena.charAt(i) == 'y') {
                y++;
            }
            if (cadena.charAt(i) == 'Y') {
                Y++;
            }
            if (cadena.charAt(i) == 'z') {
                z++;
            }
            if (cadena.charAt(i) == 'Z') {
                Z++;
            }

        }

            System.out.println("La frase tiene el siguiente número de letras");
            System.out.println("a: " + a);
            System.out.println("A: " + A);
            System.out.println("b: " + b);
            System.out.println("B: " + B);
            System.out.println("c: " + c);
            System.out.println("C: " + C);
            System.out.println("d: " + d);
            System.out.println("D: " + D);
            System.out.println("e: " + e);
            System.out.println("E: " + E);
            System.out.println("f: " + f);
            System.out.println("F: " + F);
            System.out.println("g: " + g);
            System.out.println("G: " + G);
            System.out.println("h: " + h);
            System.out.println("H: " + H);
            System.out.println("i: " + i);
            System.out.println("I: " + I);
            System.out.println("j: " + j);
            System.out.println("J: " + J);
            System.out.println("k: " + k);
            System.out.println("K: " + K);
            System.out.println("l: " + l);
            System.out.println("L: " + L);
            System.out.println("m: " + m);
            System.out.println("M: " + M);
            System.out.println("n: " + n);
            System.out.println("N: " + N);
            System.out.println("ñ: " + ñ);
            System.out.println("Ñ: " + Ñ);
            System.out.println("o: " + o);
            System.out.println("O: " + O);
            System.out.println("p: " + p);
            System.out.println("P: " + P);
            System.out.println("q: " + q);
            System.out.println("Q: " + Q);
            System.out.println("r: " + r);
            System.out.println("R: " + R);
            System.out.println("s: " + s);
            System.out.println("S: " + S);
            System.out.println("t: " + t);
            System.out.println("T: " + T);
            System.out.println("u: " + u);
            System.out.println("U: " + U);
            System.out.println("v: " + v);
            System.out.println("V: " + V);
            System.out.println("w: " + w);
            System.out.println("W: " + W);
            System.out.println("x: " + x);
            System.out.println("X: " + X);
            System.out.println("y: " + y);
            System.out.println("Y: " + Y);
            System.out.println("z: " + z);
            System.out.println("Z: " + Z);
        }

    }

I have not yet done the process of separating the words, although for this I have put the "useDelimiter". With what I'm stuck (and grated, I've been spinning for several hours but I do not know what I'm wrong) is that I do not know what bug, because I execute this code with the following example sentence "My city is beautiful" and I add all the letters to M instead of adding the letters to each letter that corresponds (that is, M: 1, i: 3 etc.). Thank you very much, best regards.

    
asked by Sergio 28.12.2018 в 21:07
source

2 answers

1

This exercise can be done in many ways ... for example you can use a Switch (which is better option than a lot of if's, even many IDE's detect this and replace it if's by switch) to verify the letter (numbers, letters, Enums, and from java 7 Strings ):

assuming the existence of a int contadores[] array that contains as many spaces as letters need to be counted:

switch (letra) {
        case 'a':  contadores[<index de a>]= contadores[<index de a>]++ ;
                 break;
        case 'A': contadores[<index de A>]= contadores[<index de A>]++ ;
                 break;
        case 'b':  contadores[<index de b>]= contadores[<index de b>]++ ;
                 break;
        //..... etc. 
}

Another option that simplifies the code is through the use of a collection in particular from java.util.map family, HashMap :

//tamanio inicial 27 caracteres x 2 (mayus+minus) 
HashMap<Character, Integer> Contador = new HashMap<>(27 * 2);
String cadena = sc.nextLine();
if (cadena != null && !cadena.trim().isEmpty()) {
    for (char current : cadena.toCharArray()) {
        if (Character.isLetter(current) && ((current >= 'a' && current <= 'z') || (current >= 'A' && current <= 'Z'))) {
            //agrega o actualiza el valor en el Map si el valor no existe use 0
            Contador.put(current, Contador.getOrDefault(current, 0) + 1);
        }
    }
} else {
    System.out.println("No hay informacion!");
}
System.out.println("La frase tiene el siguiente número de letras");
Contador.forEach( (key, value)->{System.out.println(String.format("La letra '%s' aparece %d veses en el texto",key,value));});

and in this way you do not have to put 54 case statements or 54 if 's, so read the Java Tutorial of how to use Collections and likewise how to use Arrays  With regard to the count / separate words part of the sentence please refer to this question: How to separate a String in Java. How to use split ()

    
answered by 29.12.2018 в 00:04
-1

Mount two loops:

  • in the first you go through the string
    • for each letter you take
      • you search on a map to see if you already have the letter
      • if you do not have it, add the entry to the map * then add one, if you have it, just add up
  • If during this loop you write down the position you occupy the first time
  • and then, when you find a space, you can use substring to get the word and print it
answered by 28.12.2018 в 21:23