How to fix variable might not have been initialized [duplicate]Variable might not have been initialized errorHow do I declare and initialize an array in Java?How to initialize HashSet values by construction?How do servlets work? Instantiation, sessions, shared variables and multithreadingHow to write a method to recognize words from following languageHow do I fix android.os.NetworkOnMainThreadException?How to directly initialize a HashMap (in a literal way)?How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor versionWhen use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?Why does my method say it must return a type string and gives me an error even though my method does return a type string
What are the words for people who cause trouble believing they know better?
correct term describing the action of sending a brand-new ship out into its first seafaring trip
Why is Colorado so different politically from nearby states?
Credit card offering 0.5 miles for every cent rounded up. Too good to be true?
Is it possible for people to live in the eye of a permanent hypercane?
How to decline physical affection from a child whose parents are pressuring them?
Applicants clearly not having the skills they advertise
Could the Missouri River be running while Lake Michigan was frozen several meters deep?
X-shaped crossword
Traffic law UK, pedestrians
Pronoun introduced before its antecedent
Why do guitarists wave their guitars?
What happened to all the nuclear material being smuggled after the fall of the USSR?
When writing an error prompt, should we end the sentence with a exclamation mark or a dot?
How could a possessed body begin to rot and decay while it is still alive?
Does any lore text explain why the planes of Acheron, Gehenna, and Carceri are the alignment they are?
What happens if you do emergency landing on a US base in middle of the ocean?
Is the decompression of compressed and encrypted data without decryption also theoretically impossible?
What flavor of zksnark in tezos
Chopin: marche funèbre bar 15 impossible place
How do you build a story from a world?
How to make thick Asian sauces?
Java 8: How to convert String to Map<String,List<String>>?
Who operates delivery flights for commercial airlines?
How to fix variable might not have been initialized [duplicate]
Variable might not have been initialized errorHow do I declare and initialize an array in Java?How to initialize HashSet values by construction?How do servlets work? Instantiation, sessions, shared variables and multithreadingHow to write a method to recognize words from following languageHow do I fix android.os.NetworkOnMainThreadException?How to directly initialize a HashMap (in a literal way)?How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor versionWhen use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?Why does my method say it must return a type string and gives me an error even though my method does return a type string
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Variable might not have been initialized error
11 answers
I am trying to create a method that spits out a boolean value if the user enter 1 word that is included in a list of 6. otherwise prompt the user until he enters 1 corrected one
I've tried using a while loop with switch but it doesnt seem to work
/**
* If the user says yes or y or true, return boolean value of true
* if the user says no or n or false, return boolean value of false
* Display a prompt if user fails to write any of 6 above words until
* the user does
*/
public static boolean promptForYesNo(Scanner in, String prompt)userAnswer.equals("no")
java
marked as duplicate by LppEdd, Nicholas K, Aniket Sahrawat, John Joe, Svetlin Zarev Mar 25 at 5:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Variable might not have been initialized error
11 answers
I am trying to create a method that spits out a boolean value if the user enter 1 word that is included in a list of 6. otherwise prompt the user until he enters 1 corrected one
I've tried using a while loop with switch but it doesnt seem to work
/**
* If the user says yes or y or true, return boolean value of true
* if the user says no or n or false, return boolean value of false
* Display a prompt if user fails to write any of 6 above words until
* the user does
*/
public static boolean promptForYesNo(Scanner in, String prompt)userAnswer.equals("no")
java
marked as duplicate by LppEdd, Nicholas K, Aniket Sahrawat, John Joe, Svetlin Zarev Mar 25 at 5:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Variable might not have been initialized error
11 answers
I am trying to create a method that spits out a boolean value if the user enter 1 word that is included in a list of 6. otherwise prompt the user until he enters 1 corrected one
I've tried using a while loop with switch but it doesnt seem to work
/**
* If the user says yes or y or true, return boolean value of true
* if the user says no or n or false, return boolean value of false
* Display a prompt if user fails to write any of 6 above words until
* the user does
*/
public static boolean promptForYesNo(Scanner in, String prompt)userAnswer.equals("no")
java
This question already has an answer here:
Variable might not have been initialized error
11 answers
I am trying to create a method that spits out a boolean value if the user enter 1 word that is included in a list of 6. otherwise prompt the user until he enters 1 corrected one
I've tried using a while loop with switch but it doesnt seem to work
/**
* If the user says yes or y or true, return boolean value of true
* if the user says no or n or false, return boolean value of false
* Display a prompt if user fails to write any of 6 above words until
* the user does
*/
public static boolean promptForYesNo(Scanner in, String prompt)userAnswer.equals("no")
This question already has an answer here:
Variable might not have been initialized error
11 answers
java
java
edited Mar 24 at 13:41
forpas
27.3k4830
27.3k4830
asked Mar 24 at 13:40
Đằng LongĐằng Long
32
32
marked as duplicate by LppEdd, Nicholas K, Aniket Sahrawat, John Joe, Svetlin Zarev Mar 25 at 5:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by LppEdd, Nicholas K, Aniket Sahrawat, John Joe, Svetlin Zarev Mar 25 at 5:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Even if you initialize the variable, it won't satisfy your intention. Instead, you should recursively call the method again with the arguments until you expect the user's intention, like so:
public static boolean promptForYesNo(Scanner in, String prompt) userAnswer.equals("n")
I have cleaned up your code a bit.
dude you're a genius. I just started Java so I'm pretty dumb. I wasnt able to grasp the key point inside of your code : return promptForYesNo(in, prompt) . I knew I had to return some kind of boolean value in order for the method to be usable , I kept thinking of having to tweak it in a way of somehow returning the toReturn variable I wrote inside of my own code. Could you please further elaborate the idea of how return promptForYesNo(in, prompt) was possible ? thank u so much dude
– Đằng Long
Mar 25 at 4:53
ifuserAnswer
doesn't meet theif
andelse-if
condition, it will return topromptForYesNo
function.
– John Joe
Mar 26 at 7:57
we are calling the function promptForYesNo until the user entering the expected value. Check this introcs.cs.princeton.edu/java/23recursion to learn more.
– Prashanth
Mar 27 at 6:13
add a comment |
The above code goes in continuous loop: use break when condition is satisfied
while (conditions)
if (conditions)
toReturn = true;
break;
else if (conditions)
toReturn = false;
break;
else
System.out.println(prompt + "Yes or No?");
userAnswer = in.next();
userAnswer = userAnswer.toLowerCase();
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Even if you initialize the variable, it won't satisfy your intention. Instead, you should recursively call the method again with the arguments until you expect the user's intention, like so:
public static boolean promptForYesNo(Scanner in, String prompt) userAnswer.equals("n")
I have cleaned up your code a bit.
dude you're a genius. I just started Java so I'm pretty dumb. I wasnt able to grasp the key point inside of your code : return promptForYesNo(in, prompt) . I knew I had to return some kind of boolean value in order for the method to be usable , I kept thinking of having to tweak it in a way of somehow returning the toReturn variable I wrote inside of my own code. Could you please further elaborate the idea of how return promptForYesNo(in, prompt) was possible ? thank u so much dude
– Đằng Long
Mar 25 at 4:53
ifuserAnswer
doesn't meet theif
andelse-if
condition, it will return topromptForYesNo
function.
– John Joe
Mar 26 at 7:57
we are calling the function promptForYesNo until the user entering the expected value. Check this introcs.cs.princeton.edu/java/23recursion to learn more.
– Prashanth
Mar 27 at 6:13
add a comment |
Even if you initialize the variable, it won't satisfy your intention. Instead, you should recursively call the method again with the arguments until you expect the user's intention, like so:
public static boolean promptForYesNo(Scanner in, String prompt) userAnswer.equals("n")
I have cleaned up your code a bit.
dude you're a genius. I just started Java so I'm pretty dumb. I wasnt able to grasp the key point inside of your code : return promptForYesNo(in, prompt) . I knew I had to return some kind of boolean value in order for the method to be usable , I kept thinking of having to tweak it in a way of somehow returning the toReturn variable I wrote inside of my own code. Could you please further elaborate the idea of how return promptForYesNo(in, prompt) was possible ? thank u so much dude
– Đằng Long
Mar 25 at 4:53
ifuserAnswer
doesn't meet theif
andelse-if
condition, it will return topromptForYesNo
function.
– John Joe
Mar 26 at 7:57
we are calling the function promptForYesNo until the user entering the expected value. Check this introcs.cs.princeton.edu/java/23recursion to learn more.
– Prashanth
Mar 27 at 6:13
add a comment |
Even if you initialize the variable, it won't satisfy your intention. Instead, you should recursively call the method again with the arguments until you expect the user's intention, like so:
public static boolean promptForYesNo(Scanner in, String prompt) userAnswer.equals("n")
I have cleaned up your code a bit.
Even if you initialize the variable, it won't satisfy your intention. Instead, you should recursively call the method again with the arguments until you expect the user's intention, like so:
public static boolean promptForYesNo(Scanner in, String prompt) userAnswer.equals("n")
I have cleaned up your code a bit.
edited Mar 25 at 5:06
Cody Gray♦
197k36390480
197k36390480
answered Mar 24 at 17:25
PrashanthPrashanth
1097
1097
dude you're a genius. I just started Java so I'm pretty dumb. I wasnt able to grasp the key point inside of your code : return promptForYesNo(in, prompt) . I knew I had to return some kind of boolean value in order for the method to be usable , I kept thinking of having to tweak it in a way of somehow returning the toReturn variable I wrote inside of my own code. Could you please further elaborate the idea of how return promptForYesNo(in, prompt) was possible ? thank u so much dude
– Đằng Long
Mar 25 at 4:53
ifuserAnswer
doesn't meet theif
andelse-if
condition, it will return topromptForYesNo
function.
– John Joe
Mar 26 at 7:57
we are calling the function promptForYesNo until the user entering the expected value. Check this introcs.cs.princeton.edu/java/23recursion to learn more.
– Prashanth
Mar 27 at 6:13
add a comment |
dude you're a genius. I just started Java so I'm pretty dumb. I wasnt able to grasp the key point inside of your code : return promptForYesNo(in, prompt) . I knew I had to return some kind of boolean value in order for the method to be usable , I kept thinking of having to tweak it in a way of somehow returning the toReturn variable I wrote inside of my own code. Could you please further elaborate the idea of how return promptForYesNo(in, prompt) was possible ? thank u so much dude
– Đằng Long
Mar 25 at 4:53
ifuserAnswer
doesn't meet theif
andelse-if
condition, it will return topromptForYesNo
function.
– John Joe
Mar 26 at 7:57
we are calling the function promptForYesNo until the user entering the expected value. Check this introcs.cs.princeton.edu/java/23recursion to learn more.
– Prashanth
Mar 27 at 6:13
dude you're a genius. I just started Java so I'm pretty dumb. I wasnt able to grasp the key point inside of your code : return promptForYesNo(in, prompt) . I knew I had to return some kind of boolean value in order for the method to be usable , I kept thinking of having to tweak it in a way of somehow returning the toReturn variable I wrote inside of my own code. Could you please further elaborate the idea of how return promptForYesNo(in, prompt) was possible ? thank u so much dude
– Đằng Long
Mar 25 at 4:53
dude you're a genius. I just started Java so I'm pretty dumb. I wasnt able to grasp the key point inside of your code : return promptForYesNo(in, prompt) . I knew I had to return some kind of boolean value in order for the method to be usable , I kept thinking of having to tweak it in a way of somehow returning the toReturn variable I wrote inside of my own code. Could you please further elaborate the idea of how return promptForYesNo(in, prompt) was possible ? thank u so much dude
– Đằng Long
Mar 25 at 4:53
if
userAnswer
doesn't meet the if
and else-if
condition, it will return to promptForYesNo
function.– John Joe
Mar 26 at 7:57
if
userAnswer
doesn't meet the if
and else-if
condition, it will return to promptForYesNo
function.– John Joe
Mar 26 at 7:57
we are calling the function promptForYesNo until the user entering the expected value. Check this introcs.cs.princeton.edu/java/23recursion to learn more.
– Prashanth
Mar 27 at 6:13
we are calling the function promptForYesNo until the user entering the expected value. Check this introcs.cs.princeton.edu/java/23recursion to learn more.
– Prashanth
Mar 27 at 6:13
add a comment |
The above code goes in continuous loop: use break when condition is satisfied
while (conditions)
if (conditions)
toReturn = true;
break;
else if (conditions)
toReturn = false;
break;
else
System.out.println(prompt + "Yes or No?");
userAnswer = in.next();
userAnswer = userAnswer.toLowerCase();
add a comment |
The above code goes in continuous loop: use break when condition is satisfied
while (conditions)
if (conditions)
toReturn = true;
break;
else if (conditions)
toReturn = false;
break;
else
System.out.println(prompt + "Yes or No?");
userAnswer = in.next();
userAnswer = userAnswer.toLowerCase();
add a comment |
The above code goes in continuous loop: use break when condition is satisfied
while (conditions)
if (conditions)
toReturn = true;
break;
else if (conditions)
toReturn = false;
break;
else
System.out.println(prompt + "Yes or No?");
userAnswer = in.next();
userAnswer = userAnswer.toLowerCase();
The above code goes in continuous loop: use break when condition is satisfied
while (conditions)
if (conditions)
toReturn = true;
break;
else if (conditions)
toReturn = false;
break;
else
System.out.println(prompt + "Yes or No?");
userAnswer = in.next();
userAnswer = userAnswer.toLowerCase();
answered Mar 24 at 14:10
ShrirajShriraj
313
313
add a comment |
add a comment |