Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Morse Code Conversion Problems

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Morse Code Conversion Problems

    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 used to compile, but I tried to tighten things up (before I had huge if blocks) and now I have two error methods.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!
    import java.util.Scanner;
    import java.util.*;
    import java.io.*;
    public class Project1Array	{
    	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)
    		{
    			Scanner englishtomorse = new Scanner(System.in);
    				System.out.println ("Please enter an original sentence in English.");
    				String english = englishtomorse.nextLine();
    			System.out.println (EnglishtoMorse(english));
    		}
    		else
    		{
    			Scanner morsetoenglish = new Scanner(System.in);
    				System.out.println ("Please enter an original sentence in morse code.");
    				String morse = morsetoenglish.nextLine();
    			System.out.println (MorsetoEnglish(morse));
     
    		}
     
    	}
    	public String EnglishtoMorse(String english) 
    	{
    		String[] alphabet = {"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", " ", "1", "2",
    			"3", "4", "5", "7", "8", "9", "0"};
    		String a = ".- ";
    		String b = "-... ";
    		String c = "-.-. ";
    		String d = "-.. ";
    		String e = ". ";
    		String f = "..-. ";
    		String g = "--. ";
    		String h = ".... ";
    		String i = ".. ";
    		String j = ".--- ";
    		String k = "-.- ";
    		String l = ".-.. ";
    		String m = "-- ";
    		String n = "-. ";
    		String o = "--- ";
    		String p = ".--. ";
    		String q = "--.- ";
    		String r = ".-. ";
    		String s = "... ";
    		String t = "- ";
    		String u = "..- ";
    		String v = "...- ";
    		String w = ".-- ";
    		String x = "-..- ";
    		String y = "-.-- ";
    		String z = "--.. ";
    		String space = "| ";
    		String code[] = { 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 };
       		for(int variable = 0; variable < alphabet.length; ++variable) 
       		{
           		if(english.equals(alphabet[variable]))
              	return code[variable];
       		}
       		return " ";
    	}
    	public String MorsetoEnglish(String morse)
    	{
    		String[] alphabet = {"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", " ", "1", "2",
    			"3", "4", "5", "7", "8", "9", "0"};
    		String a = ".- ";
    		String b = "-... ";
    		String c = "-.-. ";
    		String d = "-.. ";
    		String e = ". ";
    		String f = "..-. ";
    		String g = "--. ";
    		String h = ".... ";
    		String i = ".. ";
    		String j = ".--- ";
    		String k = "-.- ";
    		String l = ".-.. ";
    		String m = "-- ";
    		String n = "-. ";
    		String o = "--- ";
    		String p = ".--. ";
    		String q = "--.- ";
    		String r = ".-. ";
    		String s = "... ";
    		String t = "- ";
    		String u = "..- ";
    		String v = "...- ";
    		String w = ".-- ";
    		String x = "-..- ";
    		String y = "-.-- ";
    		String z = "--.. ";
    		String space = "| ";
    		String code[] = { 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 };
       		for(int variable = 0; variable < code.length; ++variable) 
       		{
           		if(morse.equals(code[variable]))
              	return alphabet[variable];
       		}
       		return " "; 
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Morse Code Conversion Problems

    A couple words of advice: don't waste time complaining about your instructor. Plenty of people here taught themselves programming without the aid of a teacher, a classroom, fellow students, a tutoring department, or any of the other perks that come with being in school. Also, you should boil your problem down to an SSCCE- not your entire program, but enough to show us the problem that we can work with by copying and pasting it into our own editors. Also, you should use standard naming conventions- methods and variables start with lower-case letters.

    The main method is static. That means it can only access things outside of itself that are also static. Your other two methods are non-static, which means they can only be access with an instance of the Project1Array class. To fix your problem, you either need to create an instance of Project1Array or make the other methods static.

    This looks like a job for a Map, by the way.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    SillyLilly (August 27th, 2013)

  4. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code Conversion Problems

    Thanks for the advice! It now compiles, but when run, the encoding and decoding doesn't happen.

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Morse Code Conversion Problems

    Quote Originally Posted by SillyLilly View Post
    It now compiles, but when run, the encoding and decoding doesn't happen.
    Post the edited version of the code so we can see the changes

    --- Update ---

    Side notes:
    There is no reason to create a new Scanner after the user enters 1 or 2, just reuse the Scanner you already have.
    There is no reason to define the alphabet and morse code set twice, it should be defined once and accessible throughout the class.

  6. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code Conversion Problems

    I guess this time would be a time where I should post the whole document. I had to re-declare the arrays is because when I call morsetoEnglish() in the main, if I have the arrays as arguments in the method call, I am expected to call the arrays as well in the main.
    import java.util.Scanner;
    import java.util.*;
    import java.io.*;
    public class Project1Array	{
    	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)
    		{
    			Scanner englishtomorse = new Scanner(System.in);
    				System.out.println ("Please enter an original sentence in English.");
    				String english = englishtomorse.nextLine();
    			System.out.println (englishtoMorse(english));
    		}
    		else
    		{
    			Scanner morsetoenglish = new Scanner(System.in);
    				System.out.println ("Please enter an original sentence in morse code.");
    				String morse = morsetoenglish.nextLine();
    			System.out.println (morsetoEnglish(morse));
     
    		}
     
    	}
    	public static String englishtoMorse(String english) 
    	{
    		String[] alphabet = {"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", " ", "1", "2",
    			"3", "4", "5", "7", "8", "9", "0"};
    		String a = ".- ";
    		String b = "-... ";
    		String c = "-.-. ";
    		String d = "-.. ";
    		String e = ". ";
    		String f = "..-. ";
    		String g = "--. ";
    		String h = ".... ";
    		String i = ".. ";
    		String j = ".--- ";
    		String k = "-.- ";
    		String l = ".-.. ";
    		String m = "-- ";
    		String n = "-. ";
    		String o = "--- ";
    		String p = ".--. ";
    		String q = "--.- ";
    		String r = ".-. ";
    		String s = "... ";
    		String t = "- ";
    		String u = "..- ";
    		String v = "...- ";
    		String w = ".-- ";
    		String x = "-..- ";
    		String y = "-.-- ";
    		String z = "--.. ";
    		String space = "| ";
    		String code[] = { 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 };
       		for(int variable = 0; variable < alphabet.length; ++variable) 
       		{
           		if(english.equals(alphabet[variable]))
              	return code[variable];
       		}
       		return " ";
    	}
    	public static String morsetoEnglish(String morse)
    	{
    		String[] alphabet = {"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", " ", "1", "2",
    			"3", "4", "5", "7", "8", "9", "0"};
    		String a = ".- ";
    		String b = "-... ";
    		String c = "-.-. ";
    		String d = "-.. ";
    		String e = ". ";
    		String f = "..-. ";
    		String g = "--. ";
    		String h = ".... ";
    		String i = ".. ";
    		String j = ".--- ";
    		String k = "-.- ";
    		String l = ".-.. ";
    		String m = "-- ";
    		String n = "-. ";
    		String o = "--- ";
    		String p = ".--. ";
    		String q = "--.- ";
    		String r = ".-. ";
    		String s = "... ";
    		String t = "- ";
    		String u = "..- ";
    		String v = "...- ";
    		String w = ".-- ";
    		String x = "-..- ";
    		String y = "-.-- ";
    		String z = "--.. ";
    		String space = "| ";
    		String code[] = { 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 };
       		for(int variable = 0; variable < code.length; ++variable) 
       		{
           		if(morse.equals(code[variable]))
              	return alphabet[variable];
       		}
       		return " "; 
    	}
    }

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Morse Code Conversion Problems

    Pick one method to work with at a time. Say englishtoMorse for example (since it is easier to type the English version)
    Enter just one letter and check the output.
    Enter two letters and check the output.
    What happens?

  8. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code Conversion Problems

    Both methods work with only one character / code element at a time. When I have two English characters or morse letters, nothing displays.

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Morse Code Conversion Problems

    Yes, that is what it looks like.
    Again I say work on just one problem at a time. There is no need to complicate the process by involving more than one problem.
    Can you see why it works for just one character?
    Can you see why it does not work for multiple characters?

    What is the output when two (or more) characters are used for input?
    How does that happen?/where does that output come from?

  10. #9
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code Conversion Problems

    I'm pretty sure the problem occurs because I only go through the first letter of the input. I'm trying to loop through the input string when I do the conversion like so:
    for(int xyz = 0; xyz <= english.length; ++xyz)
       		{
       			for(int variable = 0; variable < alphabet.length; ++variable) 
       			{
           			if(english.equals(alphabet[variable]))
              		return code[variable];
       			}
       		}
    ......but I receive the error message error: cannot find symbol....variable length of String type english.
    Oh and the output when two or more characters are entered is that a blank line is printed.

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Morse Code Conversion Problems

    Focus on this line:

    if(english.equals(alphabet[variable]))

    If english is more than one character, will it ever be the same as a single element of alphabet[]? What should you be comparing instead?

  12. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Morse Code Conversion Problems

    Every time you make a change to the code, (especially if there is a new error), you will need to post the new version of the code on the forum where we can see it.
    Also when you need help with an error, copy-paste the full text of the error message with your code and question.

    I think if you do some research you will see that input of two or more characters does not result in a blank line, there is a character printed. Find out what character is there and how it got there.
    Looking at the snippet in post #9 it looks like you are already on top of that problem, but ran into another one.

  13. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Morse Code Conversion Problems


  14. #13
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Morse Code Conversion Problems

    I've really worked through this and tweaked things so that I'm working this from a very different angle. Now, when I run this, I receive the StringOutOfBoundsException error for the morse to english part. English to morse works just as it should! I am familiar with this, but do not know how to fix it.
    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)
    		{
    			Scanner englishtomorse = new Scanner(System.in);
    				System.out.println ("Please enter an original sentence in English.");
    				String english = englishtomorse.nextLine();
    			System.out.println (englishtoMorse(english));
    		}
    		else
    		{
    			Scanner morsetoenglish = new Scanner(System.in);
    				System.out.println ("Please enter an original sentence in morse code.");
    				String morse = morsetoenglish.nextLine();
    			System.out.println (morsetoEnglish(morse));
     
    		}
     
    	}
    	public static String englishtoMorse(String english) 
    	{
    		String alphabet = "ABC DEFGHIJKLMNOPQRSTUVWXYZ";
    		String code[] =  {".-", "-...", "-.-.", "|", "-..",".","..-.","--.", "....", 
    			"..", ".---", "-.-", ".-..", "--","- .", "---", ".--.", "--.-", ".-.", 
    			"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
    		english = english.toUpperCase();
    		for(int xyz = 0; xyz < english.length(); ++xyz)
    		{
    			char letter = english.charAt(xyz);
    			if (letter == ' ')
    			{
    				System.out.print ("|");
    				continue;
    			}
       			for(int variable = 0; variable < alphabet.length(); ++variable) 
       			{
           			if(alphabet.charAt(variable) == letter)
           			{
           				System.out.print(code[variable] + " ");
           				break;
           			}
       			}
       		}
       		return " ";
    	}
    	public static String morsetoEnglish(String morse)
    	{
    		String alphabet = "ABC DEFGHIJKLMNOPQRSTUVWXYZ";
    		String code[] =  {".-", "-...", "-.-.", "|", "-..",".","..-.","--.", "....", 
    			"..", ".---", "-.-", ".-..", "--","-.", "---", ".--.", "--.-", ".-.", 
    			"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
    		for(int xyz = 0; xyz < code.length; ++xyz)
    		{
    			char letter = morse.charAt(xyz);
    			if (letter == '|')
    			{
    				System.out.print (" ");
    				continue;
    			}
     
       			for(int variable = 0; variable < code.length; ++variable) 
       			{
           			if(code[variable].equals(letter))
           			{
           				System.out.print (alphabet.charAt(variable) + " ");
           				break;
           			}
       			}
       		}
       		return " "; 
    	}
    }

  15. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Morse Code Conversion Problems

    Answered in crosspost.

Similar Threads

  1. Morse Code
    By m49er704 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 24th, 2013, 06:48 AM
  2. Code conversion (Matlab to Java)
    By Nithyanathan Naiker in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 5th, 2013, 01:43 AM
  3. Simple Java Code Help--- temperature conversion using while loop
    By Idiot_willing_to_learn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 26th, 2013, 11:33 AM
  4. Help me with code conversion
    By davx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 04:29 PM
  5. morse code
    By daz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2012, 05:50 AM