1

Thanks for having a look at my question. I wanna ask that can i take character or arithmetic operator as input from the user without using scanner?

This is my code to Write a program using the arithmetic operators to perform algebraic operations on two numbers. (Algebraic operation is +, - , *, /, %)

import java.util.Scanner;
class q4
{
    public static void main(String args[])
    {
        Scanner s = new Scanner(System.in );
        int a, b;
        char operator;
        System.out.print("Enter A : ");
        a=s.nextInt();
        System.out.print("Enter B : ");
        b=s.nextInt();
        System.out.print("Enter operator (+, -, *, /)");
        operator = s.next().charAt(0);
        double addition  = a+b;
        double subtraction  = a-b;
        double multiplication  = a*b;
        double division  = a/b;

        switch(operator)
        {
            case '+' :
            {
                System.out.print("Total after Addition is : "+addition);
                break;
            }
            case '-' :
            {
                System.out.print("Total after Subtraction is : " +subtraction);
                break;
            }
            case '*' :
            {
                System.out.print("Total after Multiplication is : "+multiplication);
                break;
            }
            case '/' :
            {
                System.out.print("Total after Division is : "+division);
                break;
            }
            default :
            {
                System.out.print("Please select proper operator");
                return;
            }
        }
    }
}

Thanks for replying me even when you are very busy :)

4
  • 2
    Why don't you want to use Scanner? Commented Jan 13, 2018 at 11:53
  • You can use the System.in field as you like. It's a normal InputStream and it has a method read() to read the next byte from that input stream. But what exactly is the question here? Commented Jan 13, 2018 at 12:03
  • Possible duplicate of How can I get the user input in Java? Commented Jan 13, 2018 at 12:06
  • Algebraic? Anyways...you could use BufferedReader Class and wrap it with the InputStreamReader Class or even the Console Class if you using the Java SE6 or higher (see this SO Post). Or create a GUI. Maybe even stick with the Scanner Class, it works fairly decent. :) Commented Jan 13, 2018 at 12:06

2 Answers 2

0

I'm not really sure why wouldn't you use Scanner in that situation... However, these are some other ways to read users input without using it:

  1. Use JOptionPane (read more about it here)

use:

 a = Integer.parseInt(JOptionPane.showInputDialog("Enter A :"))

instead of:

System.out.print("Enter A : "); 
a=s.nextInt(); 

use this for all you different inputs.

  1. Create a reader variable using System.in and define the variable a using a BufferedReader for obtaining the input:

    InputStreamReader isr = new InputStreamReader(System.in); 
    BufferedReader br = new BufferedReader (isr);
    try {
    a = Integer.parseInt(br.readLine());
    } catch (IOException e) {
    e.printStackTrace();
    }
    

As a side note, you have no need to perform all the operations before knowing which is the one the user wants. Change this:

double addition  = a+b;
double subtraction  = a-b;
double multiplication  = a*b;
double division  = a/b;

switch(operator)
{
    case '+' :
    {
        System.out.print("Total after Addition is : "+addition);
        break;
    }
    case '-' :
    {
        System.out.print("Total after Subtraction is : " +subtraction);
        break;
    }
    case '*' :
    {
        System.out.print("Total after Multiplication is : "+multiplication);
        break;
    }
    case '/' :
    {
        System.out.print("Total after Division is : "+division);
        break;
    }
    default :
    {
        System.out.print("Please select proper operator");
        return;
    }
}

for only this:

switch(operator)
{
    case '+' :
    {
        double addition  = a+b;
        System.out.print("Total after Addition is : "+addition);
        break;
    }
    case '-' :
    {
        double subtraction  = a-b;
        System.out.print("Total after Subtraction is : " +subtraction);
        break;
    }
    case '*' :
    {
        double multiplication  = a*b;
        System.out.print("Total after Multiplication is : "+multiplication);
        break;
    }
    case '/' :
    {
        double division  = a/b;
        System.out.print("Total after Division is : "+division);
        break;
    }
    default :
    {
        System.out.print("Please select proper operator");
        return;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

yes you can by using the concept of command line argument.After you compile your java file and at the time of executing it like java .you can write the input which you want to give beside the class which is taken as a String in String type array.

public static void main(String args[])

the input which you pass while executing your java files is taken into the args[] and store at respective index numbers like args[0] args[1]....args[n]. this is a dynamic array whose size increases depending upon the arguments your passing at command line.

Example: adding numbers

Code

class Addition


{

public static void main(String args[])

{

    int a=Integer.parseInt(args[0]);
    int b=Integer.parseInt(args[1]);

  System.out.println(a+b);
   }
  }

to convert the Strings which i passed in args[0],args[1],args[2] we need to Wrapper classes followed by its methods.

Compiling

javac Addition.java

Execute

java Addition 1 2 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.