1

I'm looking for a way to basically give back to the user some console output that looks identical to what they type and then prompt again for more input. The problem is I have a method that modifies each String discovered that does not consist of white space.

At the end of each sentence given by the user, I am trying to figure out a way to get a newline to occur and then for the prompt to output to the console again saying "Please type in a sentence: ". Here is what I have so far...

System.out.print("Please type in a sentence: ");

    while(in.hasNext()) {
        strInput = in.next();

        System.out.print(scramble(strInput) + " ");


        if(strInput.equals("q")) {
            System.out.println("Exiting program... ");
            break;
        }

    }

Here is what is displaying as console output:

Please type in a sentence: Hello this is a test
Hello tihs is a tset 

And the cursor stops on the same line as "tset" in the above example.

What I want to happen is:

Please type in a sentence: Hello this is a test
Hello tihs is a tset
Please type in a sentence: 

With the cursor appearing on the same line as and directly after "sentence:"

Hopefully that helps clear up my question.

4
  • can't you just move your please type output INSIDE your loop, then? Commented Oct 4, 2016 at 19:10
  • 2
    Be even more specific. Specify exactly what the user should input and what output the program should give in response. Commented Oct 4, 2016 at 19:11
  • @nhouser9 Tried to be more specific. I'm not sure how to format my console output in the question so its clearly identified. Commented Oct 4, 2016 at 19:15
  • 1
    @ClownInTheMoon thanks for the edit that makes it more clear. see my answer, and if it helps please upvote and accept =] Commented Oct 4, 2016 at 19:28

2 Answers 2

1

Try this, I didn't test it but it should do what you want. Comments in the code explain each line I added:

while (true) {
    System.out.print("Please type in a sentence: ");
    String input = in.nextLine(); //get the input

    if (input.equals("quit")) {
        System.out.println("Exiting program... ");
        break;
    }

    String[] inputLineWords = input.split(" "); //split the string into an array of words
    for (String word : inputLineWords) {        //for each word
        System.out.print(scramble(word) + " "); //print the scramble followed by a space
    }
    System.out.println(); //after printing the whole line, go to a new line
}
Sign up to request clarification or add additional context in comments.

3 Comments

If you need to scramble each word separately (instead of the sentence as a whole), then this is the way to go.
This worked well. Ended up using an ArrayList for a few different reasons but overall the program is working well now!
@ClownInTheMoon Happy to hear it =]
0

How about the following?

while (true) {
    System.out.print("Please type in a sentence: ");

    while (in.hasNext()) {
        strInput = in.next();

        if (strInput.equals("q")) {
            System.out.println("Exiting program... ");
            break;
        }
        System.out.println(scramble(strInput) + " ");
    }
    break;
}

The changes are:

  1. You should be printing "Please type in a sentence: " inside a loop to have it print again.
  2. I think you want to check if the strInput is "q" and exit BEFORE printing it, i.e. no need to print the "q", or is there?
  3. Use println to print the scrambled strInput so that the next "Please type in a sentence: " appears in the next line, and the cursor appears on the same line as " ... sentence: " because that is output by System.out.print (no ln)

1 Comment

This will print each word of the sentence on a new line. Not what OP wants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.