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 9235327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:00:10+00:00 2026-06-18T07:00:10+00:00

I am taking my first Java class, and am having a hard time finishing

  • 0

I am taking my first Java class, and am having a hard time finishing this assignment.

The assignment itself is quite simple. All I have to do is prompt the user to input an integer, a character, then another integer.

If the character inputted is +, the two integers should be added together. If the character inputted is -, the two integers should be subtracted. Etc…

The problem is that I do not know how to accept a character from the user, then use it in my If/Switch statements.

Here are my prompts:

// Prompt the user to enter an integer
    System.out.print("Please enter an integer number:");
    int int1  = input.nextInt();

// Prompt the user to enter a character
// Since I could not use characters, I am using integers to represent them.
    System.out.print("Enter 0 for +, 1 for -, 2 for /, 3 for %, and 4 for *");
    int char1 = input.nextInt();

// Prompt the user to enter another integer
    System.out.print("Please enter another integer number:");
    int int2 = input.nextInt();

Here are my If/Switch statements: (I know they are redundant, but it’s part of the assignment)

if (char1 == 0) {
    int ans = int1 + int2;
    System.out.println(int1 + " + " + int2 + " = " + ans);
} else if (char1 == 1) {
    int ans = int1 - int2;
    System.out.println(int1 + " - " + int2 + " = " + ans);
} else if (char1 == 2) {
    int ans = int1 / int2;
    System.out.println(int1 + " / " + int2 + " = " + ans);
} else if (char1 == 3) {
    int ans = int1 % int2;
    System.out.println(int1 + " % " + int2 + " = " + ans);
} else if (char1 == 4) {
    int ans = int1 * int2;
    System.out.println(int1 + " * " + int2 + " = " + ans);
} else
    System.out.println("Invalid operator");

int result = 0;
switch (char1) {
    case 0: result = int1 + int2;
            System.out.println(int1 + " + " + int2 + " = " + result); break;
    case 1: result = int1 - int2;
            System.out.println(int1 + " - " + int2 + " = " + result); break;
    case 2: result = int1 / int2;
            System.out.println(int1 + " / " + int2 + " = " + result); break;
    case 3: result = int1 % int2;
            System.out.println(int1 + " % " + int2 + " = " + result); break;
    case 4: result = int1 * int2;
            System.out.println(int1 + " * " + int2 + " = " + result); break;
    default: System.out.println("Invalid operator");
}

Everything else works just fine. I’m just trying to change from using 0 for +, 1 for -, to just using +, -, /, %, and *.

  • 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-18T07:00:12+00:00Added an answer on June 18, 2026 at 7:00 am

    you can take the operator from the user as String or Char

    Like this :

     Scanner input = new Scanner(System.in);
            // Prompt the user to enter an integer
            System.out.print("Please enter an integer number:");
            int int1 = input.nextInt();
    
    // Prompt the user to enter a character
    // Since I could not use characters, I am using integers to represent them.
            System.out.print("Enter +, -,  /,  %, *");
            char char1 = input.next().charAt(0);
    
    // Prompt the user to enter another integer
            System.out.print("Please enter another integer number:");
            int int2 = input.nextInt();
    
            if (char1=='+') {
                int ans = int1 + int2;
                System.out.println(int1 + " + " + int2 + " = " + ans);
            } else if (char1=='-') {
                int ans = int1 - int2;
                System.out.println(int1 + " - " + int2 + " = " + ans);
            } else if (char1=='/') {
                int ans = int1 / int2;
                System.out.println(int1 + " / " + int2 + " = " + ans);
            } else if (char1=='%') {
                int ans = int1 % int2;
                System.out.println(int1 + " % " + int2 + " = " + ans);
            } else if (char1=='*') {
                int ans = int1 * int2;
                System.out.println(int1 + " * " + int2 + " = " + ans);
            } else {
                System.out.println("Invalid operator");
            }
    
            int result = 0;
    
            switch (char1) {
                case '+':
                    result = int1 + int2;
                    System.out.println(int1 + " + " + int2 + " = " + result);
                    break;
                case '-':
                    result = int1 - int2;
                    System.out.println(int1 + " - " + int2 + " = " + result);
                    break;
                case '/':
                    result = int1 / int2;
                    System.out.println(int1 + " / " + int2 + " = " + result);
                    break;
                case '%':
                    result = int1 % int2;
                    System.out.println(int1 + " % " + int2 + " = " + result);
                    break;
                case '*':
                    result = int1 * int2;
                    System.out.println(int1 + " * " + int2 + " = " + result);
                    break;
                default:
                    System.out.println("Invalid operator");
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all thanks for taking the time to look into this. I store
First, thanks in advance for taking the time to read through this. I have
I am taking my first steps in C++ having a good background in Java.
first of all thanks for taking your time! I'm a junior Dev, working with
JAVA: First off, Thanks so much for taking the time to look at my
I am taking my first OOP class (learning Java) in the fall so I've
I am taking a class in C++ and have been given my first project.
Obviously, Java does not have delegates or functions as first class values and uses
First of all, thanks for taking the time to read my question, I appreciate
First off, let me thank you if you're taking the time to look at

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.