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 3 of 3

Thread: Calculator Using JOptionPane

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    Manila, Philippines
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculator Using JOptionPane

    Guys need help on this. I'm making a calculator using a JOptionPane. But the Operator part where I can choose what operation to be use was stopped performing. Thank you guys
    *
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
    public class Calculator
    {
        public static void main(String [] args)
        {
            Scanner norbel = new Scanner(System.in);
     
            int answer;
     
            String FNumber = JOptionPane.showInputDialog("Enter first Integer: ");
            int fnum = Integer.parseInt(FNumber);
            fnum = norbel.nextInt();     
            String operator = JOptionPane.showInputDialog("Operation:\n 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
            Integer opt = Integer.parseInt(operator);
     
     
            if(opt == 1)
            {
                String opt1 = JOptionPane.showInputDialog("Enter second Integer: ");
                int snum = Integer.parseInt(opt1);
                snum = norbel.nextInt();
                answer = fnum + snum;
                String ans = "Answer" +answer+ "!";
                JOptionPane.showMessageDialog(null,answer);
     
            if(opt == 2)
            {
                String opt2 = JOptionPane.showInputDialog("Enter second Integer: ");
                int snum = Integer.parseInt(opt2);
                snum = norbel.nextInt();
                answer = fnum -  snum;
                String ans = "Answer" +answer+ "!";
                JOptionPane.showMessageDialog(null,ans);
            }
            if(opt == 3)
            {
                String opt3 = JOptionPane.showInputDialog("Enter second Integer: ");
                int snum = Integer.parseInt(opt3);
                snum = norbel.nextInt();
                answer = fnum * snum;
                String ans = "Answer" +answer+ "!";
                JOptionPane.showMessageDialog(null,ans);
            }
            if(opt == 4)
            {
                String opt4 = JOptionPane.showInputDialog("Enter second Integer: ");
                int snum = Integer.parseInt(opt4);
                snum = norbel.nextInt();
                answer = fnum / snum;
                String ans = "Answer" +answer+ "!";
                JOptionPane.showMessageDialog(null,ans);
            }
            else
            {
                String error = "Error";
                JOptionPane.showMessageDialog(null, error);
            }
     
        }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calculator Using JOptionPane

    Right now your code has lost all formatting and is all but unreadable. Please don't highlight your code with color but rather look at the forum FAQ's and use code tags to help your posted code retain its formatting and be readable.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calculator Using JOptionPane

    Why does the code use both the JOptionPane AND the Scanner class to read input from the user. That would be very confusing. I'd get rid of one of those methods and get all the input using just one technique.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Still having problem with JOptionPane
    By runkerr in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 28th, 2013, 07:40 PM
  2. While JOptionPane equal Yes????
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 08:57 PM
  3. Display in JOptionPane
    By t-rank in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 19th, 2010, 12:09 AM
  4. Change to JOptionPane
    By Liuric in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 12:07 AM
  5. JOptionPane using If and Else
    By Liuric in forum Member Introductions
    Replies: 7
    Last Post: October 1st, 2010, 12:05 AM