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 3 of 3 FirstFirst 123
Results 51 to 65 of 65

Thread: Help with calculator

  1. #51
    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 you want to test if all the characters in a String are digits, use the Character class's method to test each character in a loop.

    Another way would be to test if each character is >= '0' and <= '9'
    okay we've been told to use isdigit but I don't know how to use it...
    Last edited by Dragonfly11; November 6th, 2019 at 10:02 AM.

  2. #52
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with calculator

    Here is how you use a static method:
    returnValue = TheClassName.theMethodName(theArgs);

    Read the API doc for specifics: http://docs.oracle.com/javase/8/docs/api/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #53
    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
    Here is how you use a static method:
    returnValue = TheClassName.theMethodName(theArgs);

    Read the API doc for specifics: http://docs.oracle.com/javase/8/docs/api/index.html
    okay thank you like this?
    returnValue = TheClassName.theMethodName(tal1);
    Last edited by Dragonfly11; November 6th, 2019 at 11:14 AM.

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with calculator

    Did you try it? What happened?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #55
    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
    Did you try it? What happened?
    No it doesn't work, I try doing it with a for loop with isdigit in it
    for (int i = 0; i <s.length() ; i++) {
    char a4 = s.charAt(i);
    if (!Character.isDigit(a4)) {
    JOptionPane.showMessageDialog(null, "you need to use numbers");
    System.exit(0);
    }

    }
    it shows error message but when I try to write 1+1 it also display the error message.

  6. #56
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with calculator

    No it doesn't work
    Please post the full code so we can see what it is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #57
    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 post the full code so we can see what it is doing.

       String tal1 = "";
            String tal2 = "";
     
            String s = JOptionPane.showInputDialog("Skriv in tal");
     
     
            if (s.isEmpty()) {
                JOptionPane.showMessageDialog(null, "Får inte vara tomt");
            }
            if (s.contains("%")) {
                JOptionPane.showMessageDialog(null, "Får inte innehålla %");
     
            }
            if (s.contains(" ")) {
                JOptionPane.showMessageDialog(null, "Hittar ingen operator");
            }
            if ((s.startsWith("+")) || (s.startsWith("-")) || (s.startsWith("*")) || (s.startsWith("/"))) {
                System.out.println("error start with");
                System.exit(0);
            }
            if ((s.endsWith("+")) || (s.endsWith("-")) || (s.endsWith("*")) || (s.endsWith("/"))) {
                System.out.println("error end with");
                System.exit(0);
            }
     
     
            int count = 0;
     
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '*' || s.charAt(i) == '/' || s.charAt(i) == '+' || s.charAt(i) == '-') {
     
     
                    count++;
                }
     
            }
            if (count > 1) {
                System.out.println("error");
                System.exit(0);
            }
            for (int i = 0; i < s.length(); i++) {
                char a4 = s.charAt(i);
                if (!Character.isDigit(a4)) {
                    JOptionPane.showMessageDialog(null, "Du behöver använda dig av siffror");
                    System.exit(0);
     
                    if (s.contains("*")) {
                        int tal = s.indexOf("*");
                        tal1 = s.substring(0, tal);
                        tal2 = s.substring(tal + 1);
                        System.out.println("tal " + tal1);
                        System.out.println("tal " + tal2);
                        double resultat = Double.parseDouble(tal1);
                        double resultat2 = Double.parseDouble(tal2); //error
                        double result = (resultat * resultat2);
                        //System.out.println(result);
                        JOptionPane.showMessageDialog(null, Math.round(result));
     
                    } else if (s.contains("+")) {
                        int tal = s.indexOf("+");
                        tal1 = s.substring(0, tal);
                        tal2 = s.substring(tal + 1);
                        System.out.println("tal " + tal1);
                        System.out.println("tal " + tal2);
                        double resultat = Double.parseDouble(tal1);
                        double resultat2 = Double.parseDouble(tal2);
                        double result = (resultat + resultat2);
                        System.out.println(result);
                        JOptionPane.showMessageDialog(null, Math.round(result));
     
                    } else if (s.contains("-")) {
                        int tal = s.indexOf("-");
                        tal1 = s.substring(0, tal);
                        tal2 = s.substring(tal + 1);
                        System.out.println("tal " + tal1);
                        System.out.println("tal " + tal2);
                        double resultat = Double.parseDouble(tal1);
                        double resultat2 = Double.parseDouble(tal2);
                        double result = (resultat - resultat2);
                        System.out.println(result);
                        JOptionPane.showMessageDialog(null, Math.round(result));
     
     
                    }
                    if (s.contains("/0")) {
                        JOptionPane.showMessageDialog(null, "Det finns ingen operator");
     
                    } else if (s.contains("/")) {
                        int tal = s.indexOf("/");
                        tal1 = s.substring(0, tal);
                        tal2 = s.substring(tal + 1);
                        System.out.println("tal " + tal1);
                        System.out.println("tal " + tal2);
                        double resultat = Double.parseDouble(tal1);
                        double resultat2 = Double.parseDouble(tal2);
                        double result = (resultat / resultat2);
                        System.out.println(result);
                        JOptionPane.showMessageDialog(null, result);
                    }
     
                    //} else {
                    //JOptionPane.showMessageDialog(null, "input error");
                    //}
                }
     
     
            }
        }
    thank you so much!

  8. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with calculator

    The posted code is missing the import statements and the declaration of the class.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #59
    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
    The posted code is missing the import statements and the declaration of the class.
    you mean these?
    package com.company;

    import javax.swing.*;

    public class Main {

    public static void main(String[] args) {

    sorry for not including them

    --- Update ---

    package com.company;
     
    import javax.swing.*;
     
    public class Main {
     
        public static void main(String[] args) {
            String tal1 = "";
            String tal2 = "";
     
            String s = JOptionPane.showInputDialog("Skriv in tal");
     
     
            if (s.isEmpty()) {
                JOptionPane.showMessageDialog(null, "Får inte vara tomt");
            }
            if (s.contains("%")) {
                JOptionPane.showMessageDialog(null, "Får inte innehålla %");
     
            }
            if (s.contains(" ")) {
                JOptionPane.showMessageDialog(null, "Hittar ingen operator");
            }
            if ((s.startsWith("+")) || (s.startsWith("-")) || (s.startsWith("*")) || (s.startsWith("/"))) {
                System.out.println("error start with");
                System.exit(0);
            }
            if ((s.endsWith("+")) || (s.endsWith("-")) || (s.endsWith("*")) || (s.endsWith("/"))) {
                System.out.println("error end with");
                System.exit(0);
            }
     
     
            int count = 0;
     
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '*' || s.charAt(i) == '/' || s.charAt(i) == '+' || s.charAt(i) == '-') {
     
     
                    count++;
                }
     
            }
            if (count > 1) {
                System.out.println("error");
                System.exit(0);
            }
            for (int i = 0; i < s.length(); i++) {
                char a4 = s.charAt(i);
                if (!Character.isDigit(a4)) {
                    JOptionPane.showMessageDialog(null, "Du behöver använda dig av siffror");
                    System.exit(0);
                }
     
     
                    if (s.contains("*")) {
                        int tal = s.indexOf("*");
                        tal1 = s.substring(0, tal);
                        tal2 = s.substring(tal + 1);
                        System.out.println("tal " + tal1);
                        System.out.println("tal " + tal2);
                        double resultat = Double.parseDouble(tal1);
                        double resultat2 = Double.parseDouble(tal2); //error
                        double result = (resultat * resultat2);
                        //System.out.println(result);
                        JOptionPane.showMessageDialog(null, Math.round(result));
     
                    } else if (s.contains("+")) {
                        int tal = s.indexOf("+");
                        tal1 = s.substring(0, tal);
                        tal2 = s.substring(tal + 1);
                        System.out.println("tal " + tal1);
                        System.out.println("tal " + tal2);
                        double resultat = Double.parseDouble(tal1);
                        double resultat2 = Double.parseDouble(tal2);
                        double result = (resultat + resultat2);
                        System.out.println(result);
                        JOptionPane.showMessageDialog(null, Math.round(result));
     
                    } else if (s.contains("-")) {
                        int tal = s.indexOf("-");
                        tal1 = s.substring(0, tal);
                        tal2 = s.substring(tal + 1);
                        System.out.println("tal " + tal1);
                        System.out.println("tal " + tal2);
                        double resultat = Double.parseDouble(tal1);
                        double resultat2 = Double.parseDouble(tal2);
                        double result = (resultat - resultat2);
                        System.out.println(result);
                        JOptionPane.showMessageDialog(null, Math.round(result));
     
     
                    }
                    if (s.contains("/0")) {
                        JOptionPane.showMessageDialog(null, "Det finns ingen operator");
     
                    } else if (s.contains("/")) {
                        int tal = s.indexOf("/");
                        tal1 = s.substring(0, tal);
                        tal2 = s.substring(tal + 1);
                        System.out.println("tal " + tal1);
                        System.out.println("tal " + tal2);
                        double resultat = Double.parseDouble(tal1);
                        double resultat2 = Double.parseDouble(tal2);
                        double result = (resultat / resultat2);
                        System.out.println(result);
                        JOptionPane.showMessageDialog(null, result);
                    }
     
                    //} else {
                    //JOptionPane.showMessageDialog(null, "input error");
                    //}
                }
     
     
            }
        }

  10. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with calculator

    There is a lot of repeated code that should be in a method.
    The test using isDigit should be in a method that is passed the Strings: tal1 and tal2 one at a time to test if they are valid numbers.

    In my first post I said this:
    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.
    The program has lots of problems in it. You need to work out the steps it should take to solve the problem, before writing the code. The code has most of the coding techniques that are needed, but they are not used in a logic way.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #61
    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
    There is a lot of repeated code that should be in a method.
    The test using isDigit should be in a method that is passed the Strings: tal1 and tal2 one at a time to test if they are valid numbers.

    In my first post I said this:


    The program has lots of problems in it. You need to work out the steps it should take to solve the problem, before writing the code. The code has most of the coding techniques that are needed, but they are not used in a logic way.
    I see but I don't know how....and I don't have much time left on this assignment I have to deliver it tomorrow.

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with calculator

    Here is a sample design:
    get user input into a string
    find the operator that should be in the middle of the string -> if not found give error and exit
    split the string into three parts: first operand, operator, second operand
    check that first operand is a valid number (write method for this) -> if not give error
    check that second operand is a valid number (write method for this) -> if not give error
    convert first operand to an int value
    convert second operand to an int value, check if /0
    use the operator to compute the result using the two int values
    show result to user
    If you don't understand my answer, don't ignore it, ask a question.

  13. #63
    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
    Here is a sample design:
    get user input into a string
    find the operator that should be in the middle of the string -> if not found give error and exit
    split the string into three parts: first operand, operator, second operand
    check that first operand is a valid number (write method for this) -> if not give error
    check that second operand is a valid number (write method for this) -> if not give error
    convert first operand to an int value
    convert second operand to an int value, check if /0
    use the operator to compute the result using the two int values
    show result to user
    Okay I see, I feel like I've done that (or at least tried...) but made a really bad code for it....I did manage to fix my problem with a isLetter statement. The problem I have now is that it prints out the input say if you write 1+1, you get a message dialogue 3 times and also in the terminal.
    }if (Character.isLetter(s.charAt(i))){
    System.out.println("error");
    System.exit(0);
    }

    Also Thank you very much for trying to help me I reall appreciate it. It's just a very hard assignment...

  14. #64
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with calculator

    My suggestion is to start over and follow a design for the steps the program should take. Trying to fix what you have now will be harder than writing a new program from the beginning. The current program has most of the needed techniques, but they are not organized correctly.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #65
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Help with calculator

    Sure, here's a solution to your problem. You need to split the user input into operands and the operator. Then perform the calculation based on the operator.

    ```java
    import javax.swing.JOptionPane;
    import java.util.ArrayList;

    public class Calculator {
    public static void main(String[] args) {
    String s = JOptionPane.showInputDialog(null, "Type in expression (e.g., 2+2):");

    // Split the input string into operands and operator
    String[] parts = s.split("[\\+\\-\\*/]");
    int num1 = Integer.parseInt(parts[0]);
    int num2 = Integer.parseInt(parts[1]);
    String operator = s.replaceAll("[0-9]", ""); // Extract operator from the string

    int result = 0;

    // Perform calculation based on the operator
    switch(operator) {
    case "+":
    result = num1 + num2;
    break;
    case "-":
    result = num1 - num2;
    break;
    case "*":
    result = num1 * num2;
    break;
    case "/":
    // Check for division by zero
    if(num2 != 0)
    result = num1 / num2;
    else
    JOptionPane.showMessageDialog(null, "Error: Division by zero");
    break;
    default:
    JOptionPane.showMessageDialog(null, "Error: Invalid operator");
    return;
    }

    // Display the result in a dialogue window
    JOptionPane.showMessageDialog(null, "Result: " + result);
    }
    }
    ```

    This code will take the input from the user, parse it into operands and operator, perform the calculation, and then display the result in a dialogue window.

    Lastly, if you require further help with your Java assignment or any programming tasks, don't hesitate to seek additional resources or help online. There are various platforms like ProgrammingHomworkHelp.com and communities where you can find guidance and support to enhance your programming skills. Exploring reputable online resources related to programming homework assistance could offer valuable insights and solutions.

Page 3 of 3 FirstFirst 123

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