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.
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 :)
Consider the following line:
char c = zinArray[i];
Assigning copies the value (or the reference, in case of a class instance). So you have created a copy of the character at zinArray[i]. This means that changing the value of the variable c will not change the value stored in zinArray[i]. You have to perform the change to the array item, like this:
zinArray[i] = (char)(c + 32);
Change the code to this :
c = (char)(c + 32);
zinArray[i]=c;
Basically you are converting uppercase to lower case. That part was right but you are not storing the lower case back to the array that's why it is not showing in output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With