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
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.
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.
Yes that is all that should be in num2 - the second operand: "2"(tecken+1) only prints our the last number now!
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.
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.
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.
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.
Leave them as int values.making them into one string
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.
A statement that assigns a value to a variable. The variable is on the left, followed by =, followed by a valuewhat is assignment statement?
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.
Did you try that? What happened?
If you don't understand my answer, don't ignore it, ask a question.
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....
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.
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.
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.
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.
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?
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.