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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 65

Thread: Help with calculator

  1. #26
    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

    Looks like you are making progress. As I said in post#20, the index value needs to be adjusted so that the String does not have the "+"
    For example use index+1 instead of just index as the argument to substring
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    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
    Looks like you are making progress. As I said in post#20, the index value needs to be adjusted so that the String does not have the "+"
    For example use index+1 instead of just index as the argument to substring
    Yes thank you! (tecken+1) only prints our the last number now.

  3. #28
    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

    (tecken+1) only prints our the last number now!
    Yes that is all that should be in num2 - the second operand: "2"

    Change the expression to something with values that will not be confused with index values. For example: "321+44"
    The problem with "1+2" is that when 1 is printed, you do not know if it is the index of "+" or the value of the first operand.
    When the second operand's value is printed, you should see "44" not "2"
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    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
    Yes that is all that should be in num2 - the second operand: "2"

    Change the expression to something with values that will not be confused with index values. For example: "321+44"
    The problem with "1+2" is that when 1 is printed, you do not know if it is the index of "+" or the value of the first operand.
    When the second operand's value is printed, you should see "44" not "2"
    I understand I changed my string to 341+44 and tecken+1 prints out 44 and 0, tecken prints out 321

  5. #30
    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

    Ok, now the expression has been broken into three parts. Now convert the operands to int values and do the addition to get the results.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    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, now the expression has been broken into three parts. Now convert the operands to int values and do the addition to get the results.
    Okay
    int num4 = Integer.parseInt(num2);
            int num5 = Integer.parseInt(num1);
    What am I going to put in my print code?

  7. #32
    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

    What do you want to see on the console when the program executes?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    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 do you want to see on the console when the program executes?
    The result of what 341+44 is

  9. #34
    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

    Ok, if those two values are in variables, can you add the variables together to get the desired result?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    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, if those two values are in variables, can you add the variables together to get the desired result?
    I tried making them into one string and convert it to int to print out but then it prints out 34144

    String result = num1 + num2;
            int num4 = Integer.parseInt(result);
     
            System.out.println(result);

  11. #36
    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

    making them into one string
    Leave them as int values.
    Use an assignment statement to assign their sum to another int variable.
    Then print the value of that int variable.

    The + operator with two Strings concatenates their values: "asd" + "def" gives "asddef"
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    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
    Leave them as int values.
    Use an assignment statement to assign their sum to another int variable.
    Then print the value of that int variable.

    The + operator with two Strings concatenates their values: "asd" + "def" gives "asddef"
    okay what is assignment statement?

  13. #38
    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

    what is assignment statement?
    A statement that assigns a value to a variable. The variable is on the left, followed by =, followed by a value
    Examples from your code:
    String result = num1 + num2;
    int num4 = Integer.parseInt(result);
    int tecken = num.indexOf('+');

    See the tutorial: https://docs.oracle.com/javase/tutor...bolts/op1.html
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    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
    A statement that assigns a value to a variable. The variable is on the left, followed by =, followed by a value
    Examples from your code:
    String result = num1 + num2;
    int num4 = Integer.parseInt(result);
    int tecken = num.indexOf('+');

    See the tutorial: https://docs.oracle.com/javase/tutor...bolts/op1.html
    like this?
    int result = (num4 + num5);
    System.out.println(result);

  15. #40
    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 that? What happened?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    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 that? What happened?
    Yes it worked!! I only need now to make error messages when you try to /0 and if you try to use more than to numbers. So when you type in for example 2+2-1, you're supposed to get an error message. I've tried with 0 like
    if (s.contains("/0")){
    JOptionPane.showMessageDialog(null, "error");
    }
    but it doesn't work and for the other I don't know....

  17. #42
    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

    Do you know about try{}catch for trapping exceptions? If so you could put the parseInt method calls inside of a try{}catch block and let the parseInt method throw an exception if either of the two operands (before and after the opcode) are not valid int values.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #43
    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
    Do you know about try{}catch for trapping exceptions? If so you could put the parseInt method calls inside of a try{}catch block and let the parseInt method throw an exception if either of the two operands (before and after the opcode) are not valid int values.
    No I haven't learn about it yet and I'm not sure if my teacher will allow it but I'll ask him. So I can't use if statement or a for loop?

  19. #44
    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

    Yes, you can make a method to scan a String and test if it contains only digits. The String class has methods for getting at each character in the String.
    The Character class has methods to test a character to see if it is a digit.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #45
    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
    Yes, you can make a method to scan a String and test if it contains only digits. The String class has methods for getting at each character in the String.
    The Character class has methods to test a character to see if it is a digit.
    okay something like this?
    for (i=0; i<s.length(); i++)
    			if (s.charAt(i) == '')

  21. #46
    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

    That looks like a start. Now add the method definition and use a Character class method.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #47
    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
    That looks like a start. Now add the method definition and use a Character class method.
    Okay I have solved all of it and I don't have much time on this assignment. How do I make error message if there is a letter in the input?
    Last edited by Dragonfly11; November 6th, 2019 at 09:18 AM.

  23. #48
    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 test inside of the if statement should be about the char being a digit or not. See the Character class for method to use.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #49
    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 test inside of the if statement should be about the char being a digit or not. See the Character class for method to use.
    okay I solved it like this

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

    I missed making an error message if there is a letter, how do I make it?

  25. #50
    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

    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'
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 3 FirstFirst 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