Program to convert text to morse code

In summary, the conversation discussed a coding problem where the user inputs text to be converted into Morse code. The code provided by one party had some errors, such as using an int type instead of a String type for the indexOf method and checking an int against a String. It was also mentioned that the word "exit" may have been misspelled in the code. Suggestions were made to use a for loop and the charAt or substring methods to pick out individual letters for conversion.
  • #1
remaan
132
0

Homework Statement



Hi, this is an Hw need to be submitted in 2 hrs !
So, anyone helps I would really apppreaitiate it >>

in this problem I am asking the user to enter some text And I wil convert it into

Morse Method, I am perfectly fin with that, But the code DOES NOT TYPE out



Homework Equations





The Attempt at a Solution




import java.util.Scanner;
public class CodingTry {

public static void main (String args [])
{ System.out.println(" Do you want to encode in Morse, or SMS, or exist the program?");

Scanner scan = new Scanner (System.in);

String check = scan.next();
System.out.println(" plese type " + check + " text to be coded >");
String text = scan.next();

if (text == "Morse" )


{ String morsetext2 = scan.next();




// String MorseText = scan.next();


// String morsetext2 = scan.next();
while ( morsetext2 != "Done")
{ morsetext2 = scan.next();

int counter = 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 = "--.. ";
int morseletter = morsetext2.indexOf(counter);

if ( morseletter == 'a' || morseletter == 'A')
System.out.print ( a );

if ( morseletter == 'b' || morseletter == 'B' )
System.out.print ( b );

if ( morseletter == 'c' || morseletter =='C' )
System.out.print ( c );

if ( morseletter =='d' || morseletter =='D')
System.out.print ( d );

if ( morseletter == 'e' || morseletter == 'E' )
System.out.print ( e );

if ( morseletter == 'f' || morseletter == 'F' )
System.out.print ( f );

if ( morseletter == 'G' || morseletter == 'g' )
System.out.print ( g );

if ( morseletter == 'h' || morseletter == 'H' )
System.out.print ( h );

if ( morseletter == 'i' || morseletter == 'I' )
System.out.print ( i );

if ( morseletter == 'J' || morseletter == 'j' )
System.out.print ( j );

if ( morseletter =='k' || morseletter == 'K' )
System.out.print ( k );

if ( morseletter == 'l' || morseletter == 'L' )
System.out.print ( L );

if ( morseletter == 'M' || morseletter == 'm' )
System.out.print ( m );

if ( morseletter =='n'|| morseletter == 'N' )
System.out.print ( n );

if ( morseletter == 'o' || morseletter == 'O' )
System.out.print ( o );

if ( morseletter == 'p' || morseletter == 'P' )
System.out.print ( p );

if ( morseletter =='Q'|| morseletter == 'q' )
System.out.print ( q );

if ( morseletter == 'R' || morseletter == 'r' )
System.out.print ( r );

if ( morseletter == 'S' || morseletter == 's' )
System.out.print ( s );

if ( morseletter == 't' || morseletter == 'T' )
System.out.print ( t );

if ( morseletter == 'u' || morseletter == 'U' )
System.out.print ( u );

if ( morseletter == 'v' || morseletter =='V' )
System.out.print ( v );

if ( morseletter == 'w' || morseletter == 'W' )
System.out.print ( w );

if ( morseletter == 'x' || morseletter == 'X' )
System.out.print ( x );

if ( morseletter == 'y' || morseletter == 'y' )
System.out.print ( y );

if ( morseletter == 'z' || morseletter == 'Z' )
System.out.print ( z );
if (morseletter == ' ')
System.out.print( " ");

counter ++;
}

}


if ( check == "exist")
System.exit(0);



}

}
 
Physics news on Phys.org
  • #2
Maybe it's because you seplled exit wrong.
 
  • #3
So far all I've found

Code:
int morseletter = morsetext2.indexOf(counter);

-indexOf takes in a string type.
-counter, as far as I know, is int type.
-you put in int type where a string belongs.
-(btw indexOf gives the index in which counter first shows up in morsetext2 given proper conversions).

Code:
if ( morseletter == 'a' || morseletter == 'A')
System.out.print ( a );

if ( morseletter == 'b' || morseletter == 'B' )
System.out.print ( b );
//and so on...

-you are checking morseletter (an int type) against a String.

Code:
if ( check == "exist")

I'm guessing you want to type "exit"Your code will not work even if these bits are fixed. You will want to pick out letter by letter. I suggest a for loop and String.charAt or String.subString.
 
Last edited:

FAQ: Program to convert text to morse code

What is a program to convert text to morse code?

A program to convert text to morse code is a computer program that takes in a string of characters and translates it into a series of dots and dashes which represent letters, numbers, and special characters in the morse code alphabet.

Why would I need a program to convert text to morse code?

A program to convert text to morse code can be useful for communication in situations where verbal or written communication is not possible, such as in emergency situations or for people with disabilities.

How does a program to convert text to morse code work?

A program to convert text to morse code uses a code table to match each letter, number, or special character to its corresponding morse code representation. The program then converts the input text into a series of dots and dashes according to this code table.

Can I customize the output of a program to convert text to morse code?

It depends on the specific program you are using. Some programs may allow you to choose between different code tables or adjust the speed or tone of the morse code output. Others may have limited or no customization options.

Are there any limitations to a program to convert text to morse code?

One limitation of a program to convert text to morse code is that it can only translate text into morse code and not the other way around. Additionally, the output may be affected by the user's typing speed or the quality of the input text. Some programs may also have limitations on the length of the input text or the characters that can be translated.

Similar threads

Replies
7
Views
2K
Replies
10
Views
2K
Replies
2
Views
3K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
12
Views
2K
Replies
5
Views
8K
Replies
1
Views
2K
Back
Top