Program to convert text to morse code

Click For Summary
SUMMARY

The discussion centers on a Java program designed to convert user-inputted text into Morse code. The primary issues identified include incorrect string comparisons using "==" instead of the .equals() method, and the misuse of the indexOf() method, which requires a string argument rather than an integer. Additionally, the suggestion to utilize a for loop with String.charAt() or String.substring() for character processing is emphasized as a necessary improvement for the program's functionality.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of string manipulation in Java
  • Knowledge of Morse code representation
  • Basic debugging techniques in Java
NEXT STEPS
  • Learn about Java String methods, particularly .equals() and .charAt()
  • Research how to implement loops in Java for character processing
  • Explore the concept of Morse code and its encoding
  • Study Java debugging techniques to identify and fix common coding errors
USEFUL FOR

Java developers, computer science students, and anyone interested in text encoding and string manipulation in programming.

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 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
6K