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.
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.
Did you try it? What happened?
If you don't understand my answer, don't ignore it, ask a question.
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.
Please post the full code so we can see what it is doing.No it doesn't work
If you don't understand my answer, don't ignore it, ask a question.
thank you so much!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"); //} } } }
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.
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"); //} } } }
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.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.
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.
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...
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.
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.