これがばかげた質問のように思える場合は申し訳ありませんが、Java のユーザー入力の部分文字列を空白と比較する方法がわかりません。私のプログラムでは、私は使用します
Input.getString()
これは通常ではありませんが、Input.class はユーザーからの入力を受け取るために使用されます。これが私のプログラムです:
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" );
}
}
問題は、コードのこの部分がモールス信号を英語に翻訳する際に使用されないことです。
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 );
}
}
}
これにより、モールス符号の文字が分離されなくなります。たとえば、TextToTranslate の入力が
. .-. | .-. .
その場合、プログラムは ..-..- と考えるでしょう。言葉でした。これが、翻訳された結果が端末 (または Windows のコマンド プロンプト) に出力されない理由かどうかはわかりません。
また、以前にスペースを静的文字にしようとしましたが、if 構文で次のように使用して、最終的な文字としてスペースを使用しようとしましたが、同じ問題がありました。
TextToTranslate.charAt( SectionOfString ) == space
この問題が発生する理由がわかりません。同様の質問を見つけようとしましたが、見つけられなかったので、問題を解決する方法を誰かが知っていれば幸いです。
注: Java プログラミング フォーラム (ユーザー: Indybones33) で同じ質問をしました。