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;








0
















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")









share|improve this 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.
























    0
















    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")









    share|improve this 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.




















      0












      0








      0









      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")









      share|improve this question

















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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.
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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.






          share|improve this answer

























          • 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











          • 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



















          1














          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();







          share|improve this answer





























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            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.






            share|improve this answer

























            • 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











            • 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
















            0














            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.






            share|improve this answer

























            • 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











            • 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














            0












            0








            0







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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












            • 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


















            • 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











            • 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














            1














            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();







            share|improve this answer



























              1














              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();







              share|improve this answer

























                1












                1








                1







                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();







                share|improve this answer













                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();








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 24 at 14:10









                ShrirajShriraj

                313




                313













                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript