Skip to content

Instantly share code, notes, and snippets.

@0x000000AC
Last active October 14, 2015 00:25
Show Gist options
  • Save 0x000000AC/d3d919b3258a9ed6dbe9 to your computer and use it in GitHub Desktop.
Save 0x000000AC/d3d919b3258a9ed6dbe9 to your computer and use it in GitHub Desktop.
/*
SentenceBuilder.java
Write a Java program called SentenceBuilder that takes words from the user
at the command line and constructs a sentence. Concatenate the words and
insert spaces to build a sentence.
Use a do-while loop and a sentinel to allow the user to tell the program
when they have finished entering words.
Output the sentence to the command line. Don’t include the sentinel in the sentence!
*/
import java.util.Scanner;
public class SentenceBuilder {
public static void main(String[] args)
{
Scanner stringIn = new Scanner(System.in);
Scanner sentinelIn = new Scanner(System.in);
String userInput = "";
String sentinelValue = "";
do
{
System.out.println("Enter a word or series of words. The words will be combined into a sentence: ");
userInput += " " + stringIn.nextLine();
System.out.println("Continue adding words? (-1 to quit, anything else to continue)");
sentinelValue = sentinelIn.nextLine();
} while (!sentinelValue.equals("-1"));
System.out.print(userInput + ".");
} // end main
} // end SentenceBuilder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment