Ich habe gerade versucht einen Code zu schreiben, um eine Anzahl an Zeichen auslesen zu können. Das klappt auch alles so weit.
Wenn ich nun aber irgendwo einen Text kopiere und den einfüge, dann erscheint immer eine 0. Liegt wohl daran, dass der eingefügte Text nicht akzeptiert wird. Was muss ich beachten?
Wenn ich nun aber irgendwo einen Text kopiere und den einfüge, dann erscheint immer eine 0. Liegt wohl daran, dass der eingefügte Text nicht akzeptiert wird. Was muss ich beachten?
Java:
class Counter {
public static int letter( String text ) {
int count = 0;
for ( int i = 0; text.length() >= i; i++ ) {
char myletter = text.charAt(i);
if ( Character.isLetter( myletter ) ) {
count++;
}
}
return count;
}
public static int numbers( String text ) {
int count = 0;
for ( int i = 0; text.length() - 1 >= i; i++ ) {
char myletter = text.charAt(i);
if ( Character.isDigit( myletter ) ) {
count++;
}
}
return count;
}
public static int specialcharacter ( String text ) {
int count = 0;
for ( int i = 0; text.length() - 1 >= i; i++ ) {
char myletter = text.charAt(i);
if ( myletter == '!' || myletter == '?' || myletter == '.' || myletter == '"' || myletter == '$' || myletter == '&' || myletter == '*' || myletter == '-' || myletter == ':' || myletter == ';' || myletter == ',' ) {
count++;
}
}
return count;
}
public static int numberordigit( String text ) {
int count = 0;
for ( int i = 0; text.length() - 1 >= i; i++ ) {
char myletter = text.charAt(i);
if ( Character.isLetterOrDigit( myletter ) ) {
count++;
}
}
return count;
}
public static int whitespace( String text ) {
int count = 0;
for ( int i = 0; text.length() - 1 >= i; i++ ) {
char myletter = text.charAt(i);
if ( Character.isWhitespace( myletter ) ) {
count++;
}
}
return count;
}
public static int upper( String text ) {
int count = 0;
for ( int i = 0; text.length() - 1 >= i; i++ ) {
char myletter = text.charAt(i);
if ( Character.isUpperCase( myletter ) ) {
count++;
}
}
return count;
}
public static int lower( String text ) {
int count = 0;
for ( int i = 0; text.length() - 1 >= i; i++ ) {
char myletter = text.charAt(i);
if ( Character.isLowerCase( myletter ) ) {
count++;
}
}
return count;
}
public static void main( String[] args ) {
System.out.println( "Welcher Text soll genutzt werden?" );
String text = new java.util.Scanner( System.in ).nextLine();
System.out.println( "Was soll gezählt werden?" );
String typecounter = new java.util.Scanner( System.in ).nextLine();
switch ( typecounter ) {
case "Alles":
System.out.println( numberordigit( text ) + whitespace( text ) + specialcharacter( text ) );
break;
case "Buchstaben":
System.out.println( letter( text ) );
break;
case "Zahlen":
System.out.println( numbers( text ) );
break;
case "Zahlen & Buchstaben":
System.out.println( numberordigit( text ) );
break;
case "Leerzeichen":
System.out.println( whitespace( text ) );
break;
case "Großbuchstaben":
System.out.println( upper( text ) );
break;
case "Kleinbuchstaben":
System.out.println( lower( text ) );
break;
default:
System.out.println( "Die Kategorie konnte nicht gefunden werden!" );
}
}
}