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 :)

Abhishek Diwakar
  • 466
  • 1
  • 6
  • 18
  • 1
    Why don't you want to use Scanner? – Emre Jan 13 '18 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? – Progman Jan 13 '18 at 12:03
  • Possible duplicate of [How can I get the user input in Java?](https://stackoverflow.com/questions/5287538/how-can-i-get-the-user-input-in-java) – Bernhard Barker Jan 13 '18 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](https://stackoverflow.com/questions/16122256/getting-input-from-user-in-console-without-using-scanner)). Or create a GUI. Maybe even stick with the Scanner Class, it works fairly decent. :) – DevilsHnd - 退職した Jan 13 '18 at 12:06

3 Answers3

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;
    }
}
Alex Cuadrón
  • 638
  • 12
  • 19
0

I used character data type to store the input operators cause operators are characters. So using sc.next().charAt(0) will take the input of operators.

import java.util.Scanner;

public class Q04 {
    public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        System.out.println("enter first number : ");
        int n1 = sc.nextInt();
        System.out.println("enter any of the (+,-,*,/)  operators : ");
        **char operator = sc.next().charAt(0);**
        System.out.println("enter second number : ");
        double n2 = sc.nextInt();
        double sum =  n1 + n2;
        double mul = n1 * n2 ;
        double sub = n1-n2;
        double divide = n1/n2;
        if(operator =='+'){
            System.out.println(" sum is : " + sum);
        }
        else if(operator=='-'){
            System.out.println("difference is : " + sub);
        }
        else if(operator=='*'){
            System.out.println("multiplication is : " + mul);
        }
        else if(operator=='/'){
            System.out.println("division is : " + divide);
            
        }

    }
}

    Blockquote
-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
Sai Kiran
  • 74
  • 5