Search:

Type: Posts; User: Norm

Page 1 of 2 1 2

Search: Search took 0.10 seconds.

  1. Replies
    64
    Views
    4,433

    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...
  2. Replies
    64
    Views
    4,433

    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...
  3. Replies
    64
    Views
    4,433

    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.
    ...
  4. Replies
    64
    Views
    4,433

    Re: Help with calculator

    The posted code is missing the import statements and the declaration of the class.
  5. Replies
    64
    Views
    4,433

    Re: Help with calculator

    Please post the full code so we can see what it is doing.
  6. Replies
    64
    Views
    4,433

    Re: Help with calculator

    Did you try it? What happened?
  7. Replies
    64
    Views
    4,433

    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
  8. Replies
    64
    Views
    4,433

    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. Replies
    64
    Views
    4,433

    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.
  10. Replies
    64
    Views
    4,433

    Re: Help with calculator

    That looks like a start. Now add the method definition and use a Character class method.
  11. Replies
    64
    Views
    4,433

    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...
  12. Replies
    64
    Views
    4,433

    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...
  13. Replies
    64
    Views
    4,433

    Re: Help with calculator

    Did you try that? What happened?
  14. Replies
    64
    Views
    4,433

    Re: Help with calculator

    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 =...
  15. Replies
    64
    Views
    4,433

    Re: Help with calculator

    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...
  16. Replies
    64
    Views
    4,433

    Re: Help with calculator

    Ok, if those two values are in variables, can you add the variables together to get the desired result?
  17. Replies
    64
    Views
    4,433

    Re: Help with calculator

    What do you want to see on the console when the program executes?
  18. Replies
    64
    Views
    4,433

    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.
  19. Replies
    64
    Views
    4,433

    Re: Help with calculator

    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...
  20. Replies
    64
    Views
    4,433

    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...
  21. Replies
    64
    Views
    4,433

    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...
  22. Replies
    64
    Views
    4,433

    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...
  23. Replies
    64
    Views
    4,433

    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...
  24. Replies
    64
    Views
    4,433

    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...
  25. Replies
    64
    Views
    4,433

    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....
Results 1 to 25 of 32
Page 1 of 2 1 2