Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

questions about Morse Code translater

807591Mar 17 2008 — edited Mar 19 2008
Hi, everyone! I am writing a program to translate English text to morse code. I need to use space to seperate between characters, but not necessarily to seperate words with forwars slash(/).
And here is the code I wrote:
import java.util.*;

public class MorseCode
{
  public static void main(String[]args)
  {
      Scanner s = new Scanner(System.in);
      System.out.println("Please input the English text you want to translate:");
      String input = s.nextString();
      s.nextLine();
      MorseCode x = MorseCode();
                                
      String result = x.encode(input);
      System.out.println("The result is:" + result);
    }
    public static String encode(String input)
    {
        String input1 = input.toLowerCase();
        for(int i=0; i<input1.length(); i++)
        char c = input1.charAt(i);
        switch(c)
        {
            case a:
            System.out.print(".- ");
            break;
            case b:
            System.out.print("-... ");
            break;
            case c:
            System.out.print("-.-. ");
            break;
            case d:
            System.out.print("-.. ");
            break;
            case e:
            System.out.print(". ");
            break;
            case f:
            System.out.print("..-. ");
            break;
            case g:
            System.out.print("--. ");
            break;
            case h:
            System.out.print(".... ");
            break;
            case i:
            System.out.print(".. ");
            break;
            case j:
            System.out.print(".--- ");
            break;
            case k:
            System.out.print("-.- ");
            break;
            case l:
            System.out.print(".-.. ");
            break;
            case m:
            System.out.print("-- ");
            break;
            case n:
            System.out.print("-. ");
            break;
            case o:
            System.out.print("--- ");
            break;
            case p:
            System.out.print(".--. ");
            break;
            case q:
            System.out.print("--.- ");
            break;
            case r:
            System.out.print(".-. ");
            break;
            case s:
            System.out.print("... ");
            break;
            case t:
            System.out.print("- ");
            break;
            case u:
            System.out.print("..- ");
            break;
            case v:
            System.out.print("...- ");
            break;
            case w:
            System.out.print(".-- ");
            break;
            case x:
            System.out.print("-..- ");
            break;
            case y:
            System.out.print("-.-- ");
            break;
            case z:
            System.out.print("--.. ");
            break;
            case 0:
            System.out.print("----- ");
            break;
            case 1:
            System.out.print(".---- ");
            break;
            case 2:
            System.out.print("..--- ");
            break;
            case 3:
            System.out.print("...-- ");
            break;
            case 4:
            System.out.print("....- ");
            break;
            case 5:
            System.out.print("..... ");
            break;
            case 6:
            System.out.print("-.... ");
            break;
            case 7:
            System.out.print("--... ");
            break;
            case 8:
            System.out.print("---.. ");
            break;
            case 9:
            System.out.print("----. ");
            break;
            case .:
            System.out.print(".-.-.- ");
            break;
            case ,:
            System.out.print("--..-- ");
            break;
            case ?:
            System.out.print("..--.. ");
            break;
            default:
        }
    }
}
I have got different compiler complains when I checked my code with Bluej and Eclipse. I got a lot more complains from Eclipse. Why?
The first compiler error I got from Bluej is about this code
char c = input1.charAt(i);
the complain is: .'class' expected
Why I need a class here? I have declared the class at the very beginning.


Another complain which comes from Eclipse
String input = s.nextString(); 
The complain I got is: the method nextString() is undefined for the type Scanner
Why we can use nextInt() but not nextString()? besides int, is there any other type I can use with the Scanner type? and if I want to use the nextString() method here, what should I do?
Thank you very much!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 16 2008
Added on Mar 17 2008
33 comments
1,611 views