Showing posts with label How to use Scanner in Java. Show all posts
Showing posts with label How to use Scanner in Java. Show all posts

Monday, October 5, 2015

Java Program to calculating value using Scanner in Java

Using this Program we can calculate given value of a&b using scanner. This program will help to build the calculator. So read carefully and implement this program.


package com.indra;

import java.util.Scanner;

public class SystemUsingScanner {
static int a;
static int b;
static int c;

public static void main(String[] hh) {
Scanner obj = new Scanner(System.in);
for (int i = 0; i >= 0; i++) {
System.out.println("1 Addition");
System.out.println("2 Substraction");
System.out.println("3 Multiplication");
System.out.println("4 Division");
System.out.println("5 for close your calculator");
int op = obj.nextInt();
boolean close = false;///// use to check case 5 for close this program
switch (op) {

case 1:
System.out.println("Enter the value of a & b for Addition");
a = obj.nextInt();
b = obj.nextInt();
c = a + b;
System.out.println(c);
break;
case 2:
System.out.println("Enter the value of a&b for multiplication");
a = obj.nextInt();
b = obj.nextInt();
c = a - b;
System.out.println(c);
break;
case 3:
System.out.println("Enter the value of a&b for multiplication");
a = obj.nextInt();
b = obj.nextInt();
c = a * b;
System.out.println(c);
break;
case 4:
System.out.println("Enter the value of a&b for multiplication");
a = obj.nextInt();
b = obj.nextInt();
c = a / b;
System.out.println(c);
break;
case 5:
close = true;
break;
default:
break;
}
if (close) {        /// condition true then program will close
break;
}
}
}
}

Friday, October 2, 2015

Make a Calculator Using Scanner

The java.util.Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions.Following are the important points about Scanner:
1-A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
2-A scanning operation may block waiting for input.
3-A Scanner is not safe for multithreaded use without external synchronization.

package com.indra;

import java.util.Scanner;

public class TangentProgram {

public static void main(String[] hh) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i >= 0; i++) {
System.out.println("1 for sin value\n2 for cos value\n3 for tan value\n4 for cot value\n5 for sec value\n6 for cosec value\n7 for square\n8 for square root\n9 1/square root");
int operator = sc.nextInt();
System.out.println("Enter the value of theta");
double theta = sc.nextDouble();

switch (operator) {
case 1:
System.out.println("Answer is - "
+ Math.sin(Math.toRadians(theta)));
break;
case 2:
System.out.println("Answer is - "
+ Math.cos(Math.toRadians(theta)));
break;
case 3:
System.out.println("Answer is - "
+ Math.tan(Math.toRadians(theta)));
break;
case 4:
System.out.println("Answer is - "
+ (1 / Math.tan(Math.toRadians(theta))));
break;
case 5:
System.out.println("Answer is - "
+ (1 / Math.cos(Math.toRadians(theta))));
break;
case 6:
System.out.println("Answer is - "
+ (1 / Math.sin(Math.toRadians(theta))));
break;
case 7:
System.out.println("Answer is - " + theta * theta);
break;
case 8:
System.out.println("Answer is - " + theta * theta * theta);
break;
case 9:
System.out.println("Answer is - " + (Math.sqrt(theta)));
break;
case 10:
System.out.println("Answer is - " + (Math.cbrt(theta)));
break;
default:
break;
}
}
}
}
Tricks and Tips