How can I keep my value from an if-statement?












2















I tried making a very basic calculator, but if I try to output the calculated result I get the error that my variable "result" has not been initialised, even though I did initialise it inside the if-statement at the bottom. When I put the "System.out" line in my if-statement it works, so I have been wondering how can initialise a variable in my statement and keep the value.



But all I found were threads that talked about using "return" to give out a value, but not a variable.



import javax.swing.JOptionPane;
class Calculator{
public static void main(String args)
{
double z1, z2, result;
String input, s;

input = JOptionPane.showInputDialog("Input a number:");

//check whether or not the input is a number
if(isNumber(input) == false)
{
JOptionPane.showMessageDialog(null, "Input a real Number", "Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}

z1 = Double.parseDouble(input);

input = JOptionPane.showInputDialog("Input a second number:");

//check whether or not the input is a number
if(isNumber(input) == false)
{
JOptionPane.showMessageDialog(null, "Input a real number", "Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
z2 = Double.parseDouble(input);

s = JOptionPane.showInputDialog("Input an operation(+,-,*,/):");

if(s.equals("+"))
{ result = z1 + z2; }
if(s.equals("-"))
{ result = z1 - z2; }
if(s.equals("*"))
{ result = z1 * z2; }
if(s.equals("/"))
{ result = z1 / z2; }

System.out.println(result);









share|improve this question

























  • s might not be +, -, * or /. If s isn't one of those, then result is still undefined.

    – Elliott Frisch
    Nov 22 '18 at 16:51













  • yeah, but when inputting I'm making sure to use a simple "+" or "-"so it shouldn't be a problem. I mean it works if I use system.out.println(result) inside my if-statement

    – Awais
    Nov 22 '18 at 16:54











  • Can you set a breakpoint to System.out.println(result) and check what the exact value of result is?

    – Lajos Arpad
    Nov 22 '18 at 16:55











  • What are your values and what is the output at the end?

    – Lajos Arpad
    Nov 22 '18 at 16:56











  • Sorry, but I'm not using a compiler. I'm just using a text editor and try to compile things through the windows console. I don't think i can set breakpoints there.

    – Awais
    Nov 22 '18 at 17:00
















2















I tried making a very basic calculator, but if I try to output the calculated result I get the error that my variable "result" has not been initialised, even though I did initialise it inside the if-statement at the bottom. When I put the "System.out" line in my if-statement it works, so I have been wondering how can initialise a variable in my statement and keep the value.



But all I found were threads that talked about using "return" to give out a value, but not a variable.



import javax.swing.JOptionPane;
class Calculator{
public static void main(String args)
{
double z1, z2, result;
String input, s;

input = JOptionPane.showInputDialog("Input a number:");

//check whether or not the input is a number
if(isNumber(input) == false)
{
JOptionPane.showMessageDialog(null, "Input a real Number", "Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}

z1 = Double.parseDouble(input);

input = JOptionPane.showInputDialog("Input a second number:");

//check whether or not the input is a number
if(isNumber(input) == false)
{
JOptionPane.showMessageDialog(null, "Input a real number", "Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
z2 = Double.parseDouble(input);

s = JOptionPane.showInputDialog("Input an operation(+,-,*,/):");

if(s.equals("+"))
{ result = z1 + z2; }
if(s.equals("-"))
{ result = z1 - z2; }
if(s.equals("*"))
{ result = z1 * z2; }
if(s.equals("/"))
{ result = z1 / z2; }

System.out.println(result);









share|improve this question

























  • s might not be +, -, * or /. If s isn't one of those, then result is still undefined.

    – Elliott Frisch
    Nov 22 '18 at 16:51













  • yeah, but when inputting I'm making sure to use a simple "+" or "-"so it shouldn't be a problem. I mean it works if I use system.out.println(result) inside my if-statement

    – Awais
    Nov 22 '18 at 16:54











  • Can you set a breakpoint to System.out.println(result) and check what the exact value of result is?

    – Lajos Arpad
    Nov 22 '18 at 16:55











  • What are your values and what is the output at the end?

    – Lajos Arpad
    Nov 22 '18 at 16:56











  • Sorry, but I'm not using a compiler. I'm just using a text editor and try to compile things through the windows console. I don't think i can set breakpoints there.

    – Awais
    Nov 22 '18 at 17:00














2












2








2








I tried making a very basic calculator, but if I try to output the calculated result I get the error that my variable "result" has not been initialised, even though I did initialise it inside the if-statement at the bottom. When I put the "System.out" line in my if-statement it works, so I have been wondering how can initialise a variable in my statement and keep the value.



But all I found were threads that talked about using "return" to give out a value, but not a variable.



import javax.swing.JOptionPane;
class Calculator{
public static void main(String args)
{
double z1, z2, result;
String input, s;

input = JOptionPane.showInputDialog("Input a number:");

//check whether or not the input is a number
if(isNumber(input) == false)
{
JOptionPane.showMessageDialog(null, "Input a real Number", "Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}

z1 = Double.parseDouble(input);

input = JOptionPane.showInputDialog("Input a second number:");

//check whether or not the input is a number
if(isNumber(input) == false)
{
JOptionPane.showMessageDialog(null, "Input a real number", "Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
z2 = Double.parseDouble(input);

s = JOptionPane.showInputDialog("Input an operation(+,-,*,/):");

if(s.equals("+"))
{ result = z1 + z2; }
if(s.equals("-"))
{ result = z1 - z2; }
if(s.equals("*"))
{ result = z1 * z2; }
if(s.equals("/"))
{ result = z1 / z2; }

System.out.println(result);









share|improve this question
















I tried making a very basic calculator, but if I try to output the calculated result I get the error that my variable "result" has not been initialised, even though I did initialise it inside the if-statement at the bottom. When I put the "System.out" line in my if-statement it works, so I have been wondering how can initialise a variable in my statement and keep the value.



But all I found were threads that talked about using "return" to give out a value, but not a variable.



import javax.swing.JOptionPane;
class Calculator{
public static void main(String args)
{
double z1, z2, result;
String input, s;

input = JOptionPane.showInputDialog("Input a number:");

//check whether or not the input is a number
if(isNumber(input) == false)
{
JOptionPane.showMessageDialog(null, "Input a real Number", "Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}

z1 = Double.parseDouble(input);

input = JOptionPane.showInputDialog("Input a second number:");

//check whether or not the input is a number
if(isNumber(input) == false)
{
JOptionPane.showMessageDialog(null, "Input a real number", "Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
z2 = Double.parseDouble(input);

s = JOptionPane.showInputDialog("Input an operation(+,-,*,/):");

if(s.equals("+"))
{ result = z1 + z2; }
if(s.equals("-"))
{ result = z1 - z2; }
if(s.equals("*"))
{ result = z1 * z2; }
if(s.equals("/"))
{ result = z1 / z2; }

System.out.println(result);






java if-statement double






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:21







Awais

















asked Nov 22 '18 at 16:49









AwaisAwais

195




195













  • s might not be +, -, * or /. If s isn't one of those, then result is still undefined.

    – Elliott Frisch
    Nov 22 '18 at 16:51













  • yeah, but when inputting I'm making sure to use a simple "+" or "-"so it shouldn't be a problem. I mean it works if I use system.out.println(result) inside my if-statement

    – Awais
    Nov 22 '18 at 16:54











  • Can you set a breakpoint to System.out.println(result) and check what the exact value of result is?

    – Lajos Arpad
    Nov 22 '18 at 16:55











  • What are your values and what is the output at the end?

    – Lajos Arpad
    Nov 22 '18 at 16:56











  • Sorry, but I'm not using a compiler. I'm just using a text editor and try to compile things through the windows console. I don't think i can set breakpoints there.

    – Awais
    Nov 22 '18 at 17:00



















  • s might not be +, -, * or /. If s isn't one of those, then result is still undefined.

    – Elliott Frisch
    Nov 22 '18 at 16:51













  • yeah, but when inputting I'm making sure to use a simple "+" or "-"so it shouldn't be a problem. I mean it works if I use system.out.println(result) inside my if-statement

    – Awais
    Nov 22 '18 at 16:54











  • Can you set a breakpoint to System.out.println(result) and check what the exact value of result is?

    – Lajos Arpad
    Nov 22 '18 at 16:55











  • What are your values and what is the output at the end?

    – Lajos Arpad
    Nov 22 '18 at 16:56











  • Sorry, but I'm not using a compiler. I'm just using a text editor and try to compile things through the windows console. I don't think i can set breakpoints there.

    – Awais
    Nov 22 '18 at 17:00

















s might not be +, -, * or /. If s isn't one of those, then result is still undefined.

– Elliott Frisch
Nov 22 '18 at 16:51







s might not be +, -, * or /. If s isn't one of those, then result is still undefined.

– Elliott Frisch
Nov 22 '18 at 16:51















yeah, but when inputting I'm making sure to use a simple "+" or "-"so it shouldn't be a problem. I mean it works if I use system.out.println(result) inside my if-statement

– Awais
Nov 22 '18 at 16:54





yeah, but when inputting I'm making sure to use a simple "+" or "-"so it shouldn't be a problem. I mean it works if I use system.out.println(result) inside my if-statement

– Awais
Nov 22 '18 at 16:54













Can you set a breakpoint to System.out.println(result) and check what the exact value of result is?

– Lajos Arpad
Nov 22 '18 at 16:55





Can you set a breakpoint to System.out.println(result) and check what the exact value of result is?

– Lajos Arpad
Nov 22 '18 at 16:55













What are your values and what is the output at the end?

– Lajos Arpad
Nov 22 '18 at 16:56





What are your values and what is the output at the end?

– Lajos Arpad
Nov 22 '18 at 16:56













Sorry, but I'm not using a compiler. I'm just using a text editor and try to compile things through the windows console. I don't think i can set breakpoints there.

– Awais
Nov 22 '18 at 17:00





Sorry, but I'm not using a compiler. I'm just using a text editor and try to compile things through the windows console. I don't think i can set breakpoints there.

– Awais
Nov 22 '18 at 17:00












2 Answers
2






active

oldest

votes


















3














Since this code:



if(s.equals("+"))
{ result = z1 + z2; }
if(s.equals("-"))
{ result = z1 - z2; }
if(s.equals("*"))
{ result = z1 * z2; }
if(s.equals("/"))
{ result = z1 / z2; }


does not have a final else statement it is not guaranteed that result will be initialized.

One way to overcome this problem:



    if (s.equals("+")) {
result = z1 + z2;
} else if (s.equals("-")) {
result = z1 - z2;
} else if (s.equals("*")) {
result = z1 * z2;
} else if (s.equals("/")) {
result = z1 / z2;
} else {
result = 0.0;
}


or in the definition:



double result = 0.0;





share|improve this answer

































    1














    The problem is that the environment checks whether everything is okay and sees that you have a few ifs where the variable might be initialized and finds that result is not guaranteed to be initialized. As a result you get this error. Try to initialize it at the start:



    double z1, z2, result = 0;





    share|improve this answer
























    • @Awais if this answer solved your problem, then you might consider accepting it as the correct answer. The other answer is correct as well.

      – Lajos Arpad
      Nov 22 '18 at 17:04











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


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435360%2fhow-can-i-keep-my-value-from-an-if-statement%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Since this code:



    if(s.equals("+"))
    { result = z1 + z2; }
    if(s.equals("-"))
    { result = z1 - z2; }
    if(s.equals("*"))
    { result = z1 * z2; }
    if(s.equals("/"))
    { result = z1 / z2; }


    does not have a final else statement it is not guaranteed that result will be initialized.

    One way to overcome this problem:



        if (s.equals("+")) {
    result = z1 + z2;
    } else if (s.equals("-")) {
    result = z1 - z2;
    } else if (s.equals("*")) {
    result = z1 * z2;
    } else if (s.equals("/")) {
    result = z1 / z2;
    } else {
    result = 0.0;
    }


    or in the definition:



    double result = 0.0;





    share|improve this answer






























      3














      Since this code:



      if(s.equals("+"))
      { result = z1 + z2; }
      if(s.equals("-"))
      { result = z1 - z2; }
      if(s.equals("*"))
      { result = z1 * z2; }
      if(s.equals("/"))
      { result = z1 / z2; }


      does not have a final else statement it is not guaranteed that result will be initialized.

      One way to overcome this problem:



          if (s.equals("+")) {
      result = z1 + z2;
      } else if (s.equals("-")) {
      result = z1 - z2;
      } else if (s.equals("*")) {
      result = z1 * z2;
      } else if (s.equals("/")) {
      result = z1 / z2;
      } else {
      result = 0.0;
      }


      or in the definition:



      double result = 0.0;





      share|improve this answer




























        3












        3








        3







        Since this code:



        if(s.equals("+"))
        { result = z1 + z2; }
        if(s.equals("-"))
        { result = z1 - z2; }
        if(s.equals("*"))
        { result = z1 * z2; }
        if(s.equals("/"))
        { result = z1 / z2; }


        does not have a final else statement it is not guaranteed that result will be initialized.

        One way to overcome this problem:



            if (s.equals("+")) {
        result = z1 + z2;
        } else if (s.equals("-")) {
        result = z1 - z2;
        } else if (s.equals("*")) {
        result = z1 * z2;
        } else if (s.equals("/")) {
        result = z1 / z2;
        } else {
        result = 0.0;
        }


        or in the definition:



        double result = 0.0;





        share|improve this answer















        Since this code:



        if(s.equals("+"))
        { result = z1 + z2; }
        if(s.equals("-"))
        { result = z1 - z2; }
        if(s.equals("*"))
        { result = z1 * z2; }
        if(s.equals("/"))
        { result = z1 / z2; }


        does not have a final else statement it is not guaranteed that result will be initialized.

        One way to overcome this problem:



            if (s.equals("+")) {
        result = z1 + z2;
        } else if (s.equals("-")) {
        result = z1 - z2;
        } else if (s.equals("*")) {
        result = z1 * z2;
        } else if (s.equals("/")) {
        result = z1 / z2;
        } else {
        result = 0.0;
        }


        or in the definition:



        double result = 0.0;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 17:03

























        answered Nov 22 '18 at 17:02









        forpasforpas

        10.8k2423




        10.8k2423

























            1














            The problem is that the environment checks whether everything is okay and sees that you have a few ifs where the variable might be initialized and finds that result is not guaranteed to be initialized. As a result you get this error. Try to initialize it at the start:



            double z1, z2, result = 0;





            share|improve this answer
























            • @Awais if this answer solved your problem, then you might consider accepting it as the correct answer. The other answer is correct as well.

              – Lajos Arpad
              Nov 22 '18 at 17:04
















            1














            The problem is that the environment checks whether everything is okay and sees that you have a few ifs where the variable might be initialized and finds that result is not guaranteed to be initialized. As a result you get this error. Try to initialize it at the start:



            double z1, z2, result = 0;





            share|improve this answer
























            • @Awais if this answer solved your problem, then you might consider accepting it as the correct answer. The other answer is correct as well.

              – Lajos Arpad
              Nov 22 '18 at 17:04














            1












            1








            1







            The problem is that the environment checks whether everything is okay and sees that you have a few ifs where the variable might be initialized and finds that result is not guaranteed to be initialized. As a result you get this error. Try to initialize it at the start:



            double z1, z2, result = 0;





            share|improve this answer













            The problem is that the environment checks whether everything is okay and sees that you have a few ifs where the variable might be initialized and finds that result is not guaranteed to be initialized. As a result you get this error. Try to initialize it at the start:



            double z1, z2, result = 0;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 16:59









            Lajos ArpadLajos Arpad

            27.6k1861116




            27.6k1861116













            • @Awais if this answer solved your problem, then you might consider accepting it as the correct answer. The other answer is correct as well.

              – Lajos Arpad
              Nov 22 '18 at 17:04



















            • @Awais if this answer solved your problem, then you might consider accepting it as the correct answer. The other answer is correct as well.

              – Lajos Arpad
              Nov 22 '18 at 17:04

















            @Awais if this answer solved your problem, then you might consider accepting it as the correct answer. The other answer is correct as well.

            – Lajos Arpad
            Nov 22 '18 at 17:04





            @Awais if this answer solved your problem, then you might consider accepting it as the correct answer. The other answer is correct as well.

            – Lajos Arpad
            Nov 22 '18 at 17:04


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435360%2fhow-can-i-keep-my-value-from-an-if-statement%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'