Program to convert text to morse code

AI Thread Summary
The discussion centers on a Java program intended to convert user-input text into Morse code but contains several coding errors. Key issues include incorrect use of the `indexOf` method, which requires a string argument instead of an integer, and improper string comparison using `==` instead of `.equals()`. Additionally, the program logic fails to iterate through each character of the input text for Morse code conversion. Suggestions include using a for loop to process each character and utilizing `String.charAt()` for accurate indexing. The overall consensus is that the code needs significant revisions to function correctly.
remaan
Messages
132
Reaction score
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
Maybe it's because you seplled exit wrong.
 
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:

Similar threads

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