Creating scanner with multiple wordsCreate ArrayList from arrayHow do I create a Java string from the contents of a file?How can I create an executable JAR with dependencies using Maven?How do I create a file and write to it in Java?Create the perfect JPA entityCreating a memory leak with JavaJava - Scanner(System.in) and “Blocking a thread”Scanner is skipping nextLine() after using next() or nextFoo()?Keep getting this error message for my RockPaperScissor Program. How do I fix this error?
Of strange atmospheres - the survivable but unbreathable
How to keep consistency across the application architecture as a team grows?
Who knighted this Game of Thrones character?
Where is Jon going?
Why sampling a periodic signal doesn't yield a periodic discrete signal?
Count all vowels in string
First Program Tic-Tac-Toe
Cardio work for Muay Thai fighters
Navigating a quick return to previous employer
Did this character show any indication of wanting to rule before S8E6?
Does French have the English "short i" vowel?
Using too much dialogue?
Creating second map without labels using QGIS?
Why isn't Tyrion mentioned in the in-universe book "A Song of Ice and Fire"?
Sorting with IComparable design
Must a warlock replace spells with new spells of exactly their Pact Magic spell slot level?
Is there any chance a man can get the death penalty for causing a miscarriage?
Why is this integration method not valid?
Why does Bran want to find Drogon?
A burglar's sunglasses, a lady's odyssey
Is superuser the same as root?
How would a developer who mostly fixed bugs for years at a company call out their contributions in their CV?
Can you still travel to America on the ESTA waiver program if you have been to Iran in transit?
Can a UK national work as a paid shop assistant in the USA?
Creating scanner with multiple words
Create ArrayList from arrayHow do I create a Java string from the contents of a file?How can I create an executable JAR with dependencies using Maven?How do I create a file and write to it in Java?Create the perfect JPA entityCreating a memory leak with JavaJava - Scanner(System.in) and “Blocking a thread”Scanner is skipping nextLine() after using next() or nextFoo()?Keep getting this error message for my RockPaperScissor Program. How do I fix this error?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I need to include 10 variables but when I type in anything it comes up with only 1 of the variables I need to know how to make it so for example when "Gaming" is typed, the program comes back with "Do you like gaming?" right now it is at the point where anything I type it comes up with "Do you like uni?"
I have tried searching google for 3 hours and have done everything they have said and I can't find it. using .contains doesn't work for some reason.
Scanner chatterbot = new Scanner(System.in);
String uni = ("University");
String gaming = ("Gaming");
uni = chatterbot.nextLine();
if (uni.contains("University"))
System.out.println("Do you like uni?");
uni = chatterbot.nextLine();
if (uni.contains("Yes"))
System.out.println("Do you study one of SE CS or IT?");
else if (uni.contains("No"))
System.out.println("Do you study one of SE CS or IT?");
uni = chatterbot.nextLine();
if (uni.contains("Yes"))
System.out.println("That is really great!");
else if (uni.contains("No"))
System.out.println("That is not good!");
System.exit(0);
gaming = chatterbot.nextLine();
if (gaming.contains("Gaming"))
System.out.println("Do you like gaming?");
gaming = chatterbot.nextLine();
if (gaming.contains("Yes"))
System.out.println("What kind of games do you like to play?");
I expect the output to be if the user types Uni then it says "Do you like uni?" and when you type gaming it says "Do you like gaming?" The uni part works but when I type Gaming nothing appears.
java
add a comment |
I need to include 10 variables but when I type in anything it comes up with only 1 of the variables I need to know how to make it so for example when "Gaming" is typed, the program comes back with "Do you like gaming?" right now it is at the point where anything I type it comes up with "Do you like uni?"
I have tried searching google for 3 hours and have done everything they have said and I can't find it. using .contains doesn't work for some reason.
Scanner chatterbot = new Scanner(System.in);
String uni = ("University");
String gaming = ("Gaming");
uni = chatterbot.nextLine();
if (uni.contains("University"))
System.out.println("Do you like uni?");
uni = chatterbot.nextLine();
if (uni.contains("Yes"))
System.out.println("Do you study one of SE CS or IT?");
else if (uni.contains("No"))
System.out.println("Do you study one of SE CS or IT?");
uni = chatterbot.nextLine();
if (uni.contains("Yes"))
System.out.println("That is really great!");
else if (uni.contains("No"))
System.out.println("That is not good!");
System.exit(0);
gaming = chatterbot.nextLine();
if (gaming.contains("Gaming"))
System.out.println("Do you like gaming?");
gaming = chatterbot.nextLine();
if (gaming.contains("Yes"))
System.out.println("What kind of games do you like to play?");
I expect the output to be if the user types Uni then it says "Do you like uni?" and when you type gaming it says "Do you like gaming?" The uni part works but when I type Gaming nothing appears.
java
Don't usenew String("University")
, just doString uni = "University";
– GBlodgett
Mar 23 at 23:53
1
Also remove the semicolon after the lastif
– GBlodgett
Mar 24 at 0:00
No reason to use 2 different String fields for this. Just useString input = chatterbot.nextLine();
first time and useinput = chatterbot.nextLine();
second time. No need to instantiate strings with University and Gaming values either.
– oxyt
Mar 24 at 0:03
add a comment |
I need to include 10 variables but when I type in anything it comes up with only 1 of the variables I need to know how to make it so for example when "Gaming" is typed, the program comes back with "Do you like gaming?" right now it is at the point where anything I type it comes up with "Do you like uni?"
I have tried searching google for 3 hours and have done everything they have said and I can't find it. using .contains doesn't work for some reason.
Scanner chatterbot = new Scanner(System.in);
String uni = ("University");
String gaming = ("Gaming");
uni = chatterbot.nextLine();
if (uni.contains("University"))
System.out.println("Do you like uni?");
uni = chatterbot.nextLine();
if (uni.contains("Yes"))
System.out.println("Do you study one of SE CS or IT?");
else if (uni.contains("No"))
System.out.println("Do you study one of SE CS or IT?");
uni = chatterbot.nextLine();
if (uni.contains("Yes"))
System.out.println("That is really great!");
else if (uni.contains("No"))
System.out.println("That is not good!");
System.exit(0);
gaming = chatterbot.nextLine();
if (gaming.contains("Gaming"))
System.out.println("Do you like gaming?");
gaming = chatterbot.nextLine();
if (gaming.contains("Yes"))
System.out.println("What kind of games do you like to play?");
I expect the output to be if the user types Uni then it says "Do you like uni?" and when you type gaming it says "Do you like gaming?" The uni part works but when I type Gaming nothing appears.
java
I need to include 10 variables but when I type in anything it comes up with only 1 of the variables I need to know how to make it so for example when "Gaming" is typed, the program comes back with "Do you like gaming?" right now it is at the point where anything I type it comes up with "Do you like uni?"
I have tried searching google for 3 hours and have done everything they have said and I can't find it. using .contains doesn't work for some reason.
Scanner chatterbot = new Scanner(System.in);
String uni = ("University");
String gaming = ("Gaming");
uni = chatterbot.nextLine();
if (uni.contains("University"))
System.out.println("Do you like uni?");
uni = chatterbot.nextLine();
if (uni.contains("Yes"))
System.out.println("Do you study one of SE CS or IT?");
else if (uni.contains("No"))
System.out.println("Do you study one of SE CS or IT?");
uni = chatterbot.nextLine();
if (uni.contains("Yes"))
System.out.println("That is really great!");
else if (uni.contains("No"))
System.out.println("That is not good!");
System.exit(0);
gaming = chatterbot.nextLine();
if (gaming.contains("Gaming"))
System.out.println("Do you like gaming?");
gaming = chatterbot.nextLine();
if (gaming.contains("Yes"))
System.out.println("What kind of games do you like to play?");
I expect the output to be if the user types Uni then it says "Do you like uni?" and when you type gaming it says "Do you like gaming?" The uni part works but when I type Gaming nothing appears.
java
java
edited Mar 24 at 10:07
Fidelity
asked Mar 23 at 23:35
FidelityFidelity
62
62
Don't usenew String("University")
, just doString uni = "University";
– GBlodgett
Mar 23 at 23:53
1
Also remove the semicolon after the lastif
– GBlodgett
Mar 24 at 0:00
No reason to use 2 different String fields for this. Just useString input = chatterbot.nextLine();
first time and useinput = chatterbot.nextLine();
second time. No need to instantiate strings with University and Gaming values either.
– oxyt
Mar 24 at 0:03
add a comment |
Don't usenew String("University")
, just doString uni = "University";
– GBlodgett
Mar 23 at 23:53
1
Also remove the semicolon after the lastif
– GBlodgett
Mar 24 at 0:00
No reason to use 2 different String fields for this. Just useString input = chatterbot.nextLine();
first time and useinput = chatterbot.nextLine();
second time. No need to instantiate strings with University and Gaming values either.
– oxyt
Mar 24 at 0:03
Don't use
new String("University")
, just do String uni = "University";
– GBlodgett
Mar 23 at 23:53
Don't use
new String("University")
, just do String uni = "University";
– GBlodgett
Mar 23 at 23:53
1
1
Also remove the semicolon after the last
if
– GBlodgett
Mar 24 at 0:00
Also remove the semicolon after the last
if
– GBlodgett
Mar 24 at 0:00
No reason to use 2 different String fields for this. Just use
String input = chatterbot.nextLine();
first time and use input = chatterbot.nextLine();
second time. No need to instantiate strings with University and Gaming values either.– oxyt
Mar 24 at 0:03
No reason to use 2 different String fields for this. Just use
String input = chatterbot.nextLine();
first time and use input = chatterbot.nextLine();
second time. No need to instantiate strings with University and Gaming values either.– oxyt
Mar 24 at 0:03
add a comment |
1 Answer
1
active
oldest
votes
When you call chatterbot.nextLine() your input is consumed so if you call nextLine() again it waits for the next input.
But by saving the input in a variable you can compare it to more than one value.
This should work:
Scanner chatterbot = new Scanner(System.in);
String input = chatterbot.nextLine();
if (input.contains("University"))
System.out.println("Do you like uni?");
else if (input.contains("Gaming"))
System.out.println("Do you like gaming?");
I need to be able to add multiple answers for questions.
– Fidelity
Mar 24 at 1:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55319368%2fcreating-scanner-with-multiple-words%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you call chatterbot.nextLine() your input is consumed so if you call nextLine() again it waits for the next input.
But by saving the input in a variable you can compare it to more than one value.
This should work:
Scanner chatterbot = new Scanner(System.in);
String input = chatterbot.nextLine();
if (input.contains("University"))
System.out.println("Do you like uni?");
else if (input.contains("Gaming"))
System.out.println("Do you like gaming?");
I need to be able to add multiple answers for questions.
– Fidelity
Mar 24 at 1:10
add a comment |
When you call chatterbot.nextLine() your input is consumed so if you call nextLine() again it waits for the next input.
But by saving the input in a variable you can compare it to more than one value.
This should work:
Scanner chatterbot = new Scanner(System.in);
String input = chatterbot.nextLine();
if (input.contains("University"))
System.out.println("Do you like uni?");
else if (input.contains("Gaming"))
System.out.println("Do you like gaming?");
I need to be able to add multiple answers for questions.
– Fidelity
Mar 24 at 1:10
add a comment |
When you call chatterbot.nextLine() your input is consumed so if you call nextLine() again it waits for the next input.
But by saving the input in a variable you can compare it to more than one value.
This should work:
Scanner chatterbot = new Scanner(System.in);
String input = chatterbot.nextLine();
if (input.contains("University"))
System.out.println("Do you like uni?");
else if (input.contains("Gaming"))
System.out.println("Do you like gaming?");
When you call chatterbot.nextLine() your input is consumed so if you call nextLine() again it waits for the next input.
But by saving the input in a variable you can compare it to more than one value.
This should work:
Scanner chatterbot = new Scanner(System.in);
String input = chatterbot.nextLine();
if (input.contains("University"))
System.out.println("Do you like uni?");
else if (input.contains("Gaming"))
System.out.println("Do you like gaming?");
answered Mar 24 at 0:12
BahmutBahmut
111
111
I need to be able to add multiple answers for questions.
– Fidelity
Mar 24 at 1:10
add a comment |
I need to be able to add multiple answers for questions.
– Fidelity
Mar 24 at 1:10
I need to be able to add multiple answers for questions.
– Fidelity
Mar 24 at 1:10
I need to be able to add multiple answers for questions.
– Fidelity
Mar 24 at 1:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55319368%2fcreating-scanner-with-multiple-words%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Don't use
new String("University")
, just doString uni = "University";
– GBlodgett
Mar 23 at 23:53
1
Also remove the semicolon after the last
if
– GBlodgett
Mar 24 at 0:00
No reason to use 2 different String fields for this. Just use
String input = chatterbot.nextLine();
first time and useinput = chatterbot.nextLine();
second time. No need to instantiate strings with University and Gaming values either.– oxyt
Mar 24 at 0:03