If anyone wondered you don't have to use "char" type for operator. You can also use "String". Just put operators with double quotes instead of single ones in your conditions. This way you can simply write: "String op = s.next();" instead of "char op = s.next().charAt(0);"
It will not work because "==" operator checks if two variables refer to same object in memory. It doesn't check the content of string. For this you have to use .equals() method to check the content of two strings is equal or not. Eg. if (op.equals("+")) System.out.println(d1+d2); ....
Bro a doubt when we intialize multiple scanner objects like s1,s2 s3 and use it for methods like s1.nextInt() ,s2.nextInt() Then it is not stopping at spaces bro . Why is this happening
I am finally done with making the calculator with switch statements and also use character and string also here are the two codes Could anyone check if it is right or wrong? 1. Take operator as String and use switch statement(drive.google.com/file/d/1bqoD1KUEN_j6usCww1pudJ4A0eV92qFT/view?usp=sharing) 2. Take operator as Character and use switch statement-(drive.google.com/file/d/1XJzhM0q3iWDSVK3w8YzYh3G7pYTwpkqF/view?usp=sharing)
Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextDouble(Scanner.java:2564) at com.company.Calculator.main(Calculator.java:10) why???
You can't read characters by using the s.next() method, so you have to use the string methods to get the first character from the string. Also, you can't use == in an if function if you are equalling something to a string but you can do it for characters, boolean, and numbers.
there's no nextChar() method in the Scanner class to read a single character in java. that's why we use string, and since charAt() returns the character at a specified index in the string, we put (0) to return the first character.
package General; import java.util.Scanner; public class if_else_exe_one { public static void main(String args[]){ System.out.println("enter num 1 , op , num2. ");
if(op=='+') System.out.println((int)(d1+d2)); else if(op=='-') System.out.println((int)(d1-d2)); else if(op=='*') System.out.println((int)(d1*d2)); else if(op=='/') System.out.println((d1/d2)); } } for me it show some error like input mismatch
import java.util.*; public class Logical { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter num1 operator num2 (example : 1 + 2) "); double num1 = input.nextInt(); char symbol = input.next().charAt(0); double num2 = input.nextInt(); if (symbol == '+') System.out.println("The result is : " + (num1 + num2));
else if (symbol == '-') System.out.println("The result is : " + (num1 - num2)); else if (symbol == '/') System.out.println("The result is : " + (num1 / num2 )); else if (symbol == '*') System.out.println("The result is : " + (num1 * num2));
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner num = new Scanner(System.in); System.out.print("Enter First Number: "); int num1 = num.nextInt(); System.out.print("Choose Operator(+,-,*,/):"); String operator = num.next(); System.out.print("Enter Second Number: "); int num2 = num.nextInt(); if (operator.equals("+")) System.out.println("Addition of "+num1+" and "+num2+" is equal to "+(num1+num2)); else if (operator.equals("-")) System.out.println("Subtration of "+num1+" from "+num2+" is equal to "+(num1-num2)); else if (operator.equals("*")) System.out.println("Multiplication of "+num1+" and "+num2+" is equal to "+(num1*num2)); else if (operator.equals("/")) System.out.println("Division of "+num1+" by "+num2+" is equal to "+(num1/num2)); } }
import java.util.Scanner; class Check { public static void main(String[] args) {
System.out.print("Enter num1 operator num2 (example: 2 * 3): "); Scanner sc = new Scanner(System.in); int a = sc.nextInt(); char op = sc.next().charAt(0); int b = sc.nextInt(); if(op == '+') { System.out.println("The result is: " + (int) (a + b)); } else if(op == '-') { System.out.println("The result is: " + (int) (a - b)); } else if(op == '*') { System.out.println("The result is: " + a * b); } else { System.out.println("The result is: " + (double) a / (double) b); }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("This are the operators (+, -, *, /) "); System.out.print("Enter num1 operator num2 (example: 1 + 2): "); int num1 = input.nextInt(); String operator = input.next(); int num2 = input.nextInt(); if (operator.equals("+")) System.out.println("The result is: " + (num1 + num2)); else if (operator.equals("-")) System.out.println("The result is: " + (num1 - num2)); else if (operator.equals("*")) System.out.println("The result is: " + (num1 * num2)); else if (num2 != 0 ) System.out.println("The result is: " + ((double)num1 / (double)num2)); else System.out.println("Can't divide by zero"); input.close(); } }
If anyone wondered you don't have to use "char" type for operator. You can also use "String". Just put operators with double quotes instead of single ones in your conditions. This way you can simply write:
"String op = s.next();"
instead of
"char op = s.next().charAt(0);"
thanks
It will not work because "==" operator checks if two variables refer to same object in memory. It doesn't check the content of string. For this you have to use .equals() method to check the content of two strings is equal or not.
Eg.
if (op.equals("+"))
System.out.println(d1+d2);
....
Thanks
Please do more exercises
Being in class 12 This video reminds why I need to start working ..
very very nice explained thnkuuu
Wow, you are a machine thinking
Thank you ,Sir
Bro a doubt when we intialize multiple scanner objects like s1,s2 s3 and use it for methods like s1.nextInt() ,s2.nextInt()
Then it is not stopping at spaces bro .
Why is this happening
I am finally done with making the calculator with switch statements and also use character and string also here are the two codes Could anyone check if it is right or wrong?
1. Take operator as String and use switch statement(drive.google.com/file/d/1bqoD1KUEN_j6usCww1pudJ4A0eV92qFT/view?usp=sharing)
2. Take operator as Character and use switch statement-(drive.google.com/file/d/1XJzhM0q3iWDSVK3w8YzYh3G7pYTwpkqF/view?usp=sharing)
THX TOO FOR UR INTERVENTION. BLESS
Can u display output too
Please answer : Why did he write (int) in sout method? If he wanted to convert double into integer why didn't the answer showed in decimal?
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at com.company.Calculator.main(Calculator.java:10)
why???
enter the values using spaces in one line.like 5 + 7
5space+space7
@@shahidaibrahim2917 thanks dude'👍
@@shahidaibrahim2917 was literally going mad
@@shahidaibrahim2917 thank you~
I get an error like inputmismatch exception ,what to do same code I have typed but it shows error
package challenge1;
import java.util.Scanner;
public class Challenge1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter num1 operator num2 (example: 1 + 2): ");
double num1 = s.nextDouble();
char c = s.next().charAt(0);
double num2 = s.nextDouble();
if (c == '+'){
System.out.print("The result is: " +(num1 + num2));
}else if(c == '-'){
System.out.print("The result is: " +(num1 - num2));
}else if(c == '*'){
System.out.print("The result is: " +(num1 * num2));
}else if(c == '/'){
System.out.print("The result is: " +(num1 / num2));
}else{
System.out.print("Error!");
}
can anyone plese explain why we are giving spaces between the numbers i.e exmaple 2_+_3
why not we are entering 2+3
Bro which app do you use for java this is not bluej
done
why can he enter the n1+op+n2 in only one input?
sure! here you can even separate the inputs line by line
// Scanner scan= new Scanner(System.in);
// System.out.println("enter number 1");
// int number1 = scan.nextInt();
// System.out.println("enter number 2");
// int number2 = scan.nextInt();
//
// System.out.println(" Enter your operator of choice + - / * " );
// char operator = scan.next().charAt(0);
if(operator== '+')
{
results=number1+number2;
System.out.println(results);
}
else if (operator == '-')
{
results=(number1-number2);
System.out.println(results);
}
else if(operator== '/')
{
results=(number1/number2);
System.out.println(results);
}
else if(operator== '*'){
results=(number1*number2);
System.out.println(results);
}
else
System.out.println("INVALID OPERATOR : PLEASE TRY AGAIN");
}
}
i think then we cant type cast the values of double when required
Somebody please explain char op = s.next().charAt(0)
Thank you
You can't read characters by using the s.next() method, so you have to use the string methods to get the first character from the string. Also, you can't use == in an if function if you are equalling something to a string but you can do it for characters, boolean, and numbers.
I wanted to know how to add the, “The result is: “ part 😩
Sorry, what is the reason using charAt(0)? Can anyone explain it?
there's no nextChar() method in the Scanner class to read a single character in java. that's why we use string, and since charAt() returns the character at a specified index in the string, we put (0) to return the first character.
package General;
import java.util.Scanner;
public class if_else_exe_one {
public static void main(String args[]){
System.out.println("enter num 1 , op , num2. ");
Scanner sc=new Scanner(System.in);
double d1=sc.nextDouble();
char op=sc.next().charAt(0);
double d2=sc.nextDouble();
if(op=='+')
System.out.println((int)(d1+d2));
else if(op=='-')
System.out.println((int)(d1-d2));
else if(op=='*')
System.out.println((int)(d1*d2));
else if(op=='/')
System.out.println((d1/d2));
}
}
for me it show some error like input mismatch
for me also
Your public static void main method is written wrong. brackets are with the word string not args
import java.util.*;
public class Logical {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter num1 operator num2 (example : 1 + 2) ");
double num1 = input.nextInt();
char symbol = input.next().charAt(0);
double num2 = input.nextInt();
if (symbol == '+')
System.out.println("The result is : " + (num1 + num2));
else if (symbol == '-')
System.out.println("The result is : " + (num1 - num2));
else if (symbol == '/')
System.out.println("The result is : " + (num1 / num2 ));
else if (symbol == '*')
System.out.println("The result is : " + (num1 * num2));
else
System.out.println("invalid input");
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
System.out.print("Enter First Number: ");
int num1 = num.nextInt();
System.out.print("Choose Operator(+,-,*,/):");
String operator = num.next();
System.out.print("Enter Second Number: ");
int num2 = num.nextInt();
if (operator.equals("+"))
System.out.println("Addition of "+num1+" and "+num2+" is equal to "+(num1+num2));
else if (operator.equals("-"))
System.out.println("Subtration of "+num1+" from "+num2+" is equal to "+(num1-num2));
else if (operator.equals("*"))
System.out.println("Multiplication of "+num1+" and "+num2+" is equal to "+(num1*num2));
else if (operator.equals("/"))
System.out.println("Division of "+num1+" by "+num2+" is equal to "+(num1/num2));
}
}
import java.util.Scanner;
class Check {
public static void main(String[] args) {
System.out.print("Enter num1 operator num2 (example: 2 * 3): ");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
char op = sc.next().charAt(0);
int b = sc.nextInt();
if(op == '+') {
System.out.println("The result is: " + (int) (a + b));
}
else if(op == '-') {
System.out.println("The result is: " + (int) (a - b));
}
else if(op == '*') {
System.out.println("The result is: " + a * b);
}
else {
System.out.println("The result is: " + (double) a / (double) b);
}
}
}
im sry
not working
after each entry of the number press enter for example..
2
*
3
6
@@muhammadismail4779 why can he enter the n1+op+n2 in only one input?
@@catrieltorrez2450 if we do so as you say, java interprets it as string concatenation
you guys need to leav space between num1 and operator and num2
@@Rektlele why ?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("This are the operators (+, -, *, /) ");
System.out.print("Enter num1 operator num2 (example: 1 + 2): ");
int num1 = input.nextInt();
String operator = input.next();
int num2 = input.nextInt();
if (operator.equals("+"))
System.out.println("The result is: " + (num1 + num2));
else if (operator.equals("-"))
System.out.println("The result is: " + (num1 - num2));
else if (operator.equals("*"))
System.out.println("The result is: " + (num1 * num2));
else
if (num2 != 0 )
System.out.println("The result is: " + ((double)num1 / (double)num2));
else
System.out.println("Can't divide by zero");
input.close();
}
}