Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:19:40+00:00 2026-06-13T10:19:40+00:00

public class calculator { static char operator; static String operatorWord; static String nameOfFirstNumber; static

  • 0
public class calculator {
static char operator;
static String operatorWord;
static String nameOfFirstNumber;
static String nameOfSecondNumber;
static double firstNumber;
static double secondNumber;
static int response2;

public static void setResponse2(boolean b){
    if(b = true){
        response2 = JOptionPane.YES_OPTION;
    }
    else if(b = false){
        response2 = JOptionPane.NO_OPTION;
    }
    else;
}

public static void setOperator(char c){
    operator = c;
}

public static void setOperatorWord(String s) throws NotAnOperatorException{
    operatorWord = s;
}


public static String getOperatorWord(char c, int i) throws NotAnOperatorException{
    if(c == '*'){
        return "Multiplication";
    }
    else if(c == '/'){
        return "Division";
    }
    else if(c =='+'){
        return "Addition";
    }
    else if(c == '-'){
        return "Subtraction";
    }
    else if(c == '%'){
        return "Remainder";
    }
    if(i < 2){
        throw new NotAnOperatorException();
    }
    return null;
}

public static void defineNameOfNumbers(char c){
    if(c == '*'){
        nameOfFirstNumber = "multiplicand";
        nameOfSecondNumber = "multiplier";
    }
    else if(c == '/'||c == '%'){
        nameOfFirstNumber = "dividend";
        nameOfSecondNumber = "divisor";
    }
    else if(c == '+'){
        nameOfFirstNumber = "addend";
        nameOfSecondNumber = "addend";
    }
    else if(c == '-'){
        nameOfFirstNumber = "subtrahend";
        nameOfSecondNumber = "minuend";
    }
}

public static double getRemainder(){
    if(operator == '/'||operator == '%'){
        return firstNumber%secondNumber;
    }
    else{
        return (Double) null;
    }
}

public static double getAnswer(){
    if(operator == '*'){
        return firstNumber*secondNumber;
    }
    else if(operator == '/'){
        return firstNumber/secondNumber;
    }
    else if(operator == '%'){
        return firstNumber%secondNumber;
    }
    else if(operator == '+'){
        return firstNumber+secondNumber;
    }
    else if(operator == '-'){
        return firstNumber-secondNumber;
    }
    else{
        return (Double) null;
    }
}

public static String getRemainderString(){
    //get the remainder & string...//
    if(operator == '/'){
        int answer = (int) (firstNumber/secondNumber);
        int remainder = (int) (firstNumber%secondNumber);
        return "\n"+answer+" remainder "+remainder;
    }
    else{
        return "";
    }
}



/**
 * @param args
 * @throws NotAnOperatorException 
 * @throws HeadlessException 
 */
public static void main(String[] args) throws HeadlessException, NotAnOperatorException {
    //end of catch statements//
    do{

        //get the operator etc. I know it's a bit overdone.//
        try{
            char operatorThing = JOptionPane.showInputDialog("Operator:").charAt(0);
            setOperator(operatorThing);
            setOperatorWord(getOperatorWord(operator, 1));
            defineNameOfNumbers(operatorThing);
            }
        //catch a NotAnOperatorException (defined with NotAnOperatorException.class)//
            catch(NotAnOperatorException exc){
                Writer writer = new StringWriter();
                PrintWriter printWriter = new PrintWriter(writer);
                exc.printStackTrace(printWriter);
                String excSentence = writer.toString();
                JOptionPane.showMessageDialog(null,"Something went wrong... Here's the rundown on what happened:\n"+excSentence+"\nWe've figured out what happened, though.\nThat's not an operator...\nRestart to rerun it, it will exit for safety.");
                System.exit(0);
            }
        //catch a null pointer//
            catch(NullPointerException exc){
                JOptionPane.showMessageDialog(null,"You clicked Cancel. Press OK to quit.");
                System.exit(0);
        }
        //catch an other exception//
            catch(Exception exc){
                Writer writer = new StringWriter();
                PrintWriter printWriter = new PrintWriter(writer);
                exc.printStackTrace(printWriter);
                String excSentence = writer.toString();
                JOptionPane.showMessageDialog(null,"Something went wrong... Here's the rundown on what happened:\n"+excSentence+"\nI have no idea what happened.\nRestart to rerun it, it will exit for safety.");
                System.exit(0);
            }
        //get the response with "int response"//
        int response;
            response = JOptionPane.showConfirmDialog(null, "Would you like to start "+getOperatorWord(operator, 2)+" Calc?");
    if(response == JOptionPane.YES_OPTION){
        String numberOne = JOptionPane.showInputDialog("X"+operator+"Y=Z\nEnter the "+nameOfFirstNumber+" (the first number.)");
        firstNumber = Double.parseDouble(numberOne);
        String numberTwo = JOptionPane.showInputDialog(firstNumber+operator+"Y=Z\nEnter the "+nameOfSecondNumber+" (the second number.)");
        secondNumber = Double.parseDouble(numberTwo);

        if(operator == '/'||operator == '%'){
            int response3 =
            JOptionPane.showConfirmDialog(null, firstNumber+operator+secondNumber+"="+getAnswer()+getRemainderString()+"\n Would you like to start "+getOperatorWord(operator, 2)+" Calc again?");
            if(response3 == JOptionPane.YES_OPTION){
                setResponse2(true);
            }
            else if(response3 == JOptionPane.NO_OPTION||response3 == JOptionPane.CANCEL_OPTION){
                setResponse2(false);
                System.exit(0);
            }
        }
        else{
            int response3 =
            JOptionPane.showConfirmDialog(null, firstNumber+operator+secondNumber+"="+getAnswer()+"\n Would you like to start "+getOperatorWord(operator, 2)+" Calc again?");
            if(response3 == JOptionPane.YES_OPTION){
                setResponse2(true);
            }
            else if(response3 == JOptionPane.NO_OPTION||response3 == JOptionPane.CANCEL_OPTION){
                setResponse2(false);
                System.exit(0);
            }
        }
    }


    }
    while(response2 == JOptionPane.YES_OPTION);


}

}

Okay I know that’s a load of code. Sorry, but…
When the dialog shows up that says "X"+operator+"Y=Z\nEnter the "+nameOfFirstNumber+" (the first number.)", if I type 1 (in the next dialog) it says "48.0Y=Z" (if its in division mode.) It doesn’t even show the operator and I didn’t type 48, I typed 1. In other modes it says some number from 40 to 50. What’s going on here? It’s very strange…

Thanks!

[EDIT] Okay it has something to do with ASCII. But how can I fix it?
[EDIT] And why does it not show the operator?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:19:41+00:00Added an answer on June 13, 2026 at 10:19 am

    Wrap your numbers using String.valueOf()method e.g.

    String numberTwo = 
     JOptionPane.showInputDialog(String.valueOf(firstNumber)+operator+"Y=Z\nEnter the "
                           +nameOfSecondNumber+" (the second number.)");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
public class Calculator { public static void main(String[] args) { boolean isValid = false;
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
I defined two classes: class Token_ { public: virtual char operator*()const = 0;//this fnc
My main class displays a JMenuBar. This JMenuBar is managed from calculator.ui.MenuBar. public JMenuBar
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class Knowing { static final long tooth = 343L; static long doIT(long tooth)
I have this code but it doesn't work! public class Trial { public static
public class fraktal extends JFrame { public fraktal (String args[]) { calc = new
please look at this class, static method calls and output. public class OneThreadManyStaticCalls {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.