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

Thread: Java calculator using swing and applet

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

    Default Java calculator using swing and applet

    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: ");
    float x = kbReader.nextFloat();
    JOptionPane.showInputDialog("Enter second number: ");
    float y = kbReader.nextFloat();


    Scanner kbReader = new Scanner(System.in);
    int choice = kbReader.nextInt();


    if((choice<=5) && (choice>0))
    {

    switch (choice)
    {

    case 1: //addition 1 decimal
    System.out.println(x + " plus " + y + " = " + (x + y) );
    break;

    case 2: //subtraction no decimal
    System.out.println(x + " minus " + y + " = " + (x - y) );
    break;

    case 3: //multiplication 3 decimal
    System.out.println(x + " times " + y + " = " + (x * y) );
    break;

    case 4: //division 4 decimal
    System.out.println(x + " divided by " + y + " = " + (x / y) );
    break;

    case 5: //modulo
    System.out.println(x + " modulo " + y + " = " + (x % y) );
    //break;
    }

    }
    else
    {
    System.out.println("Please enter a 1, 2, 3, 4 or 5.");
    }
    }
    }

    --- Update ---

    please help me solve my code its for my final exam. the code doesn't continue to its input number


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

    Default Re: Java calculator using swing and applet

    Hello.
    You are first reading the operation using JOptionpane. So you don't need Scanner anymore.
    Just replace the scanner code with this line. It should work.
    //Scanner kbReader = new Scanner(System.in);
    //int choice = kbReader.nextInt();

    int choice=Integer.parseInt(input);

    Thats it.

    Thanks,
    Syed.

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

    Default Re: Java calculator using swing and applet

    Quote Originally Posted by syedbhai View Post
    Hello.
    You are first reading the operation using JOptionpane. So you don't need Scanner anymore.
    Just replace the scanner code with this line. It should work.
    //Scanner kbReader = new Scanner(System.in);
    //int choice = kbReader.nextInt();

    int choice=Integer.parseInt(input);

    Thats it.

    Thanks,
    Syed.
    tons of thanks sir. now my problem is how to convert those int to float and displays required decimal places.

  4. #4
    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 using swing and applet

    Quote Originally Posted by lordrey View Post
    tons of thanks sir. now my problem is how to convert those int to float and displays required decimal places.
    What did you try?
    Your homework is meant for you to figure out, to give you the ability to solve the problem. Not to see how someone on a forum would solve the problem.

Similar Threads

  1. Applet Calculator
    By arvin17 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 24th, 2013, 06:41 AM
  2. HELP - GUI Java Applet Calculator..
    By AtOmTen in forum AWT / Java Swing
    Replies: 2
    Last Post: March 24th, 2013, 06:13 AM
  3. Help with Swing applet
    By IHeartProgramming in forum AWT / Java Swing
    Replies: 3
    Last Post: December 23rd, 2012, 02:53 AM
  4. Interest calculator applet
    By bruizer in forum Java Applets
    Replies: 4
    Last Post: September 22nd, 2012, 08:38 PM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM