Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 3 123 LastLast
Results 1 to 25 of 64

Thread: Help with calculator

  1. #1
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with calculator

    I'm creating a calculator but I need help. The user is supposed to write for example 2+2 and get the answer 4 in a dialogue window but I'm stuck on how to make it work.

    here is my code
    String s = JOptionPane.showInputDialog(null, "Type in number");
            int num = Integer.parseInt(s);
     
            ArrayList<String> matte = new ArrayList<String>();
            matte.add("+");
            matte.add("-");
            matte.add("/");
            matte.add("*");
            matte.get(0);
            System.out.println(matte);
    Last edited by Norm; November 1st, 2019 at 07:44 AM. Reason: Added / to end code tag

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    Is that all your code? It needs a lot more to be able to compile and execute - import statements, class definition, main method, etc

    Before writing any more code you need to do some design work laying out the steps the program needs to take to solve the problem.
    Once you have the steps, then try writing the code to implement the steps.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    Is that all your code? It needs a lot more to be able to compile and execute - import statements, class definition, main method, etc

    Before writing any more code you need to do some design work laying out the steps the program needs to take to solve the problem.
    Once you have the steps, then try writing the code to implement the steps.
    Okay, we don't have to design it. It only has to show in one message dialogue.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    It only has to show in one message dialogue.
    If that is all the code is supposed to do, then you have it done since it already uses a JOptionPane class method to show a message in a dialog.

    we don't have to design it
    By design I meant a list of the steps the program must take to solve the problem. All programs need that.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    If that is all the code is supposed to do, then you have it done since it already uses a JOptionPane class method to show a message in a dialog.


    By design I meant a list of the steps the program must take to solve the problem. All programs need that.
    Alright! I only know the codes we need are arrays, selections and iterations. I'm still not sure exacly on how to use them in this...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    What steps must the program take to solve the problem? Can you make a list of the steps?
    Once the steps are known, then the arrays, selections and iterations, etc can be used to do them.

    For example, a list of steps at a very high level:
    ask user for input
    read user's input
    process user's input >>>>>>> this needs to have lots more detailed steps
    display results
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    What steps must the program take to solve the problem? Can you make a list of the steps?
    Once the steps are known, then the arrays, selections and iterations, etc can be used to do them.

    For example, a list of steps at a very high level:
    ask user for input
    read user's input
    process user's input >>>>>>> this needs to have lots more detailed steps
    display results
    My program has to be able to read mathimatical expression from one single dialogue input. For example 2+4, 6*2, 100-50, 6/3.
    Show error message if something is missing, or things that are not allowed for example: 10%12, 176. It's also not allowed to input such as 1+5*2, only two is allowed like 1+5.
    Show error message if you try to divide with 0.
    In order to solve this we have to use arrays, selections, iterations, string, double and character.

    I've only managed to create this now
    String s = JOptionPane.showInputDialog("number");
            int n = Integer.parseInt(s);
            int sum = 0;
            int i=1;
            while (i <= n) {
                sum = n+n;
                sum = n-n;
                sum = n*n;
                i= i + 1;
            }
            JOptionPane.showMessageDialog(null, "Summan = " + sum);

    Though you can only input one number and it will be read as n*n
    Last edited by Dragonfly11; November 3rd, 2019 at 08:03 AM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    read mathimatical expression from one single dialogue input. For example 2+4
    Ok, what steps does the code need to take to break that expression up into its 3 parts?

    The posted code looks like it will only read and process a single number, like 2 vs an expression like 2+4.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    Ok, what steps does the code need to take to break that expression up into its 3 parts?

    The posted code looks like it will only read and process a single number, like 2 vs an expression like 2+4.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Okay it's edited thank you! Yes I noticed it too I'm trying to make it so that it can process two and whatever count type the user wants input like + for example

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    make it so that it can process two and whatever count type the user wants input like +
    Ok, what does the program need to do to get the two operands and the operator from the expression the user entered?

    It could be done with Regular Expressions, but I suspect that is not how the instructor wants it done.

    Another way is to use String class methods like indexOf and substring.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    Ok, what does the program need to do to get the two operands and the operator from the expression the user entered?

    It could be done with Regular Expressions, but I suspect that is not how the instructor wants it done.

    Another way is to use String class methods like indexOf and substring.

    I'm working with substring right now and try to get tal1 but it won't work...

    String s = JOptionPane.showInputDialog("tal1 och tal2");
            //int n = Integer.parseInt(s);
            int tal = s.indexOf(' ');
            int num = s.lastIndexOf(' ');
            String v = s.substring(tal-3, tal);
     
            System.out.println(v);

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    try to get tal1 but it won't work.
    Please explain in English how you are trying to get the 3 parts of the expression.

    What if there are no spaces in the expression? For example: "3+4"
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    Please explain in English how you are trying to get the 3 parts of the expression.

    What if there are no spaces in the expression? For example: "3+4"
    I want to capture tal1 in the substring where the user type a number in the message dialogue.
    You mean 3+4 in the input?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    want to capture tal1 in the substring
    What is tal1?

    Given the String: "3+4" what steps can the code take to separate it into the three Strings: "3", "+" and "4"?
    Can you make a list of the steps in English the code needs to take to do that?

    I am assuming that "3+4" is the type of String the user will enter. If it is not that way, please post some examples of user input.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    What is tal1?

    Given the String: "3+4" what steps can the code take to separate it into the three Strings: "3", "+" and "4"?
    Can you make a list of the steps in English the code needs to take to do that?

    I am assuming that "3+4" is the type of String the user will enter. If it is not that way, please post some examples of user input.
    My code is supposed to accept two numbers and able to count in +, -, * and / (any number(any of these: +, *, -, /) any number)

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    Ok, how is the code going to separate the String: "3+4" into the three Strings: "3", "+" and "4"?

    For now assume that "+" is the only operator to worry about. Later work on getting the others.
    First the code must find the location of the "+"
    Then what?

    Some info on using the substring method:
    substring(0, index) returns String from start of String
    substring(index) returns String through the end of the String
    Read the API doc for exact details.

    Another useful String method is trim - removes spaces before and after
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    Ok, how is the code going to separate the String: "3+4" into the three Strings: "3", "+" and "4"?

    For now assume that "+" is the only operator to worry about. Later work on getting the others.
    First the code must find the location of the "+"
    Then what?

    Some info on using the substring method:
    substring(0, index) returns String from start of String
    substring(index) returns String through the end of the String
    Read the API doc for exact details.

    Another useful String method is trim - removes spaces before and after
    Okay I've been trying to do that with "+" for example with an index of code, but I don't understand the result when I run the code in the terminal I just get 1.

    String num = "1+2";
    int tecken = num.indexOf('+');
            System.out.println(tecken);
    String operator = "";
            for (int o = 0; o <= num.length(); o++) {
                if (num.contains("+"))
                    operator = "+";
     
                else if (num.contains("-"))
                    operator = "-";
                else if (num.contains("*")){
                    operator = "*";
            } else {
                System.out.println("There is no operator");
                return;
            }
        }
    Last edited by Dragonfly11; November 5th, 2019 at 03:33 AM.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

      String num = "1+2";
      int tecken = num.indexOf('+');
     System.out.println(tecken);     //<<< This prints a 1, the location of the +
    tecken now has the location of the + in the String
    Use the substring method to get the value before the + >> "1"
    Use substring to get the value after the + >> "2"
    The code now has the three parts of the expression: "1", "+" , "2"
    convert the two operands "1" and "2" to int values using the parseInt method
    now do the + operation by adding the two int values together
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
      String num = "1+2";
      int tecken = num.indexOf('+');
     System.out.println(tecken);     //<<< This prints a 1, the location of the +
    tecken now has the location of the + in the String
    Use the substring method to get the value before the + >> "1"
    Use substring to get the value after the + >> "2"
    The code now has the three parts of the expression: "1", "+" , "2"
    convert the two operands "1" and "2" to int values using the parseInt method
    now do the + operation by adding the two int values together
    Okay like this?
    String num1 = num.substring(0, 2); 
            String num2 = num.substring(0, 1);

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    No. The substring method should use the value returned by the indexOf method. The args for the method should not be numbers.

    There are two Strings you want to extract: "1" and "2"

    Earlier I said:
    substring(0, index) returns String from start of String
    substring(index) returns String through the end of the String

    To get the "1" which starts at the beginning of the String use this syntax: substring(0, index)
    To get the "2" which is in the String after the "+", use this syntax: substring(index)

    The value in index may need to be adjusted so the String does not include the +. If you print the values of the Strings returned by substring you will see which one needs to be adjusted.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    No. The substring method should use the value returned by the indexOf method. The args for the method should not be numbers.

    There are two Strings you want to extract: "1" and "2"

    Earlier I said:
    substring(0, index) returns String from start of String
    substring(index) returns String through the end of the String

    To get the "1" which starts at the beginning of the String use this syntax: substring(0, index)
    To get the "2" which is in the String after the "+", use this syntax: substring(index)

    The value in index may need to be adjusted so the String does not include the +. If you print the values of the Strings returned by substring you will see which one needs to be adjusted.
    like this?
    int index = num.indexOf("num");
            String num2 = num.substring(0, index);
            String num1 = num.substring(index);

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    Not quite right. The indexOf method should find the location of the "+" like it did in posts #17 and #18
    What happened when you compiled and executed that code? You should always compile and execute code BEFORE posting it so you can copy any error messages and post them with the code.

    Did you print out the values in num1 and num2 to check?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    Not quite right. The indexOf method should find the location of the "+" like it did in posts #17 and #18
    What happened when you compiled and executed that code? You should always compile and execute code BEFORE posting it so you can copy any error messages and post them with the code.

    Did you print out the values in num1 and num2 to check?
    String num2 = num.substring(tecken, 2);
            String num1 = num.substring(0, tecken);
    it only print out 1
    Last edited by Dragonfly11; November 5th, 2019 at 08:52 AM.

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,637
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Help with calculator

    What is printed out when you compile and execute the code.
    Be sure to print the values in num1 and num2


      String num2 = num.substring(tecken, 2);
    The substring does not need the 2 if you want all of the rest of the String returned.

    it only print out 1
    What is that the value of?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Nov 2019
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculator

    Quote Originally Posted by Norm View Post
    What is printed out when you compile and execute the code.
    Be sure to print the values in num1 and num2


      String num2 = num.substring(tecken, 2);
    The substring does not need the 2 if you want all of the rest of the String returned.


    What is that the value of?
    String num2 = num.substring(tecken);
            String num1 = num.substring(0, tecken);
    Okay! So the 0,tecken prints out the first number and the one with tecken prints out + and the second number

Page 1 of 3 123 LastLast

Similar Threads

  1. help me please this is a calculator by the way
    By ryry163 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2014, 05:02 PM
  2. Help with a calculator
    By minipanda1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 9th, 2013, 10:17 AM
  3. Help with a calculator
    By Joshroark10 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 10th, 2012, 03:59 PM
  4. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  5. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM