Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to Compare a Substring to a Normal White Space in Java

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How to Compare a Substring to a Normal White Space in Java

    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.toString().equals( M[i] ) )
                            {
                                mceStringBuffer.append( E[i] );
                            }
                        }
                    }
                    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 Stack Overflow (user1933229)
    http://stackoverflow.com/questions/1...-space-in-java
    Last edited by Norm; December 27th, 2012 at 07:15 PM. Reason: added URL


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,981
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: How to Compare a Substring to a Normal White Space in Java

    Can you copy and post the contents of the console window for when you execute the code that shows the problem?

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Compare a Substring to a Normal White Space in Java

    I fixed part of the program so now some of the translated results print to the terminal. Here is the terminal window (with incorrect types of translation as a demonstration):

    Error: "Garbage" is not a valid response.
    Error: "GARBagE" is not a valid response.
    Error: "English-MoorseCode" is not a valid response.
    Error: "MoorseCode-English" is not a valid response.
    MorseCode-English
    . .-. | .-. .
    EF F
    Done

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,981
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: How to Compare a Substring to a Normal White Space in Java

    You will need to do some debugging to find out what the code is doing with the data that is entered.
    I use println statements that print out the values of variables as they are assigned values and used to control the logic of the program. Something like this:
            System.out.println("TTT="+ TextToTranslate );
    Add printlns everywhere the code executes and print out the values of variables and what methods return so you can see what the computer sees when it executes the code.


    What is the | character for?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Compare a Substring to a Normal White Space in Java

    It represents a space in English

Similar Threads

  1. Java API to compare two files
    By Shakthi in forum Java Theory & Questions
    Replies: 16
    Last Post: August 22nd, 2012, 09:51 AM
  2. Formatting white space to 3 characters in parentheses
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2012, 02:00 AM
  3. java netbeans compare
    By senthil4984 in forum Java IDEs
    Replies: 2
    Last Post: November 6th, 2011, 04:39 AM
  4. Don't know java ...Need help compare, explain 2 different versiosn of code.
    By asian2003k in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2011, 12:41 PM
  5. AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    By nasi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2010, 10:37 PM

Tags for this Thread