5

I want to make it so that the user inputs some string, and the program takes console input until user types "/done".. so here's how it would work:

  1. print to user: enter your string

  2. user enters: hello eclipse.

hi test blah blah

bla 456 testmore /done

As soon as user enters /done within any string of any size, the program breaks. The program would NOT end if you hit "enter" key. It would only end if you type in /done.. How I setup my program so far:

Scanner 123 = new Scanner(System.in);
string input = "";
System.out.println("Enter your string: ");

do {
    input = 123.nextLine();
    System.out.print("Rest of program here..");
}

while (!input.equals("/done"));

I tried putting under while loop there something like below but I don't think I am doing it right.

while (!input.equals("/done"));
    if input.equals("/done");
    break;
}

I understand that with a do-while loop, it continues as long as boolean in while is false. So for my program, program takes inputs until user types in /done so boolean is false until string /done in inputted. Then according to the logic above, the program breaks as soon as input equals "/done"

Any ideas on what I'm doing wrong?

6
  • Sounds like you want to check whether input contains "/done" rather than input equals "/done". Commented Apr 2, 2013 at 0:47
  • whether the use types /done or is it part of a larger string Commented Apr 2, 2013 at 0:49
  • Do you actually enter "/done" on its own line? Commented Apr 2, 2013 at 0:50
  • John - yes, you're right, contains would work rather than equals. Would you put that within the while of the do-while loop?? .. Arun - user would have to input /done all at once within the string input. Not on it's own line - it could be on it's own line but it is not necessary. Just has to be "/done" together Commented Apr 2, 2013 at 0:55
  • Even if you call contains it means your program won't break until the user hits the enter key. So I could type "blah blah /done blah blah<enter>" Is that what you want? Commented Apr 2, 2013 at 1:36

3 Answers 3

0
         Scanner s=new Scanner(System.in);
         String input = "";
         String[] parts;
         System.out.println("Enter your string: ");
         while (true){
             input=s.nextLine();
            if (input.contains("/done")){
            parts=input.split("/done");
            //Use parts[0] to do any processing you want with the part of the string entered before /done in that line
                break;
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

0

I think this will work fine

Scanner scanner = new Scanner(System.in);
    String input = "";
    System.out.println("Enter your string: ");
        do {
        input = scanner.nextLine();
    } while (!input.contains("/done"));
    System.out.print("Rest of program here..");

Comments

0

You almost had it, instead of equals, use contains.

Scanner scanner = new Scanner(System.in);
String input = "";
System.out.println("Enter your string: ");

do {
    input = scanner.nextLine();
    System.out.print("Rest of program here..");
}

while (!input.contains("/done"));

Comments

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.