为什么我得到这个与java方法相关的错误?

时间:2015-07-16 06:16:11

标签: java string methods replace char

初学者在这里寻找简单," dumbed down"回答,因为我仍然习惯于方法和java。

在我的程序中,我尝试删除用户先前使用前一个方法从至少4个字符的字符串输入的特定字母字符(例如charInput =' f')例如"狐狸幻想地冷冻在冷冻食品中#34;),产生一个新的字符串(例如stringInput =" oxes anciully reeze in rozen ood")。为此,我已经四处搜索,所以我正在尝试替换 (例如stringInput = stringInput.replace(charInput,""))。

不幸的是,我收到了这个错误:

The method replace(char, char) in the type String is not applicable for the arguments (char, String).

我尝试将用户输入的字符转换为字符串以查看是否有帮助,但没有区别。

有人能解释一下这里发生了什么(很简单)吗?还有一个可能解决这个错误的方法吗?感谢。

import java.util.*;

public class Foothill {
    // main method
    public static void main(String[] args) {
        char keyCharacter = getKeyCharacter();
        String theString = getString();
        maskCharacter(theString, keyCharacter);
        countKey(theString, keyCharacter);          
        removeCharacter(theString, keyCharacter);
    }

    // get keyCharacter
    public static char getKeyCharacter() {
        Scanner input = new Scanner(System.in);
        String firstPrompt, strKeyCharacter;
        char keyCharacter;

        do {
            firstPrompt = "Please enter a SINGLE character to act as key: ";
            System.out.print(firstPrompt);
            strKeyCharacter = input.nextLine();
            keyCharacter = strKeyCharacter.charAt(0);

            if (keyCharacter == 'a' || keyCharacter == 'b'
                    || keyCharacter == 'c' || keyCharacter == 'd'
                    || keyCharacter == 'e' || keyCharacter == 'f'
                    || keyCharacter == 'g' || keyCharacter == 'h'
                    || keyCharacter == 'i' || keyCharacter == 'j'
                    || keyCharacter == 'k' || keyCharacter == 'l'
                    || keyCharacter == 'm' || keyCharacter == 'n'
                    || keyCharacter == 'o' || keyCharacter == 'p'
                    || keyCharacter == 'q' || keyCharacter == 'r'
                    || keyCharacter == 's' || keyCharacter == 't'
                    || keyCharacter == 'u' || keyCharacter == 'v'
                    || keyCharacter == 'w' || keyCharacter == 'x'
                    || keyCharacter == 'y' || keyCharacter == 'z') {

                System.out.println("You entered: " + keyCharacter);
            }

        } while (keyCharacter != 'a' && keyCharacter != 'b'
                && keyCharacter != 'c' && keyCharacter != 'd'
                && keyCharacter != 'e' && keyCharacter != 'f'
                && keyCharacter != 'g' && keyCharacter != 'h'
                && keyCharacter != 'i' && keyCharacter != 'j'
                && keyCharacter != 'k' && keyCharacter != 'l'
                && keyCharacter != 'm' && keyCharacter != 'n'
                && keyCharacter != 'o' && keyCharacter != 'p'
                && keyCharacter != 'q' && keyCharacter != 'r'
                && keyCharacter != 's' && keyCharacter != 't'
                && keyCharacter != 'u' && keyCharacter != 'v'
                && keyCharacter != 'w' && keyCharacter != 'x'
                && keyCharacter != 'y' && keyCharacter != 'z');

        input.close();
        return keyCharacter;
    }

    // declare final = 4 to be constant
    public static final int minimumLength = 4;

    // get theString
    public static String getString() {
        Scanner input = new Scanner(System.in);
        String secondPrompt, theString;
        do {
            secondPrompt = "Please enter a phrase or sentence >= 4: ";
            System.out.print(secondPrompt);
            theString = input.next();
        } while (theString.length() < minimumLength || theString == null
                || theString.length() == 0);
        input.close();
        return theString;
    }
    // mask keyCharacter with $
    public static String maskCharacter(String theString, char keyCharacter) {
        theString = theString.replace(keyCharacter, '$');
        System.out.println("String with " + " '" + keyCharacter + "' " + " masked.");
        System.out.println(theString);
        return theString;
    }
    //count number of times keyCharacter occurs in theString
    public static void countKey(String theString, char keyCharacter) {
        int countChar = 0;
        for (int charTimes = 0; charTimes <theString.length(); charTimes++) {
            if (theString.charAt(charTimes) == keyCharacter) {
                countChar++;
            }
        }
        System.out.println( "It occurs " + countChar + " times.");
        return;    
    }
    // remove keyCharacter from theString
    public static void removeCharacter(String theString, char keyCharacter){
        //error line for below line            
        theString = theString.replace(keyCharacter, ""); 
        /*error line above: The method replace(char, char) in the type String is not applicable for the arguments (char, String)*/
        System.out.println("String with " + "'" + keyCharacter + "' removed:" +  '\n');
        System.out.println(theString)
        return;
    }
}

5 个答案:

答案 0 :(得分:1)

theString.replace(keyCharacter, ""); 

应该是

theString.replace(keyCharacter, ' '); 

或致电

theString.replaceAll(""+keyCharacter, ""); 

但是对于未来:问题在错误消息中指出:

replace(char, char)(你需要2个char参数)is not applicable for the arguments (char, String)(你有一个char和一个String作为参数 - &gt;你需要改变第二个参数......或者使用另一个方法)< / p>

答案 1 :(得分:1)

这是另一个简单的解决方案。使用theString.replace(String.valueOf(keyCharacter),“”);而不是theString.replace(keyCharacter,“”);

<强>释 在Java中,用单引号括起来的字符被视为字符,而用双引号括起来的字符是String,这就是为什么你得到replace()的编译错误。 有两个版本的替换。 一个替换(char oldChar,char newChar)方法可以替换给定字符的字符。
第二个替换(CharSequence目标,CharSequence替换)方法可以替换给定字符序列的字符序列。请注意'String'是一个字符序列

replaceAll()方法用给定的字符串替换字符串,其中可以传递正则表达式以匹配目标中的多个字符串

答案 2 :(得分:0)

 public static void removeCharacter(String theString, char keyCharacter){
    //error line for below line            
    theString = theString.replace(keyCharacter, ""); 

因为keyCharacter是一个字符而""表示string。所以这样写

 theString = theString.replaceAll(""+keyCharacter, ""); //going to replace all occurances of given string

 theString = theString.replace(""+keyCharacter, ""); 

space之间使用'

  theString = theString.replace(keyCharacter,' '); 

其他错误

System.out.println(theString)在这里使用分号

maskCharactercountKey方法?

答案 3 :(得分:0)

我会将多个编译时错误列为:

  1. maskCharacter(theString, keyCharacter);方法尚未定义
  2. countKey(theString, keyCharacter);方法尚未定义
  3. System.out.println(theString)您错过了那里的;分号
  4. theString.replace(keyCharacter, "");这是因为没有定义String.replace(char,String)方法
  5.   这里的

    键是""表示java中的空字符串,您应该使用' '作为空格字符。

    =========================

    同意Rahuls的建议

    我们应该使用replace(String.valueOf(keyCharacter), "");来避免使用theString.replace(keyCharacter, ' ');添加额外的空间。

    感谢您的建议

答案 4 :(得分:0)

您需要replace(String, String);方法。它允许您用空字符串替换字符(即删除字符或字符串)。方法replace(char, char);不能用于提供“空”字符。

theString = theString.replace("f", "");

编辑:如果您不需要正则表达式,请不要使用replaceAll