• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

My Program Does not Encode or Decode as its supported to

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I am a java beginner taking an intro to java course. The thing is that my teacher will not answer questions or respond to my emails and I learn just about nothing in the lectures and am left hanging when it gets to assignments. I've managed to figure things out so far in the course, but my deadlines are nearing. In this assignment, I am to input the user to either enter an English sentence and have the program convert this sentence into morse code or enter a sentence in morse code and have the program convert the morse to English. The program compiles, but the program doesn't do the encoding and/or decoding! It just ends after the user inputs the statement in morse or English.
 
L Coward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my code:
import project1.*;
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class Project1 {
public static void main (String [] args)
{
int choice = 1;
Scanner morseorenglish = new Scanner(System.in);
System.out.println ("Choose either 1 or 2: (Type '1' for choice 1)");
System.out.println ("(1) Translate your English sentence into morse code");
System.out.println ("(2) Translate your morse code into an English sentence");
choice = morseorenglish.nextInt();

if (choice==1)
{
Alphabet a1 = new Alphabet();
Scanner englishtomorse = new Scanner(System.in);
System.out.println ("Please enter an original sentence in English.");
englishtomorse.nextLine();
}
else
{
Alphabet a1 = new Alphabet();
Scanner morsetoenglish = new Scanner(System.in);
System.out.println ("Please enter an original sentence in morse code.");
morsetoenglish.nextLine();
}
}
public static String Englishtomorse (String toEncode , String englishtomorse)
{
englishtomorse = toEncode;

if(toEncode.equalsIgnoreCase("a"))
englishtomorse = ".-";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("b"))
englishtomorse = "-...";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("c"))
englishtomorse = "-.-.";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("d"))
englishtomorse = "-..";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("e"))
englishtomorse = ".";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("f"))
englishtomorse = "..-.";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("g"))
englishtomorse = "--.";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("h"))
englishtomorse = "....";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("i"))
englishtomorse = "..";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("j"))
englishtomorse = ".---";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("k"))
englishtomorse = "-.-";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("l"))
englishtomorse = ".-..";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("m"))
englishtomorse = "--";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("n"))
englishtomorse = "-.";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("o"))
englishtomorse = "---";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("p"))
englishtomorse = ".--.";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("q"))
englishtomorse = "--.-";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("r"))
englishtomorse = ".-.";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("s"))
englishtomorse = "...";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("t"))
englishtomorse = "-";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("u"))
englishtomorse = "..-";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("v"))
englishtomorse = "...-";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("w"))
englishtomorse = ".--";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("x"))
englishtomorse = "-..-";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("y"))
englishtomorse = "-.--";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("z"))
englishtomorse = "--..";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("0"))
englishtomorse = "-----";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("1"))
englishtomorse = ".----";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("2"))
englishtomorse = "..---";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("3"))
englishtomorse = "...--";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("4"))
englishtomorse = "....-";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("5"))
englishtomorse = ".....";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("6"))
englishtomorse = "-....";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("7"))
englishtomorse = "--...";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("8"))
englishtomorse = "---..";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase("9"))
englishtomorse = "----.";
System.out.print(englishtomorse);
if(toEncode.equalsIgnoreCase(" "))
englishtomorse = "|";
System.out.print(englishtomorse);

return englishtomorse;
}
public static String morsetoenglish (String toEncode , String morsetoenglish)
{
morsetoenglish = toEncode;

if (toEncode.equalsIgnoreCase (".-"))
morsetoenglish = "a";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-..."))
morsetoenglish = "b";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-.-."))
morsetoenglish = "c";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-.."))
morsetoenglish = "d";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("."))
morsetoenglish = "e";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("..-."))
morsetoenglish = "f";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("--."))
morsetoenglish = "g";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("...."))
morsetoenglish = "h";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase (".."))
morsetoenglish = "i";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase (".---"))
morsetoenglish = "j";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-.-"))
morsetoenglish = "k";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase (".-.."))
morsetoenglish = "l";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("--"))
morsetoenglish = "m";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-."))
morsetoenglish = "n";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("---"))
morsetoenglish = "o";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase (".--."))
morsetoenglish = "p";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("--.-"))
morsetoenglish = "q";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase (".-."))
morsetoenglish = "r";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("..."))
morsetoenglish = "s";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-"))
morsetoenglish = "t";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("..-"))
morsetoenglish = "u";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("...-"))
morsetoenglish = "v";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase (".--"))
morsetoenglish = "w";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-..-"))
morsetoenglish = "x";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-.--"))
morsetoenglish = "y";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("--.."))
morsetoenglish = "z";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase (".----"))
morsetoenglish = "1";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("..---"))
morsetoenglish = "2";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("...--"))
morsetoenglish = "3";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("....-"))
morsetoenglish = "4";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("....."))
morsetoenglish = "5";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-...."))
morsetoenglish = "6";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("--..."))
morsetoenglish = "7";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("---.."))
morsetoenglish = "8";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("----."))
morsetoenglish = "9";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("-----"))
morsetoenglish = "0";
System.out.print (morsetoenglish);
if (toEncode.equalsIgnoreCase ("|"))
morsetoenglish = " ";
System.out.print (morsetoenglish);

return morsetoenglish;

}
public static String stringToMorse (String englishtomorse)
{
String selectedChar;
String convertedChar = englishtomorse;
String encode = englishtomorse;

for (int i = 0; i < englishtomorse.length(); i++)
{
selectedChar = englishtomorse.charAt(i) + "";
convertedChar = encode;
System.out.println (convertedChar);
}

return convertedChar;
}
public static String stringToEnglish (String morsetoenglish)
{
String selectedChar;
String convertedChar = morsetoenglish;
String decode = morsetoenglish;

for (int i = 0; i < morsetoenglish.length(); i++)
{
selectedChar = morsetoenglish.charAt(i) + "";
convertedChar = decode;
}

return convertedChar;
}
}
 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags to post code, it renders the code easier to read
I have done this for you now
 
Ashish Dutt
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at your code now, it occurs to me that you are not calling your methods in the main function. Though you have defined them and the main function will accept an input but then the functions to encode or decode are not called, hence the program terminates without displaying the result. Hope this helps.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides, you do not do anything with the next lines from the Scanners, you throw away the result, you do not even store them, let alone apply some operation on them.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Coward wrote:Here's my code:


Right, well quite apart from the good advice you've been given:
1. Those enormous 'if' blocks are not very efficient because even after you find a letter, it will still check all the remaining ones redundantly. You will also find that it prints out the converted letter for every 'if'.
2. Having already printed out the converted letter, you then return it, but never use it. Do one or the other - I'd suggest the latter.
3. Your conversion methods are passed entire lines, but you've coded them as though you're only converting one letter.
4. Those big 'if' blocks are not scalable. What if you had to convert the Chinese alphabet instead? Would you have 5,000 if statements? Have a think how else you might do this (Hint: I presume you've been introduced to arrays).
5. While it's not incorrect, you don't need to use equalsIgnoreCase() for your comparisons. Just convert the entire line (if it's English) to the correct case before you attempt to convert it.

Winston
 
L Coward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the advice/help! I will take some time to review your suggestions thoroughly and apply them to my code.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Coward wrote:Thank you for the advice/help! I will take some time to review your suggestions thoroughly and apply them to my code.


Another thought occurred to me:
You include a "|" as a word break when creating your Morse code, but you don't appear to have a letter break. Since Morse Code is not a proper Huffman code, it means that a reader may not be able to work out what was transmitted, eg:
..-
could be "ea" or "u".

ACP-131, for example, uses spaces to break up letters, and "/" to split up words.

HIH

Winston

 
L Coward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been trying to fix my code and tighten it up, but I now have 2 error messages. Here's what I have now:

The two messages occur when I reference my methods........(1)"non-static method EnglishtoMorse(String) cannot be referenced from a static context" and (2)"non-static method MorsetoEnglish(String) cannot be referenced from a static context."
Am I on the right track? And how do I fix these errors so that my program compiles? Thanks for all of your feedback!
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Understanding Instance and Class Members
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Coward wrote:Am I on the right track?


Yes. Although you can make it even smaller by simply setting up your code array just like you did your alphabet one.

And how do I fix these errors so that my program compiles?


Well, for one thing, take all that logic out of main(). Have a look at the MainIsAPain page, because it shows you a very quick technique for doing just that. Basically, you want as little code in main() as you possibly can.

Also, what about a Translation class that contains a translation for each character? It might look something like this:and then you'd only need one array. viz:Yes, it's a bit more typing (but no more than you've already done ) and it's also quite visual because you can see every letter beside its Morse equivalent in your program. You can also use it for translating either way.

BTW, This is just ONE possibility; there are MANY ways to solve this problem. I just offer it to show you that classes are there to help you, and it's a really good idea to get into the habit of thinking about objects rather than just code.

HIH

Winston
 
L Coward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston for the suggestions! I am now working on developing a class file, but in the meantime, I have made a few updates to my code, and I still have the persistent problem that the program will only encode/decode if there is only one letter/morse code character in the scanner. If two, I just get a blank line where the conversions should take place.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic