How to combine words to make a sentence
Author |
Message |
venomm
|
Posted: Sun Mar 28, 2010 2:55 pm Post subject: How to combine words to make a sentence |
|
|
Hello I need to make a program that asks a user to enter any amount of words then combines words in to a sentence.
so far I have this:
Quote:
import java.io.*;
class sentence
{
public static void main (String args [])
throws java.io.IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String input = "";
String stop = "stop";
do
{
System.out.println ("Please enter a word:");
System.out.println ("Type stop to end");
input = br.readLine();
}
while (!input.equals (stop));
}
}
I would know how to combine the words into a sentence if the user was only allowed to enter a certian number of words
e.g.
System.out.println (word1 + word2)
but since I dont know how many words the user will enter I cant make the same number of variables any Ideas? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
venomm
|
Posted: Sun Mar 28, 2010 3:04 pm Post subject: Re: How to combine words to make a sentence |
|
|
venomm @ Sun Mar 28, 2010 2:55 pm wrote: Hello I need to make a program that asks a user to enter any amount of words then combines words in to a sentence.
so far I have this:
Quote:
import java.io.*;
class sentence
{
public static void main (String args [])
throws java.io.IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String input = "";
String stop = "stop";
do
{
System.out.println ("Please enter a word:");
System.out.println ("Type stop to end");
input = br.readLine();
}
while (!input.equals (stop));
}
}
I would know how to combine the words into a sentence if the user was only allowed to enter a certian number of words
e.g.
String word1 = "";
String word2 = "";
System.out.println (word1 + word2)
but since I dont know how many words the user will enter I cant make the same number of variables any Ideas? |
|
|
|
|
![](images/spacer.gif) |
venomm
|
Posted: Sun Mar 28, 2010 3:07 pm Post subject: RE:How to combine words to make a sentence |
|
|
Sorry for the double post this is my first time |
|
|
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Sun Mar 28, 2010 3:09 pm Post subject: RE:How to combine words to make a sentence |
|
|
An array of strings would work for the unknown amount of words the users enters.
For a sentence you should also think about what makes a sentence a subject and a predicate. While the subject can only be nouns, pronouns or Proper nouns. Assuming there are no modifiers.
Then the predicate which has to have a verb to make a sentence along with objects and such so to structure the sentence you need to think a lot more on how to put each word in each category and outputting a word.
Also you should switch to java.util.Scanner instead of BufferedReader |
|
|
|
|
![](images/spacer.gif) |
venomm
|
Posted: Mon Mar 29, 2010 6:09 pm Post subject: RE:How to combine words to make a sentence |
|
|
Hi syntax_error thanks for the reply but I cant use arrays I can use either loops or selections would you know how to do it with those? |
|
|
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Mon Mar 29, 2010 7:49 pm Post subject: RE:How to combine words to make a sentence |
|
|
@syntax_error: chances are this is just a string concatenation excercise, not an intelligent sentence generator.
In your loop, you set the value of input, then check if it is equal to "stop." If so, you exit, meaing you are done. Otherwise, back to the start of the loop, input still has the same value, which you know is not "stop." However, the user then inputs a new word, so you lose whatever value you had.
To fix this, create a new string variable and initialize it to "", and each time you go through the loop, add the value of input to that variable. The first time through, input will be "", which is the same as adding nothing. After that, input will be added up to but not including when input equals "stop." |
|
|
|
|
![](images/spacer.gif) |
USEC_OFFICER
![](http://compsci.ca/v3/uploads/user_avatars/16624966004bb548179e82e.png)
|
Posted: Mon Mar 29, 2010 7:56 pm Post subject: RE:How to combine words to make a sentence |
|
|
What about spaces? |
|
|
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Mon Mar 29, 2010 8:09 pm Post subject: RE:How to combine words to make a sentence |
|
|
Ah, good point.
if (curSentence.length() > 0) curSentence += " ";
curSentence += input;
To put a space after each word except the last. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
venomm
|
Posted: Mon Mar 29, 2010 10:05 pm Post subject: Re: RE:How to combine words to make a sentence |
|
|
methodoxx @ Mon Mar 29, 2010 7:49 pm wrote:
To fix this, create a new string variable and initialize it to "", and each time you go through the loop, add the value of input to that variable. The first time through, input will be "", which is the same as adding nothing. After that, input will be added up to but not including when input equals "stop."
Methodoxx thank you this helped but when I added your suggestions to my code and run it it still outputs stop at the end...
here is my new code I must have made an error some how.
Quote:
import java.io.*;
class sentence
{
public static void main (String args [])
throws java.io.IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String input = "";
String stop = "stop";
String sentence = "";
do
{
System.out.println ("Please enter a word:");
System.out.println ("Type stop to end");
input = br.readLine();
if (sentence.length() > 0) sentence += " ";
{
sentence = input + word;
}
}
while (!input.equals (stop));
System.out.println (word);
}
}
|
|
|
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Mon Mar 29, 2010 10:24 pm Post subject: RE:How to combine words to make a sentence |
|
|
You're very close. I'll give you a clue: since you set input to "", you don't need to read input before adding it to sentence. "" + "" is still "".
Also:
if (sentence.length() > 0) sentence += " ";
is the same as
if (sentence.length() > 0) {
sentence += " ";
}
You can omit the braces if there is only 1 expression; remove your extras. |
|
|
|
|
![](images/spacer.gif) |
btiffin
![](http://compsci.ca/v3/uploads/user_avatars/189169540547b535e50e4a7.jpg)
|
Posted: Tue Mar 30, 2010 10:32 pm Post subject: Re: RE:How to combine words to make a sentence |
|
|
methodoxx @ Mon Mar 29, 2010 8:09 pm wrote: Ah, good point.
if (curSentence.length() > 0) curSentence += " ";
curSentence += input;
To put a space after each word except the last.
Yeah, ... old guy interject.
Sorry to dilute a simple example, but if you care about performance (and to be honest, this is not really one of those times, once per word) you should try and code something like
code: |
curSentence += input; curSentence += " ";
|
inside any loops, and at or after exit but before you use your sentence
code: |
curSentence.substring(0, curSentence.length()-1);
|
or similar and save most of the potential computational work to outside the loop. Just whack off the extra space you know you put there.
As many a good idiom goes though, this trick won't always work, streaming for instance.
Excuse the interruption.
Cheers |
|
|
|
|
![](images/spacer.gif) |
|
|