简体   繁体   English

Java初学者编程,将字符串放入char数组以过滤输入

[英]java beginner programming, putting strings in char array to filter input

in this code I've been trying to filter the characters in the array with a for-loop to a second array, but am unable to. 在这段代码中,我一直在尝试使用for循环将数组中的字符过滤到第二个数组,但是无法执行。 Could anyone tell me what is exactly wrong with my code? 谁能告诉我我的代码到底有什么问题?

public class Deel1 {

    public static void main(String[] args) {
        String zinInvoer = getInput();
        String zinUitvoer = filterZin(zinInvoer);
    }

    static String getInput() {
        Scanner scan = new Scanner(System.in);
        String zinInvoer = "";

        System.out.println("Voer een zin in: ");
        if (scan.hasNextLine()) {
            zinInvoer = scan.nextLine().trim();
        }

        if (zinInvoer.equals("")) {
            System.out.println("Geen invoer!");
            System.exit(0);
        }
        return zinInvoer;
    }

    static String filterZin(String zinInvoer) {
        String zinUitvoer = "";
        char ongefilterd[] = zinInvoer.toCharArray();
        String nogFilteren = new String(ongefilterd);
        char a = nogFilteren.charAt(97);
        for (a = 97; a <= 122; a++) {
            a = a += 32;  
            char gefilterd[] = //second array to be printed
        }
        System.out.println("Gefilterd: " + zinUitvoer);
        return zinInvoer;
    }
}

Sorry if it annoys you but I had to translate your variables into english in order to figure out what their purposes were. 抱歉让您烦恼,但是我必须将您的变量翻译成英文才能弄清楚它们的用途是什么。

First of all, it will always throw an exception when the string is less than 98 letters long because it looks for the 97th letter. 首先,当字符串长度少于98个字母时,它将始终引发异常,因为它会查找第97个字母。 Second, the for loop in "filterZin" will only filter letter # 98, which I am guessing was not your intention. 其次,“ filterZin”中的for循环将仅过滤字母#98,我猜这不是您的意图。 Also, geFilterd should probably be created outside of the for loop, and in the for loop you (i guess) would want to do 此外,geFilterd可能应该在for循环之外创建的,并在for循环,你(我猜的)会想这样做

geFilterd[a]=a+32;
a+=32;

Because I couldn't figure out what your overall goal was for this program, I made a version of it that does what I think you were trying to do, but again, I do not know. 因为我无法弄清楚您对该程序的总体目标是什么,所以我制作了该程序的一个版本,该版本可以实现您认为要执行的操作,但是同样,我也不知道。

import java.util.Scanner;

public class Deel1 {

public static void main(String[] args) {
    String phraseInput = getInput();
    filterPhrase(phraseInput);
}

static String getInput() {
    Scanner scan = new Scanner(System.in);
    String phraseInput = "";

    System.out.println("Voer een zin in: ");
    if (scan.hasNextLine()) {
        phraseInput = scan.nextLine().trim();
    }

    if (phraseInput.equals("")) {
        System.out.println("Geen invoer!");
        System.exit(0);
    }
    return phraseInput;
}

static String filterPhrase(String phraseInput) {
    String phraseOutput = "";
    char onFiltered[] = phraseInput.toCharArray();
    String currentFilter = new String(onFiltered);

//        for (a = 97; a <= 122; a++) {
//            a = a += 32;  
//            //char filtered[] = //second array to be printed
//        }
    char[] filtered = new char[26];
    for(int i=97;i<=122;i++){
        char a = currentFilter.charAt(i);
        filtered[i-97]= (char) (a+32);
    }
    System.out.println("filtered: " + filtered.toString());
    return phraseInput;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM