Reputation: 11
I am VERY new to Java (ie week 2 of AP Computer Science A). we have to use JOptionPane to make message boxes to use with math operations. My code was fine at school but I had to reformat it because I saved it on Facebook (no other option). Now I get errors on all the JOptionPane lines saying "Multiple markers at this line" and " Syntax error on tokens". How do I fix that. here is the code (please only fix what I am asking, nothing else, I know the code is probably weird)
import javax.swing.JOptionPane;
public class OptioPane {
public static void main(String[] args) {
}
String a = JOptionPane.showInputDialog("Enter an integer");
String b = JOptionPane.showInputDialog("Input another");
int x = Integer.parseInt(a);
int y = Integer.parseInt(b);
JOptionPane.showMessageDiaglog(null, "The numbers added together is " +(x+y));
String c = JOptionPane.showInputDialog("Enter an integer");
String d = JOptionPane.showInputDialog("Input another");
int f = Integer.parseInt(c);
int g = Integer.parseInt(d);
JOptionPane.showMessageDialog(null, "The second number subtracted from the first number is " +(f-g));
String s = JOptionPane.showInputDialog("Enter an integer");
String r = JOptionPane.showInputDialog("Input another");
int w = Integer.parseInt(a);
int q = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The numbers multiplied together is " +(w*q));
String k = JOptionPane.showInputDialog("Enter an integer");
String j = JOptionPane.showInputDialog("Input another");
int n = Integer.parseInt(a);
int m = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The first number divided by the second number is " +(n/m));
String fir = JOptionPane.showInputDialog("Enter an integer");
String tir = JOptionPane.showInputDialog("Input another");
int ah = Integer.parseInt(a);
int bh = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The first number modulated by the second number is " +(ah*bh));
}
Upvotes: 0
Views: 8237
Reputation: 1
If you face any issue I guess there are some suggestions given below the error in Eclipse. I have also encountered the same error it suggested me to import the java swing package then it got fixed.
Upvotes: -1
Reputation: 1
Try this:
import javax.swing.JOptionPane;
public class Dialogue1 {
public static void main(String[]arg)
{
String number1,number2;
number1=JOptionPane.showInputDialog("enter first number");
number2=JOptionPane.showInputDialog("enter second number");
int num1=Integer.parseInt(number1);
int num2=Integer.parseInt(number2);
int sum=num1+num2;
String message=String.format("the sum is %d",sum);
JOptionPane.showMessageDialog(null,sum);
}
}
Store your result in another variable and then display it. It will work for sure without any error.
Upvotes: -1
Reputation: 1
I saw your comment about multiplication and if you still have problems with it, it's because of this
String s = JOptionPane.showInputDialog("Enter an integer");
String r = JOptionPane.showInputDialog("Input another");
int w = Integer.parseInt(a);
int q = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The numbers multiplied together is " +(w*q));
when you input 5 into 's' and 5 into 'r' and get 30, you probably had 5 and 6 in 'a' and 'b' respectively.
Upvotes: 0
Reputation: 7300
It seems that you did something wrong while re-formatting the code because the entire code block should be inside the curly braces of the main method
Upvotes: 1
Reputation: 347332
Basically, the body of your code is out side of any executable context (not where it belongs)....
public class OptioPane {
public static void main(String[] args) {
}
/* All your stuff - out of bounds and behaving badly */
}
Instead, you need to place this code within a executable context, such as the main
method...
public class OptioPane {
public static void main(String[] args) {
/* All your stuff - playing nicely */
}
}
Oh, add the this line...
JOptionPane.showMessageDiaglog(null, "The numbers added together is " +(x+y));
Is wrong, see the extra g
in Dialog
, it should be...
JOptionPane.showMessageDialog(null, "The numbers added together is " +(x+y));
Upvotes: 1