JAVA program keeps asking for input

  • Thread starter Darkstar3000
  • Start date
  • Tags
    Program
In summary: If it is, it is not working as of the moment. First, if you type in "apple stop", it will not print out "apple". Second, you have an infinite loop. The method reads a sentence, but it will never go past that because the variable i is always 0, and it will always be less than the length of the sentence.I think the reason why you don't see an infinite loop is because when you type in "apple stop", it will just take "apple" and forget all about "stop".In summary, the program is supposed to take in a sentence or word from the user and check if there is the word "stop" within the input. If so, it should stop the loop
  • #1
Darkstar3000
29
0
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 :)
 
Physics news on Phys.org
  • #2
Is it because of this statement?

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

and your later statement

sentence = in.next();
 
  • #3
sourlemon said:
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
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
sourlemon said:
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
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
...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 learned so far
 
  • #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 into 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?
 

What is the cause of a Java program continuously asking for input?

There could be a few reasons for this. It could be that the program is expecting more input than what is being provided, or that the input is not being properly processed. It could also be due to a loop or conditional statement that keeps prompting for input.

How can I fix a Java program that keeps asking for input?

The first step would be to check the code to see if there are any loops or conditional statements that could be causing the issue. If the problem persists, try resetting the program or restarting the computer. You could also consult with a more experienced Java programmer for assistance.

Why does my Java program only ask for input once?

This could be due to the program reaching the end of its execution before any further input is needed. Check the code to see if there are any exit conditions or if the program is only designed to ask for input once.

Is there a way to prevent a Java program from continuously asking for input?

Yes, you can use methods like "hasNextInt()" or "hasNextLine()" to check if there is any more input available before prompting for more. You can also use loops or conditional statements to control the flow of input and only ask for more when necessary.

Why is my Java program asking for input even when I have provided it?

This could be due to the input not being properly processed or the program expecting a different type of input than what was given. Check the code to see if there are any type mismatches or if the input is being properly stored and used in the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top