Shimy
Shimy

Reputation: 53

String not equaling one another

This program will read each phone number from the telephones.txt file and will check if it can be translated into one or more words of words.txt file. The output of the program will contain the telephone numbers and their word representatives. (assuming there are words and phones in the file)

while(phones.hasNext())
{
    number = phones.next();
    number = number.replace("-","");
    //Scans for next word string
    System.out.print(number + " ");
    Scanner words = new Scanner(new File("words.txt"));     
    while(words.hasNext())
    {
        code = words.next();
        char[] wordChars = null;
        wordChars = code.toCharArray();
        output = "";

        for(char wordChar : wordChars)
        {
            output = output.concat(new String(convert(wordChar)));
        }

        if(number.equals(output))
        {
            end += code;
            System.out.print(code);
            if(end.isEmpty() && end !=null)
            {
                System.out.print("NONE");
            }
        }
    }
    System.out.println();

}
}

Assume the words in the file are CBCNEWS , CTVNEWS and the numbers are 2251493 and 2226397 if(number.equals(output)); { System.out.println(number + " " + code); } this code isn't printing only when the string number equals the string input instead it always prints. What condition do i have to make it so that it will only print when the condition is met and will also continue to look for more words

how i converted the string code was using this:

    public static char[] convert(char c)
{
    switch(c)
    {
        case 'A' :
        case 'B' :
        case 'C' :
            return new char[]{'2'};
        case 'D' :
        case 'E' :
        case 'F' :
            return new char[]{'3'};
        case 'G' :
        case 'H' :
        case 'I' :
            return new char[]{'4'};
        case 'J' :
        case 'K' :
        case 'L' :
            return new char[]{'5'};
        case 'M' :
        case 'N' :
        case 'O' :
            return new char[]{'6'};
        case 'P' :
        case 'R' :
        case 'S' :
            return new char[]{'7'};
        case 'T' :
        case 'U' :
        case 'V' :
            return new char[]{'8'};
        default:
            return new char[]{'9'};
    }
}

Upvotes: 1

Views: 135

Answers (2)

David Lau
David Lau

Reputation: 220

Just a suggestion: You may consider to change your lengthy convert method implementation by a simpler ASCII function or HashMap implementation, for example:

public static char[] convert(char c){
     int check = (c - 'A');
     if(check<3){ return new char[]{'2'};}
     else if(check>2 && check<6){ return new char[]{'3'};}
     else if(check>5 && check<9){ return new char[]{'4'};}
     else if(check>8 && check<12){ return new char[]{'5'};}
     else if(check>11 && check<15){ return new char[]{'6'};}
     else if(check>14 && check<19){ return new char[]{'7'};}
     else if(check>18 && check<22){ return new char[]{'8'};}
     else{ return new char[]{'9'};}
}

Did you miss a 'Q' for case char'7'?

Upvotes: 0

Kieveli
Kieveli

Reputation: 11075

Semi-colon after your if.

        if(number.equals(output));
        {
            System.out.println(number + " " + code);
        }

Should be:

        if(number.equals(output))
        {
            System.out.println(number + " " + code);
        }

Upvotes: 3

Related Questions