# Morse Problem

• December 21st, 2013, 11:10 PM
llowe29
Morse Problem
Code java:

```/** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; int[] num = {0,1,2,3,4,5,6,7,8,9}; int i = 0; for(i = 0; i < alpha.length; i++) { if(phrase.charAt(i) == alpha[i]); }   } }```
My problem is the morse alphabet conversion like a =--. or something to that effect.
• December 22nd, 2013, 02:47 AM
GregBrannon
Re: Morse Problem
That's the assignment, but what questions do you have about the code you posted? What problems are you having writing the code to complete the assignment? If you're getting errors, post them. If the results from the code you posted are not correct, show the results, explain what should be different, and ask specific questions about how to fix the code to get the correct results.
• December 22nd, 2013, 09:16 AM
llowe29
Re: Morse Problem
(I understand the problem but dont see how.)(pseudocode!)See i need the if to be equivalent to asking if the phrase has a letter in the alphabet, return the index. Then look for the index in the norse code adding to new phrase.
• December 22nd, 2013, 09:26 AM
GregBrannon
Re: Morse Problem
So try coding that.
• December 22nd, 2013, 11:05 AM
llowe29
Re: Morse Problem
It wont work because everytime I would go to next letter in phrase it can only be compared to next letter in array.

--- Update ---

Here is what I have (tried...)
Code java:

```/** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; private static String Newphrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; Newphrase = ""; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; int[] num = {0,1,2,3,4,5,6,7,8,9}; int i = 0; String l = ""; for(i = 0; i < alpha.length; i++) { if(phrase.charAt(i) == alpha[i]); { l = morse[i]; Newphrase += l; } }   } public static String getNewPhrase() { return Newphrase; } }```
• December 22nd, 2013, 11:13 AM
GregBrannon
Re: Morse Problem
Wow, it's hard to get info from you, but we might finally be getting somewhere.

What I think you've said is that each time you advance to the next letter of the input, the array used for comparison needs to reset to the first element. That means that the same index, i, can't be used for the charAt() method and the alpha[] index. Do you see a way to use two variables? It may take 2 for loops, or it may take an independent index variable that can be reset to zero when needed.
• December 22nd, 2013, 11:23 AM
llowe29
Re: Morse Problem
is this what you mean
Code java:

```  /** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; private static String Newphrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; Newphrase = ""; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; int[] num = {0,1,2,3,4,5,6,7,8,9}; int i = 0; String l = ""; for(i = 0; i < phrase.length(); i++) { for(i = 0; i < alpha.length; i++) { if(phrase.charAt(i) == alpha[i]); { l = morse[i]; Newphrase += l; } }   } } public static String getNewPhrase() { return Newphrase; } }```
This all I could come up with.
• December 22nd, 2013, 11:33 AM
GregBrannon
Re: Morse Problem
not the same, two variables = DIFFERENT

Use a DIFFERENT index/loop control variable in each for loop.
• December 22nd, 2013, 11:37 AM
llowe29
Re: Morse Problem
Code java:

```/** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; private static String Newphrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; Newphrase = ""; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; int[] num = {0,1,2,3,4,5,6,7,8,9}; int i = 0; int a = 0; String l = ""; for(i = 0; i < phrase.length(); i++) { for(a = 0; a < alpha.length; a++) { if(phrase.charAt(i) == alpha[a]); { l = morse[a]; Newphrase += l; } }   } } public static String getNewPhrase() { return Newphrase; } }```
this
• December 22nd, 2013, 11:53 AM
GregBrannon
Re: Morse Problem
Think it through. In plain language, you might say what you're trying to do:

Get a value from the input
Compare the value to each element of the array alpha[] to find a match

So, program that:

Code java:

```// get a value from the input value = charAt( i );   // compare the value to each element of the array alpha[] to find a match for ( j = 0 ; j < alpha.length ; j++ ) { if ( value == alpha[j] ) { // then do something } }```
The for loop I wrote is the inner or nested loop. The outside loop that you'll write (you already have) takes each value of the input one at a time. Write the outer for loop and put it around the for loop I wrote.
• December 22nd, 2013, 02:03 PM
llowe29
Re: Morse Problem
After making suggested changes here is what I have.
Code java:

```/** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; private static String Newphrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; Newphrase = ""; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; int[] num = {0,1,2,3,4,5,6,7,8,9}; int i = 0; int a = 0; char l; for(i = 0; i < phrase.length(); i++) { l = phrase.charAt(i); for ( a = 0 ; a < alpha.length ; a++ ) { if ( l == alpha[a] ) { Newphrase+= morse[a]; } }   } } public static String getNewPhrase() { return Newphrase; } }```
• December 22nd, 2013, 03:08 PM
Norm
Re: Morse Problem
Does it execute and give you the desired results?
• December 22nd, 2013, 03:29 PM
llowe29
Re: Morse Problem
yes at least for letters I suppose Ill do the same thing for numbers

--- Update ---

Now the number part wont return anything
Code java:

```/** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; private static String Newphrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; Newphrase = ""; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; int[] num = {0,1,2,3,4,5,6,7,8,9}; int i = 0; int a = 0; char l; int b = 0; phrase.toLowerCase(); for(i = 0; i < phrase.length(); i++) { l = phrase.charAt(i); for ( a = 0 ; a < alpha.length ; a++ ) { if ( l == alpha[a] ) { Newphrase+= morse[a]; } }   } for(i = 0; i < phrase.length(); i++) { b = phrase.charAt(i); for ( a = 0 ; a < num.length ; a++ ) { if ( b == num[a] ) { Newphrase+= morse[a]; } }   } } public static String getNewPhrase() { return Newphrase; } }```
Here is tester.
Code java:

```/** * Write a description of class MorseCodeTester here. * * @author (your name) * @version (a version number or a date) */ import java.io.*; public class MorseCodeTester { public static void main(String [] args) throws IOException { MorseCode m = new MorseCode("Hohoho"); m.read(); m.Convert(); System.out.println(m.getNewPhrase()); } }```
• December 22nd, 2013, 03:32 PM
Norm
Re: Morse Problem
Please copy and post the console contents that shows what was input to the program and what it printed out.
• December 22nd, 2013, 03:34 PM
llowe29
Re: Morse Problem
Input was Hohoho122.
Output was ---....---....---
• December 22nd, 2013, 03:40 PM
Norm
Re: Morse Problem
Looks like the output is missing the leading h and the ending numbers.

Try debugging the code by adding some println statements that print out the values of variables as they are changed so you can see what the computer sees as the code executes. If you understand what the code is supposed to do, seeing what it is actually doing will help you fix the problem.
Be sure to add id Strings when printing so you know what was printed. For example when printing an array:
Code :

`System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));`
• December 22nd, 2013, 03:57 PM
llowe29
Re: Morse Problem
Code java:

```/** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; private static String Newphrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; Newphrase = ""; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; int[] num = {0,1,2,3,4,5,6,7,8,9}; int i = 0; int a = 0; char l; int b = 0; phrase = phrase.toLowerCase(); for(i = 0; i < phrase.length(); i++) { l = phrase.charAt(i); for ( a = 0 ; a < alpha.length ; a++ ) { if ( l == alpha[a] ) { Newphrase+= morse[a]; } }   } for(i = 0; i < phrase.length(); i++) { b = phrase.charAt(i); for ( a = 0 ; a < num.length ; a++ ) { if ( b == num[a] ) { Newphrase+= morse[a]; } }   } } public static String getNewPhrase() { return Newphrase; } }```
Now the H prints but still even with the Id statements, nothing wrong with array printed so thats not it.
• December 22nd, 2013, 04:06 PM
Norm
Re: Morse Problem
All the input are char values. None of the input is int. The char: '1' or the String: "1" does not have the same value as the int: 1.
To see the values of the characters, look at an ASCII character table.

The code should also print out an error message when it tries to convert a char and does NOT find a match for it. That will require using a boolean variable that can remember if a match was found. At the end of the search loop test the value of the boolean and print an error message saying the char was not found.
• December 22nd, 2013, 04:57 PM
llowe29
Re: Morse Problem
but the code has to convert ints to morse code also.
• December 22nd, 2013, 05:05 PM
Norm
Re: Morse Problem
'1' is a char and "1" is a String. Their values are not the same as the int: 1.

The charAt() method returns a char like '1', it does NOT return an int value like 1.

Change the code to work with char values, not int values.

Did you look at the ASCII character table values? For example, copy and execute this:
Code :

` System.out.println("char 1 value is="+(int)'1');`
• December 22nd, 2013, 05:53 PM
llowe29
Re: Morse Problem
Thanks finally fixed the problem or at least I think I did based on results
Code java:

```  /** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; private static String Newphrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; Newphrase = ""; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; char[] num = {'0','1','2','3','4','5','6','7','8','9'}; int i = 0; int a = 0; char l; int b = 0; phrase = phrase.toLowerCase(); for(i = 0; i < phrase.length(); i++) { l = phrase.charAt(i); for ( a = 0 ; a < alpha.length ; a++ ) { if ( l == alpha[a] ) { Newphrase+= morse[a]; } }   } for(i = 0; i < phrase.length(); i++) { b = phrase.charAt(i); for ( a = 0 ; a < num.length ; a++ ) { if ( b == num[a] ) { Newphrase+= " " + morse[a+25]; } }   } } public static String getNewPhrase() { return Newphrase; } }```
Space was removed but used to test results.

--- Update ---

Now my problem is spacing when a sentence is inserted
Code java:

```  /** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; private static String Newphrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; Newphrase = ""; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; char[] num = {'0','1','2','3','4','5','6','7','8','9'}; int i = 0; int a = 0; char l; int b = 0; phrase = phrase.toLowerCase(); for(i = 0; i < phrase.length(); i++) { l = phrase.charAt(i); if((int) 'l' == 32) Newphrase += " "; for ( a = 0 ; a < alpha.length ; a++ ) { if ( l == alpha[a] ) { Newphrase += morse[a]; } }   } for(i = 0; i < phrase.length(); i++) { b = phrase.charAt(i); for ( a = 0 ; a < num.length ; a++ ) { if ( b == num[a] ) { Newphrase+= morse[a+25]; } }   } } public static String getNewPhrase() { return Newphrase; } }```
for example inputting I love
would printout all morse code letters correctly in one jumble.

--- Update ---

So go ahead and scrap what I just said (Spacing still doesnt work also found out numbers between letters dont pint out until after letters are printing which makes sense because of my organization. Now with program reorganized they dont print at all.)
Code java:

```  /** * Write a description of class MorseCode here. * * @author (Landon L) * @version (a version number or a date) */ import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { private static String[] morse; private static String phrase; private static String Newphrase; /** * Constructor for objects of class MorseCode */ public MorseCode(String p) { morse = new String[36]; phrase = p; Newphrase = ""; }   public static String[] read() throws IOException { Scanner in = new Scanner(new File("morsecode.txt")); String a = phrase; int i = 0; while(in.hasNext()) { String b = in.next(); morse[i] = b; i++; } //for(i = 0; i < morse.length; i++) return morse; } public static void Convert() { char[] alpha = {'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'}; char[] num = {'0','1','2','3','4','5','6','7','8','9'}; int i = 0; int a = 0; char l; int b = 0; phrase = phrase.toLowerCase(); for(i = 0; i < phrase.length(); i++) { l = phrase.charAt(i); if((int) 'l' == 32) Newphrase += " "; else if(((int) 'l'>= 99)&& ((int) 'l'<= 122)) { for ( a = 0 ; a < alpha.length ; a++ ) { if ( l == alpha[a] ) { Newphrase += morse[a]; }   } } else if(((int) 'l'>= 48)&& ((int) 'l'<= 57)) { for ( a = 0 ; a < num.length ; a++ ) { if ( l == num[a] ) { Newphrase+= morse[a+25]; } } } } } public static String getNewPhrase() { return Newphrase; } }```
• December 22nd, 2013, 07:05 PM
Norm
Re: Morse Problem
The code doesn't appear to be testing the value of the char variable assigned the value returned by charAt().
BTW l is a very poor variable name. It looks too much like the digit 1. In general single letter variable names are ONLY used in for loops for the looping index.
• December 23rd, 2013, 10:23 AM
llowe29
Re: Morse Problem
I dont understand what is meant by your first sentence/how to fix it.
• December 23rd, 2013, 10:28 AM
Norm
Re: Morse Problem
Code :

` l = phrase.charAt(i);`
First you need to change the name of the variable that is assigned the value returned by the charAt() method.
l is not a good variable name. It is hard to talk about the variable l as it gets lost and confused too easily in a post and it is impossible to do a Find in the program for that variable name as that single letter occurs in too many places.

Change it to something like: nextChar