Advertisement
Garro

Java - Contador de caracteres

Jan 2nd, 2012
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. /* Programa en java que solicite al usuario una frase, y que el programa
  2.  * cuente cuantas minusculas, mayusculas, numeros y caracteres que no
  3.  * incluyan los anteriores ni los espacios. */
  4. import java.util.*;
  5. public class contar_caracteres{
  6.     public static void main (String args[]) {
  7.         String frase = validador();
  8.         contador(frase);
  9.     }
  10. public static String validador(){
  11.     Scanner tecvalidador = new Scanner(System.in);
  12.     boolean noSirve = true;
  13.     String frase = "";
  14.     while (noSirve){
  15.         System.out.print("Ingrese una frase: ");
  16.         frase = tecvalidador.nextLine();
  17.         noSirve = validaQueNoSeaNula(frase);
  18.         if (noSirve==false){
  19.             noSirve = validaLosEspacios(frase);
  20.         }
  21.     }return frase;
  22. }
  23. public static boolean validaQueNoSeaNula(String X){
  24.     if(X.length()==0){
  25.         System.out.println("Error! Frase nula.");
  26.         return true;
  27.     }else{
  28.         return false;
  29.     }
  30. }
  31. public static boolean validaLosEspacios(String garro){
  32.     for(int i=0; i<garro.length(); i++){
  33.         if (garro.charAt(i)!=' '){
  34.             return false;
  35.         }
  36.     }System.out.println("Error! Frase contiene solo espacios.");
  37.     return true;
  38. }
  39. public static void contador(String garro){
  40.     int c=0, f=0, pene=0, poto=0;
  41.     char x;
  42.     for(int i=0; i<garro.length(); i++){
  43.         x=garro.charAt(i);
  44.         if (x>='0' && x<='9'){
  45.             c++;
  46.         }else if (x>='a' && x<='z'){
  47.             f++;
  48.         }else if (x>='A' && x<='Z'){
  49.             pene++;
  50.         }else if (x!=' '){
  51.             poto++;
  52.         }
  53.     }System.out.println("La cantidad de numeros es: "+c);
  54.     System.out.println("La cantidad de letras minusculas es: "+f);
  55.     System.out.println("La cantidad de letras mayusculas es: "+pene);
  56.     System.out.println("La cantidad de otros caracteres es: "+poto);
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement