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: JAVA Calculator Problem

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAVA Calculator Problem

    Please Help me solve my problem in using switch and also the answer would be output required decimal places. its for my final exam. thanks a lot.

    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
     
    public class exercise1
    {
     
    	public static void main(String args[]){
    		String input = JOptionPane.showInputDialog("Selection from the choices below:\n\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n 5. Modulo\n\n Your choice?");
     
     
    	JOptionPane.showInputDialog("\nEnter first number: ");
    	int x = Integer.parseInt(x);
    	JOptionPane.showInputDialog("Enter second number: ");
    	int y = Integer.parseInt(y);
     
     
    	int choice = Integer.parseInt(input);
    	if((choice<=5) && (choice>0))
    	{
     
    	switch (choice)
    	{
     
    	case 1: //addition 1 decimal place
    		JOptionPane.showMessageDialog(null, x + " plus " +  y + " = " + (x + y);
    	break;
     
    	case 2: //subtraction no decimal place
    		JOptionPane.showMessageDialog(x + " minus " + y + " = " + (x - y) );
    	break;
     
    	case 3: //multiplication 3 decimal place
    		JOptionPane.showMessageDialog(x + " times " + y + " = " + (x * y) );
    	break;
     
    	case 4: //division 4 decimal place
    		JOptionPane.showMessageDialog(x + " divided by " + y + " = " + (x / y) );
    	break;
     
    	case 5: //modulo
    		JOptionPane.showMessageDialog(x + " modulo " + y + " = " + (x % y) );
    	//break;
    	}
     
    	}
    	else
    	{
    	System.out.println("Please enter a 1, 2, 3, 4 or 5.");
    	}
    }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: JAVA Calculator Problem

    Quote Originally Posted by lordrey View Post
    Please Help me solve my problem in using switch
    What is your problem in using switch?
    Does it compile? Is there an error?

    If you need help, ask a question besides the implied "give me a working solution"

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: JAVA Calculator Problem

    Hello.
    Thats simple.
    If you want floating or double values then change the type of the variables x and y respectively. You will have to use the parseXxx() of the corresponding wrapper class.
    In simpler words the way you are doing for integers the same way you do for floats/doubles.

    Thanks,
    Syed.

Similar Threads

  1. Calculator dividing by 0 problem.
    By BITmixit in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 18th, 2012, 09:06 PM
  2. Infix calculator problem
    By cubertron in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2011, 07:32 AM
  3. [SOLVED] Another Calculator Problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 4th, 2010, 03:31 PM
  4. Calculator problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 4th, 2010, 01:01 PM
  5. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 02:09 PM