0

The error im getting is on Line 35 and it says "method english in class Project1 cannot be applied to given types. I don't understand and any help would be greatly appreciated. I think that I need to switch the variable type but I don't exactly know how. By the way I am new to java and trying to learn as fast as I can!

public class Project1
{
public static void main( String [] args )
{
    System.out.println();
    choice();

}
    public static void choice()
{
    int user_choice = 0; 
    user_choice = Input.getInt("Enter 1 if you want to change English to Morse code, and enter 2 to change Morse code to English");
    if(user_choice == 1)
    {
    String output = new String();
    String inital = new String();
    inital = english_to_morse();

    for( int k = 0; k < inital.length(); k++)
    {
        output += morse(inital.charAt( k ));
    }

        System.out.print(output);

    }
    if(user_choice == 2)
    {
    String output2 = new String();
    String inital2 = new String();
    inital2 = english_to_morse();

    for( int k = 0; k < inital2.length(); k++)
    {
        output2 += english(inital2.charAt( k ));///the error is here
    }

        System.out.print(output2);
    }
}

public static String english_to_morse() 
{
  String user_input = new String();

  user_input = Input.getString("Enter a phrase and I'll convert it to Morse Code");

  return user_input.toLowerCase();
}

public static String morse_to_english() 
{
  String user_input = new String();

  user_input = Input.getString("Enter a phrase in Morse Code and I'll convert it to English");

  return user_input.toLowerCase();
}

public static String morse(char letter)
{
    String output = new String();
    char[] alphabet_numbers = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ' };
    String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };

    for( int j = 0; j < alphabet_numbers.length; j++ )
    {
        if (alphabet_numbers[j]==letter)
        {
            output = morse_code[j];
        }
    }
    return output + " ";
}   
public static String english(String letter)
{
    String output = new String();
    String alphabet_numbers[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " " };
    String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };

    for( int j = 0; j < morse_code.length; j++ )
    {
        if (morse_code[j]==letter)
        {
            output = alphabet_numbers[j];
        }
    }
    return output + " ";
}   

}

  • 2
    Note: we don't know what the line 35 is. If you could, in the future, copy/paste the error message and indicates the line which cause the problem that would be helpful. Also look at the signature of `public static String english(String letter)` and with what argument you calls it. And see also [How do I compare Strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Alexis C. Mar 28 '14 at 23:00
  • Compare the type of the variable you are passing into the method to the one the method is expecting in its declaration. They need to match; that error says they don't. – Reinstate Monica -- notmaynard Mar 28 '14 at 23:01
  • i said where the error was now with a comment – user3474526 Mar 28 '14 at 23:02
  • @user3474526 Look at the signature of the english method and with what parameters you call it. – Alexis C. Mar 28 '14 at 23:03

2 Answers2

1

You can not directly convert char into String. Use String.valueOf() for this.

You had this line where your method english() accepts String as parameter, but you were passing char to it.

output2 += english(inital2.charAt( k ));

But it should be:

output2 += english(String.valueOf(inital2.charAt( k )));
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
1

The english method takes a String as an argument. The call, output += english(inital.charAt( k ));, attempts to give it a char as input. You may want to use inital.substring(k, k+1) instead of inital.charAt(k) to provide a one character String as an argument, instead of a char.

Warren Dew
  • 8,790
  • 3
  • 30
  • 44