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?

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
sega_one
  • 63
  • 3
  • 9
  • Sounds like you want to check whether input `contains` "/done" rather than input `equals` "/done". – John3136 Apr 02 '13 at 00:47
  • whether the use types `/done` or is it part of a larger string – Arun P Johny Apr 02 '13 at 00:49
  • Do you actually enter "/done" on its own line? – PM 77-1 Apr 02 '13 at 00: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 – sega_one Apr 02 '13 at 00: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" Is that what you want? – Catchwa Apr 02 '13 at 01:36
  • I forgot to mention that part. If you press enter key after typing /done within the string, the program WILL terminate. If you press enter key without /done anywhere in the string/line, then it simply moves you to new line in eclipse console and continues user string input on a new line. I hope that clears it up a bit – sega_one Apr 02 '13 at 01:42

3 Answers3

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;
            }
        }
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Avik
  • 1,170
  • 10
  • 25
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..");
Harshit Gupta
  • 719
  • 1
  • 9
  • 26
0

You almost had it, instead of equals, your going to want to 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"));
Reilas
  • 3,297
  • 2
  • 4
  • 17