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: Help for simple calculator code

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help for simple calculator code

    This code wont work for some reason. For example when i input 4+7 it says that it's 108.
    import java.util.Scanner;
     
    public class myCalculator {
        public static void main(String[] args) {
            Scanner Calculator = new Scanner(System.in);
     
            System.out.println("Plese input equation. NO DOUBLE DIGITS!");
            String calculator = Calculator.next();
     
            char[] op = calculator.toCharArray();
     
            switch(op[1]){
                case('+'):
                double stuff = (op[0] + op[2]);
                System.out.println(stuff);
                break;
     
                case('-'):
                double stuff0 = (op[0] - op[2]);
                System.out.println(stuff0);
                break;
     
                case('*'):
                double stuff1 = (op[0] * op[2]);
                System.out.println(stuff1);
                break;
     
                case('/'):
                double stuff2 = (op[0] / op[2]);
                System.out.println(stuff2);
                break;
     
                default:
                System.out.println("ERROR");
            }
        }
    }
    Last edited by Andthehand; May 15th, 2019 at 12:21 PM.

  2. #2
    Junior Member
    Join Date
    Apr 2019
    Posts
    25
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Help for simple calculator code

    It works but need input 4 char + - * /, if less then 4 char throw Exception

     
    import java.util.Scanner;
     
    public class myCalculator {
        public static void main(String[] args) {
            Scanner Calculator = new Scanner(System.in);
     
            System.out.println("Plese input equation. NO DOUBLE DIGITS!");
            String calculator = Calculator.next();
     
            char[] op = calculator.toCharArray();
     
            switch (op[1]) {
            case ('+'):
                double stuff = (op[0] + op[2]);
                System.out.println(stuff);
                break;
     
            case ('-'):
                double stuff0 = (op[0] - op[2]);
                System.out.println(stuff0);
                break;
     
            case ('*'):
                double stuff1 = (op[0] * op[2]);
                System.out.println(stuff1);
                break;
     
            case ('/'):
                double stuff2 = (op[0] / op[2]);
                System.out.println(stuff2);
                break;
     
            default:
                System.out.println("ERROR");
            }
        }
    }

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

    Default Re: Help for simple calculator code

    wont work for some reason.
    Please explain what "wont work" means: compiler error, execution error or wrong results.
    Copy the contents of the console and paste it here that shows the problem and add some comments.

    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.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Andthehand (May 15th, 2019)

Similar Threads

  1. Simple Calculator HELP
    By krowley in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 20th, 2014, 08:55 PM
  2. Help with simple calculator
    By GMPoison in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 22nd, 2013, 01:29 PM
  3. Simple Calculator
    By Spanky13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 25th, 2013, 05:33 PM
  4. Simple Commission Calculator
    By mrivera8504 in forum What's Wrong With My Code?
    Replies: 22
    Last Post: August 27th, 2012, 06:35 PM
  5. Simple Calculator
    By JKDSurfer in forum Loops & Control Statements
    Replies: 3
    Last Post: June 28th, 2011, 01:37 PM