Win a copy of Rust Web Development this week in the Other Languages forum!

L Coward

Greenhorn
+ Follow
since Aug 24, 2013
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by L Coward

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.
8 years ago
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!
8 years ago
Thank you for the advice/help! I will take some time to review your suggestions thoroughly and apply them to my code.
8 years ago
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;
}
}
8 years ago
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.
8 years ago