Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: help me out am kinda new to java and can't figure out these errors..

  1. #1
    fireman is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default 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;
    ^

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: help me out am kinda new to java and can't figure out these errors..

    From this code:
    Java Code:
    static String a= JOptionPane.showInputDialog(null,"assign a value to A");
    int A=Integer.parseInt(a);
    Which variable holds a String and which variable holds an int?

    And from this code:
    Java Code:
    int answer = a*b;
    What are you trying to do math on, ints or Strings?

    And knowing the above, doesn't this error message make sense?
    Java Code:
    operator * cannot be applied to java.lang.String,java.lang.String
    Also, please make your variable names comply with Java conventions and all start with lower case letters.

    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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: help me out am kinda new to java and can't figure out these errors..

    Quote Originally Posted by Fubarable View Post
    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.
    I think you missed the non- in those bolded bits.
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: help me out am kinda new to java and can't figure out these errors..

    Quote Originally Posted by Tolls View Post
    I think you missed the non- in those bolded bits.
    Yep, my bad, and thanks for the catch!

Similar Threads

  1. Kinda stuck... help?
    By Samir4021 in forum New To Java
    Replies: 9
    Last Post: 12-05-2011, 03:45 PM
  2. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  3. Replies: 16
    Last Post: 08-10-2010, 12:40 AM
  4. Kinda stuck of learning Java
    By jurka in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 05:00 PM
  5. need help, weird question kinda.
    By carlos123 in forum New To Java
    Replies: 6
    Last Post: 01-22-2008, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •