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.

Results 1 to 5 of 5

Thread: Calculator Using JOption

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculator Using JOption

    import javax.swing.JOptionPane;

    public class ProjectCalculator {

    public static void main (String[] args)
    {
    double firstnum, secondnum, ans;
    char operator;

    String firstnumStr, secondnumStr;
    String operatorStr;
    String ansStr;

    String msg = "Semi Finals Project for Programming 2";
    javax.swing.JOptionPane.showMessageDialog( null, msg );
    String msg1 = "Simple Calculator using the switch statement";
    javax.swing.JOptionPane.showMessageDialog( null, msg1 );


    firstnumStr=
    JOptionPane.showInputDialog("Enter the First Number");
    firstnum = Double.parseDouble(firstnumStr);

    secondnumStr=
    JOptionPane.showInputDialog("Enter the Second number");
    secondnum = Double.parseDouble(secondnumStr);

    operatorStr=
    JOptionPane.showInputDialog("Enter the operator to be use");
    operator = (char) Double.parseDouble(operatorStr);

    if (operator == '+' ) {
    System.out.print("You have choosen Addition \n");
    ans=firstnum + secondnum;
    System.out.print("The sum is = " + ans );
    }
    else if(operator == '-') {
    System.out.print("You have choosen Subtraction \n");
    ans=firstnum-secondnum;
    System.out.print("The difference is = " + ans );
    }

    else if(operator == '/'){
    System.out.print("You have choosen Division \n");
    ans=firstnum/secondnum;
    System.out.print("The quotient is = " + ans );
    }
    else if(operator == '*'){
    System.out.print("You have choosen Multiplication \n");
    ans=firstnum*secondnum;
    System.out.print("The product is = " + ans );
    }

    else

    return;

    ansStr = "First Number:" + firstnum + " \n" +
    "Second Number:" + secondnum + " \n +" +
    "Operator: " + operator + "\n" +
    "Solution: " + ans + " \n +";


    }

    }

    Hello guys can you please help me i just cant run this program it says (bellow) I dont know what to change. THank you in advance.

    Exception in thread "main" java.lang.NumberFormatException: For input string: "+"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
    at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at ProjectCalculator.main(ProjectCalculator.java:30)

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Calculator Using JOption

    When you retrieve the operator you are trying to parse it as though it were a double. It is a String and does not represent a number. You need to extract the character as a char.

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Nov 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculator Using JOption

    Can you show me please what code should I change?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,322
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Calculator Using JOption

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Calculator Using JOption

    The last line of the following code is incorrect.

    operatorStr=
    JOptionPane.showInputDialog("Enter the operator to be use");
    operator = (char) Double.parseDouble(operatorStr);

    operator is a char as you have properly declared. But you are using Double.parseDouble which is wrong. You need to extract the character from the String. Check out the String API and see if you can find a good method to use. This will be good practice for future issues and a good way to learn the Java API. I also recommend that you write a small separate program to see if your correction works before incorporating it into your main program. Something as simple as extracting the char from the String and printing it. And remember that there could be white space and other characters in the inputs of your JOptionPane so you might want to take care of that too. It all depends on what the requirements of the assignment are.

    Regards,
    Jim

  6. The Following User Says Thank You to jim829 For This Useful Post:

    johndoe123 (November 19th, 2018)

Similar Threads

  1. JOption help
    By Ryn440 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 27th, 2014, 12:45 PM
  2. Java joption
    By darking123 in forum Object Oriented Programming
    Replies: 9
    Last Post: February 5th, 2013, 09:51 AM
  3. Help incorporating a Joption Pane
    By Dysun in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2012, 05:25 PM
  4. [SOLVED] I have a problem with this Joption programme.
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 12th, 2011, 03:31 AM
  5. JOption Pane help
    By dallas1125 in forum AWT / Java Swing
    Replies: 5
    Last Post: November 18th, 2009, 04:08 PM