Reputation: 51
Sorry if this seems like a silly question, but I cannot figure out how to compare a substring from user input in Java to a white space. In my program, I use
Input.getString()
which is not usual, but Input.class is used to receive input from a user. Here is my program:
public class Project1
{
static String E [] = { "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", "6", "7", "8", "9", "0", " " };
static String M [] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", "|" };
public String input[];
public String output[];
static String TranslationType;
static boolean Bool;
static String TextToTranslate;
static int i;
static int SectionOfString;
static StringBuffer emcStringBuffer = new StringBuffer();
static StringBuffer mceStringBuffer = new StringBuffer();
static String emc = "English-MorseCode";
static String mce = "MorseCode-English";
static StringBuffer MorseCodeLetter = new StringBuffer();
static String emcString;
static String mceString;
static String space = " ";
public static void main( String [] args )
{
if ( E.length != M.length )
{
System.out.println( "Error" );
}
TranslationType = Input.getString( "Please choose a method of translation (\"English-MorseCode\" or \"MorseCode-English\") " );
if ( TranslationType.equals( "English-MorseCode" ) )
{
Bool = true;
System.out.println( TranslationType );
}
else if( TranslationType.equals( "MorseCode-English" ) )
{
Bool = false;
System.out.println( TranslationType );
}
while ( !TranslationType.equals( emc ) && !TranslationType.equals( mce ) )
{
System.out.println( "Error: \"" + TranslationType + "\" is not a valid response.");
TranslationType = Input.getString( "Please choose a method of translation (\"English-MorseCode\" or \"MorseCode-English\") " );
if ( TranslationType.equals( "English-MorseCode" ) )
{
Bool = true;
System.out.println( TranslationType );
}
else if( TranslationType.equals( "MorseCode-English" ) )
{
Bool = false;
System.out.println( TranslationType );
}
}
TextToTranslate = Input.getString( "What would you like to translate?" );
TextToTranslate = TextToTranslate.toUpperCase();
System.out.println( TextToTranslate );
if ( Bool == true )
{
for ( SectionOfString = 0; SectionOfString < TextToTranslate.length(); SectionOfString++ )
{
for ( i = 0; i < E.length; i++ )
{
if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( E[i] ) )
{
emcStringBuffer.append( M[i] + " " );
}
}
}
emcString = emcStringBuffer.toString();
System.out.println( emcString );
}
else if ( Bool == false )
{
for ( SectionOfString = 0; SectionOfString < TextToTranslate.length(); SectionOfString++ )
{
if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( space ) )
{
for ( i = 0; i < M.length; i++ )
{
if ( MorseCodeLetter.equals( M[i] ) )
{
mceStringBuffer.append( E[i] );
System.out.println( MorseCodeLetter + ";" + mceStringBuffer );
}
}
}
else
{
if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( M[36] ) )
{
mceStringBuffer.append( E[36] );
}
else
{
MorseCodeLetter.append( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ) );
}
}
}
mceString = mceStringBuffer.toString();
System.out.println( mceString );
}
System.out.println( "Done" );
}
}
The problem seems to be that this part of the code is never used while translating Morse Code into English:
if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( space ) )
{
for ( i = 0; i < M.length; i++ )
{
if ( MorseCodeLetter.equals( M[i] ) )
{
mceStringBuffer.append( E[i] );
System.out.println( MorseCodeLetter + ";" + mceStringBuffer );
}
}
}
This causes the morse code letters not to be separated. For example, if the input for TextToTranslate was
. .-. | .-. .
then the program would think that ..-..-. was a word. I'm not sure if this is the reason that the translated result does not print to the terminal ( or command prompt on windows ).
Also, I tried to have space be a static char before, and I just tried having space as a final char, using the following in the if construct, and I had the same problem:
TextToTranslate.charAt( SectionOfString ) == space
I'm not sure why this problem happens, and I tried to find a similar question but was unable to, so if anyone knows how to fix the problem I would appreciate it.
Note: I asked the same question on Java Programming Forums (user: Indybones33)
Upvotes: 1
Views: 980
Reputation: 20442
This causes the morse code letters not to be separated. For example, if the input for TextToTranslate was
. .-. | .-. . then the program would think that ..-..-. was a word. I'm not sure if this is the reason that the translated result does not print to the terminal ( or command prompt on windows ).
Do you know this? Or are you assuming it? I think you are quite wrong.
I think the real problem is this:
if ( MorseCodeLetter.equals( M[i] )
This should always return false
because MorseCodeLetter
is of type StringBuffer
and M[i]
is of type String
.
You can fix this problem by simply doing this:
if ( MorseCodeLetter.toString().equals( M[i] ) )
As a side note, using a StringBuffer
is unnecessarily too complex. You could simply use String concatenation (String MorseCodeLetter = ""; MorseCodeLetter += letter;
). Also, in general StringBuffer
is intended for multi-threaded programs and StringBuilder
is preferred.
Side note 2: All of your notations and naming conventions are not standard Java convention. This means that it is difficult for other Java programmers to easily read your code. Only class
names should be UpperCamelCase. Variable names (like MorseCodeLetter
in your code) should be lowerCamelCase.
Upvotes: 2
Reputation: 9500
I didn't make it through the complexities of the code example, but here is a simple space comparison, just in case this is helpful.
String str = "x y";
String strSpace = " ";
if (strSpace.equals(str.substring(1,2))) {
System.out.println("Bingo");
}
Upvotes: 0