DISCLAIMER: This is a homework assignment
Goal of the program is: ask a sentence and then: - convert upper to lowercase (without using .toLowercase() ) - remove all characters that are not a-z, A-Z and 0-9 - print new sentence - ... something more BUT not important for.
Ok, so what I did.
- I converted my String (sentence) to a char array.
- I created a for loop to loop through all chars in my array
- If a char is a uppercase I converted it to a lowercase using ASCII
The problem I experience is: - It looks like I change the char C but it's NOT stored as a lowercase in my array? - How do I detect the non-allowed characters, and delete this from my array?
My code:
import java.util.Scanner;
public class sentence {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String zin = "";
System.out.print("Voer een zin in: ");
if (scanner.hasNextLine())
zin = scanner.nextLine().trim();
if (zin.equals("")) {
System.out.print("Geen Invoer!");
System.exit(0);
}
char[] zinArray = zin.toCharArray();
for (int i = 0; i < zinArray.length; i++) {
char c = zinArray[i];
if (c >= 'A' && c <= 'Z') {
c = (char)(c + 32);
} else if (c >= 58 && c <= 64) {
} else if (c >= 91 && c <= 96) {
} else if (c 123 && c <= 126) {
}
}
}
}
Can anyone point me in the right direction?
Thanks :)