Help with my program

  • #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:
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 :)
 

Answers and Replies

  • #2
77
1
Is it because of this statement?

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

and your later statement

sentence = in.next();
 
  • #3
Is it because of this statement?

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

and your later statement

sentence = in.next();

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:
  • #4
77
1
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.
 
  • #5
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.

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
 
  • #6
77
1
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.
 
  • #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"
 
  • #8
77
1
....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.
 
  • #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
 
  • #10
77
1
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?
 

Related Threads on Help with my program

Replies
1
Views
19K
Replies
2
Views
265
Replies
2
Views
2K
  • Last Post
Replies
8
Views
5K
  • Last Post
Replies
13
Views
4K
Replies
4
Views
1K
Replies
7
Views
5K
  • Last Post
Replies
1
Views
1K
Top