Java – convert sentences to arrays and delete characters and print new sentences

Disclaimer: This is a homework assignment

The goal of the program is to propose a sentence and then: – convert uppercase to lowercase (do not use. Tolowercase()) – delete all characters other than A-Z, A-Z and 0-9 – print a new sentence –... Something more important but less important

OK, what did I do

>I convert my string (sentence) to a char array. > I created a for loop to iterate over all the characters in my array > If char is uppercase, I convert it to lowercase using ASCII

My problem is: – it looks like I changed char c, but it's not stored in lowercase in my array? – How do I detect an disallowed character and remove it 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) {
      }

    }
    }
}

Who can point me in the right direction?

Thank you:)

Solution

Consider the following lines:

char c = zinArray[i];

Assign duplicate values (references if class instances) So you created a copy of the character in zinarray [i] This means that changing the value of variable C does not change the value stored in zinarray [i] You must make changes to the array items as follows:

zinArray[i] = (char)(c + 32);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>