错误:类中的构造函数不能应用于给定类型

时间:2014-05-04 03:54:55

标签: java string

编写一个代码,该代码将字符串作为电话号码,在数字位置加上字母,然后将该字符串解码为数字。当我编写测试人员类时,它告诉我来自我的其他类的构造函数不能应用于我的测试用例中的给定类型。

这是代码的主要部分。

public class PhoneNumber
{
    String phoneNumber;
    String telNumber;
    public PhoneNumber(String num, String telNum)
    {
        phoneNumber = num;
        telNumber = telNum;
    }

    public String decodePhoneNumber()
    {
        //Takes string form phone number and decodes based on number pad
        //Find code that makes if statement not care about caps
        //so if a || b || c number[cnt] = 1 etc..
        for (int cnt = 0; cnt < phoneNumber.length(); cnt++)
        {
            char num = phoneNumber.toLowerCase().charAt(cnt);
            if ((num == 'a') || (num == 'b') || (num == 'c'))
            {
                //number is 2
                telNumber = telNumber + "2";
            }
            else if ((num == 'd') || (num == 'e') || (num == 'f'))
            {
                //number is 3
                telNumber = telNumber +"3";
            }
            else if ((num == 'g') || (num == 'h') || (num == 'i'))
            {
                //number is 4
                telNumber = telNumber +"4";
            }
            else if ((num == 'j') || (num == 'k') || (num == 'l'))
            {
                //number is 5
                telNumber = telNumber +"5";
            }
            else if ((num == 'm') || (num == 'n') || (num == 'o'))
            {
                //number is 6
                telNumber = telNumber +"6";
            }
            else if ((num == 'p') || (num == 'q') || (num == 'r') || (num == 's'))
            {
                //number is 7
                telNumber = telNumber +"7";
            }
            else if ((num == 't') || (num == 'u') || (num == 'v'))
            {
                //number is 8
                telNumber = telNumber +"8";
            }
            else
            {
                //number is 9
                telNumber = telNumber +"9";
            }
        }
        return telNumber;
    }
}

这是我的测试类:

public class TestPhoneNumber
{
    public static void main(String []args)
    {
        PhoneNumber ph1 = new PhoneNumber("1-800-ILOVENY");
        System.out.println("Decoded phone number: " +ph1.decodedPhoneNumber());
    }
}

错误来自测试类中的第5行。

1 个答案:

答案 0 :(得分:1)

PhoneNumber类构造函数有两个字符串参数public PhoneNumber(String num, String telNum)传递另一个字符串参数或创建另一个带有一个String参数的构造函数。

像这样初始化对象

PhoneNumber ph1 = new PhoneNumber("XXXXX","1-800-ILOVENY");

或在PhoneNumber

中创建一个String参数的另一个构造函数
public PhoneNumber( String telNum)
    {

        telNumber = telNum;
    }