how to use nested loops in a simple chatbot (Java)

Watch
Carrying a torch
Badges: 9
Rep:
?
#1
Report Thread starter 1 month ago
#1
What would be the simplest way to make use of nested loops in a simple very simple chat bot that just looks for trigger words? What could I use nested loops for in such context
0
reply
0le
Badges: 21
Rep:
?
#2
Report 1 month ago
#2
You asked this question somewhere else.

Use a while loop:
Code:
Scanner scanner = new Scanner(System.in);

while (true) {
    String inputWord = scanner.nextLine();

    if (inputWord.contains("ab")) {
        //DO SOMETHING
        continue;
    } 

    if (inputWord.contains("bc")) {
        //DO SOMETHING
        continue;
    } 

    break;
}
You can also do this with regular expressions.
Last edited by 0le; 1 month ago
0
reply
Carrying a torch
Badges: 9
Rep:
?
#3
Report Thread starter 1 month ago
#3
(Original post by 0le)
You asked this question somewhere else.

Use a while loop:
Code:
Scanner scanner = new Scanner(System.in);

while (true) {
    String inputWord = scanner.nextLine();

    if (inputWord.contains("ab")) {
        //DO SOMETHING
        continue;
    } 

    if (inputWord.contains("bc")) {
        //DO SOMETHING
        continue;
    } 

    break;
}
You can also do this with regular expressions.
This would be a single while loop, how might I go about having another loop inside it or have it being inside another loop?
0
reply
0le
Badges: 21
Rep:
?
#4
Report 1 month ago
#4
(Original post by Carrying a torch)
This would be a single while loop, how might I go about having another loop inside it or have it being inside another loop?
Put your second for loop inside the curly brackets of the original loop. There isn't anything mysterious about it.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you travelling in the Uni student travel window (3-9 Dec) to go home for Christmas?

Yes (124)
28.38%
No - I have already returned home (58)
13.27%
No - I plan on travelling outside these dates (86)
19.68%
No - I'm staying at my term time address over Christmas (40)
9.15%
No - I live at home during term anyway (129)
29.52%

Watched Threads

View All