1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with my program

  1. Apr 13, 2012 #1
    I wrote this program and it keeps requesting input from me, could anyone take a look at it and tell me what's wrong ?

    Code (Text):

    import java.io.IOException;
    import java.util.Scanner;
    import java.lang.String;

    public class StringCW
    {
        private Scanner in = new Scanner( System.in );

        String sentence, s, collect;
        String lowercase = "";
        char sub;
        int i = 0;

        public String readWords()
        {
            System.out.println( "Enter your sentence/word : " );
            sentence = in.next();
            for (; ; )
            {
                if(i < sentence.length())
                {
                    lowercase = sentence.toLowerCase();

                    sub = lowercase.charAt( i );
                    if (sub == 's')
                    {
                        s = sentence.substring( i, (i + 4) ).toLowerCase();
                        if (s == "stop")
                        {
                            return ("\"" + lowercase + "\" ");
                        }
                        else
                        {
                            i++;
                            continue;
                        }
                    }

                }

            }
        }

        String splitString()
        {
            String a[] = new String[100];
            System.out.println( "Enter your sentence : " );
            sentence = in.next();

            for (i = 0; i < sentence.length(); i++)
            {

                sub = sentence.charAt( i );
                collect += sub;
                collect.toLowerCase();

                if (sub == 's')
                {
                    s = sentence.substring( i, (i + 4) ).toLowerCase();
                    if (s == "stop")
                    {
                        return ("\"" + lowercase + "\" ");
                    }
                    else
                        continue;

                }

                if (sub == ' ')
                {
                    a[i] = collect;
                    sub = ' ';
                    collect = "";
                }
            }

            for (i = 0; i < sentence.length(); i++)
            {
                System.out.println( "\"" + a[i] + "\"" );
            }

            return ("Finished");
        }

        public static void main( String[] args ) throws IOException
        {
            StringCW obj = new StringCW();

            obj.readWords();
            obj.splitString();
        }
        /////////////////////////////////////////////////////////////////////////
    } // End of class

     
    Thanks in advance :)
     
  2. jcsd
  3. Apr 13, 2012 #2
    Is it because of this statement?

    private Scanner in = new Scanner( System.in );

    and your later statement

    sentence = in.next();
     
  4. Apr 14, 2012 #3
    But the program keeps asking for input and does nothing, no matter how long the word is. It just keeps requesting input

    ----------------------------------------------------------
    EDIT :

    I noticed that I used == to compare strings so I changed all the string comparisons to .equals but it still continuously requests input
     
    Last edited: Apr 14, 2012
  5. Apr 14, 2012 #4
    I tried to run this on the online compiler, but it didn't work, so I'll make a guess.

    In your for loop in readWords(), it doesn't look like you have an....end statement. What I mean by that is, when does the for loop know when to stop the loop?

    From the look of it, if you type in stop, it would work.
     
  6. Apr 14, 2012 #5
    You can download bluej or eclipse then run it, I've tried all that I can but there doesn't seem to be any progress
     
  7. Apr 14, 2012 #6
    I tried the program with eclipse, but I'm seeing a couple of problem. Maybe it's better if you explain to me what your program is trying to do.

    When I enter apple stop (assuming you want the sentence to end with the word stop), it will take me in an infinite loop because if sub is not 's', then i doesn't increment, which makes the program runs in an infinite loop because i will always remain 0 which will always be less than length.

    Second, if I just enter "stop" alone, it will print out null.

    I also don't know why you ask for the user input again in splitString. You should also note that you never initialized collect so when you do

    collect += sub

    you would get "null" + sub.
     
  8. Apr 15, 2012 #7
    Well the first part of the programs is supposed to keep getting the words that I input over and over again until it encounters the word "stop" and the second bit it has to split the sentence into it's words until it encounters "stop"
     
  9. Apr 15, 2012 #8
    ....I'm still confused. So is the first part suppose to read word or sentence? Can you give me an example of the input?

    From your code, it looks like readWords only read one word (sentence = in.next()), takes out the word "stop" from the word, then the return string (i.e. lowercase), which doesn't seem to be doing anything.

    In the second method, it looks like you're reading a word ( sentence = in.next();), then put it in the array a to be printed out later. But it doesn't look like this will work since you're reading in.next() which will only read a word, not a line/sentence.
     
  10. Apr 15, 2012 #9
    A sentence has multiple words, right ? When it encounters a space it breaks and prints that word in a line then goes back and prints the other word and so on.

    When it encounters stop it will forget all the other words.

    ----------------------------------------------------------------

    Nevermind if this doesn't make sense, I was just messing around with some of the I learnt so far
     
  11. Apr 15, 2012 #10
    I think it's great that you're trying to use what you learn. I don't mind helping you achieve your goals if you don't mind my simple mind. But if I still don't understand what you want to do, then I think I'll just let the others step in to help.

    So let's look at your splitSpring() method. From what you posted, you wanted it to..

    1. Read a sentence(/line)
    2. Read each character in the sentence
    a. If you encounter the letter 's', then check to see if it's the word stop, if yes, break out of the loop
    b. If you encounter a space, collect that "word" by adding it to array a
    3. Print out all the words in the array

    Does that sound like what you want to do?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook