Results 1 to 4 of 4
- 03-15-2012, 02:57 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
help me out am kinda new to java and can't figure out these errors..
import javax.swing.JOptionPane;
public class calculator{
static String a= JOptionPane.showInputDialog(null,"assign a value to A");
int A=Integer.parseInt(a);
static String b= JOptionPane.showInputDialog(null, "assign a value to B");
int B=Integer.parseInt(b);
public static void add(){
JOptionPane.showMessageDialog(null, " A plus B is equal to...");
int answer = a+b;
JOptionPane.showMessageDialog(null,answer);
}
public static void multiply(){
JOptionPane.showMessageDialog(null, "A multiplied by B is equal to..");
int answer=a*b;
JOptionPane.showMessageDialog(null,answer);
}
public static void divide(){
JOptionPane.showMessageDialog(null, "A dividde by B is eqaul to...");
int answer=a/b;
JOptionPane.showMessageDialog(null,answer);
}
public static void sub(){
JOptionPane.showMessageDialog(null, "A minus B is equal to..");
int answer=a-b;
JOptionPane.showMessageDialog(null,answer);
}
}
when i compile i get these errors:
calculator.java:19: incompatible types
found : java.lang.String
required: int
int answer=a+b;
^
calculator.java:25: operator * cannot be applied to java.lang.String,java.lang.String
int answer=a*b;
^
calculator.java:31: operator / cannot be applied to java.lang.String,java.lang.String
int answer=a/b;
^
calculator.java:37: operator - cannot be applied to java.lang.String,java.lang.String
int answer=a-b;
^
-
Re: help me out am kinda new to java and can't figure out these errors..
From this code:
Which variable holds a String and which variable holds an int?Java Code:static String a= JOptionPane.showInputDialog(null,"assign a value to A"); int A=Integer.parseInt(a);
And from this code:
What are you trying to do math on, ints or Strings?Java Code:int answer = a*b;
And knowing the above, doesn't this error message make sense?
Also, please make your variable names comply with Java conventions and all start with lower case letters.Java Code:operator * cannot be applied to java.lang.String,java.lang.String
Also, you seem to be making some variables static and some non-static. Understand that you can only use static variables within static methods. This may be why you are using the wrong variables for your math equations.
- 03-15-2012, 03:49 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
-
Re: help me out am kinda new to java and can't figure out these errors..
Similar Threads
-
Kinda stuck... help?
By Samir4021 in forum New To JavaReplies: 9Last Post: 12-05-2011, 02:45 PM -
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
2 Errors Please help em figure out what is wrong with this
By jwb4291 in forum Advanced JavaReplies: 16Last Post: 08-09-2010, 11:40 PM -
Kinda stuck of learning Java
By jurka in forum New To JavaReplies: 2Last Post: 02-14-2009, 04:00 PM -
need help, weird question kinda.
By carlos123 in forum New To JavaReplies: 6Last Post: 01-22-2008, 03:19 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks