Java Calculator char thread - java

This is my void main in my code. When I compile the code it shows no errors, But when I type any random letters then it shows the following thread.
Output:
Please enter the equation :
2323.10ffxcv
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at Calculator.main(calculator.java:31)
[This is the output in CMD.][1]
Code:
Calculator calc = new Calculator();
Scanner numbers = new Scanner(System.in);
System.out.println("Please enter the equation : ");
double a = numbers.nextDouble();
char sign = numbers.next().charAt(0);
double b = numbers.nextDouble();
switch (sign) {
case '+':
calc.add(a, b);
break;
case '-':
calc.sub(a, b);
break;
case '*':
calc.multiply(a, b);
break;
case '/':
calc.divide(a, b);
break;
default:
System.out.println("Sorry I ( The program ) did not understand");
while (sign != '+' || sign != '-' || sign != '*' || sign != '/') {
switch (sign) {
case '+':
calc.add(a, b);
break;
case '-':
calc.sub(a, b);
break;
case '*':
calc.multiply(a, b);
break;
case '/':
calc.divide(a, b);
break;
default:
System.out.println("Sorry I ( The program ) did not understand");
break;
}
}
break;
}
Please help!

The Scanner class won't tokenize your input the way you seem to assume. That is, numbers.nextDouble() doesn't read up until the end of the first bit of data that looks like a Double. It reads the entire line, and attempts to parse it as a Double. You get an InputMismatchException because the input (2323.10ffxcv) doesn't match the expected data type (Double).
You need to find a way to either tokenize the string yourself, or simply enter each value on its own line. Even so, the last part of your input (ffxcv) is still going to break things, because it can't be parsed as a Double.

Related

How to comment string starting with "//" this?

I am learning compiler construction these days, and I am having trouble while making the code for comment in it.
What is actually happening is that when ever I am writing a string in the notepad file such as Hello //World. Then it is printing "/" this div operator which I don't want. What actually I want is that Hello should be printed in the output and World should get commented. I know I have included the code for div operator but it is also necessary to include. Just wanted to know how I can achieve this comment logic while checking the logic for checking the div operator should also be there.
Here is the code!
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class main {
public static void main(String[] args) throws FileNotFoundException{
File newFile = new File("C:/temp/sourcecode.txt");
Scanner scanFile = new Scanner(newFile);
//Scanner scan = new Scanner(System.in);
char ch;
String str;
while(scanFile.hasNextLine()){
str = scanFile.nextLine();
int l = str.length();
if(!str.startsWith("//") && !str.startsWith("/*") && !str.endsWith("*/")) {
for(int i =0; i<l ; i++) {
ch = str.charAt(i);
System.out.println(ch);
if(ch == '*'){
System.out.println("The Operator is MUL");
System.out.println("arop\n");
}
if(ch == '/')
{
System.out.println("The Operator is DIV");
System.out.println("arop\n");
}
}
}
int OP = 0;
switch(OP){
case 0:
if(str.contains("<") && str.contains(">")){
System.out.println("The Operator is NE");
System.out.println("relop\n");
break;
}
case 1:
if(str.contains("<") && str.contains("=")){
System.out.println("The Operator is LE");
System.out.println("relop\n");
break;
}
case 2:
if(str.contains(">") && str.contains("=")){
System.out.println("The Operator is GE");
System.out.println("relop\n");
break;
}
case 3:
if(str.contains("<")){
System.out.println("The Operator is LT");
System.out.println("relop\n");
break;
}
case 4:
if(str.contains(">")){
System.out.println("The Operator is GT");
System.out.println("relop\n");
break;
}
case 5:
if(str.contains("==")){
System.out.println("The Operator is EQ");
System.out.println("relop\n");
break;
}
case 6:
if(str.contains("+")){
System.out.println("The Operator is ADD");
System.out.println("arop\n");
break;
}
case 7:
if(str.contains("-")){
System.out.println("The Operator is SUB");
System.out.println("arop\n");
break;
}
// case 8:
// if(str.contains("*")){
// System.out.println("The Operator is MUL");
// System.out.println("arop\n");
// break;
// }
// case 9:
// if(str.contains("/")){
// System.out.println("The Operator is DIV");
// System.out.println("arop\n");
// break;
// }
case 10:
if(str.contains("=")){
System.out.println("The Operator is ASN");
System.out.println("otop\n");
break;
}
case 11:
if(str.contains("'")){
System.out.println("The Operator is PRN");
System.out.println("otop\n");
break;
}
case 12:
if(str.contains(";")){
System.out.println("The Operator is LTRN");
System.out.println("otop\n");
break;
}
case 13:
if(str.contains("{")){
System.out.println("The Operator is LBRC");
System.out.println("otop\n");
break;
}
case 14:
if(str.contains("}")){
System.out.println("The Operator is RBRC");
System.out.println("otop\n");
break;
}
}
}
}
}
Thank you in advance!
When programming a compiler the different input words in your code are called tokens, and the phase of recognising the role of each token is called the lexical analysis phase.
When trying to recognise tokens usually what is used is regex which is a way of implementing a finite automata.
You can read about it in much more detail here:
https://en.wikipedia.org/wiki/Lexical_analysis
You should replace the usage of contains, and use a lexer, it's the name of the tool that does lexical analysis. It uses regexes because it's not just about / and //, there can be many different situations where your compiler will need to decide which token to choose.
Here's an example of a finite automate for recognising different tokens, notice that for each prefix there can be many options for possible tokens:
In Java you can use jflex which will generate lexer code with your tokens definitions.
When you find a /, you need to check the next character.
if (ch == '/') {
char nextCh = (i + 1 < l ? str.charAt(i + 1) : '\0');
if (nextCh == '/') {
System.out.println("The Operator is EndOfLineComment");
System.out.println("arop\n");
i++;
} else if (nextCh == '*') {
System.out.println("The Operator is TraditionalComment");
System.out.println("arop\n");
i++;
} else {
System.out.println("The Operator is DIV");
System.out.println("arop\n");
}
}

why cant i convert String to char & use in Switch Statement directly?

why cant i convert String to char & use in Switch Statement & if i left it as string the switch statement wont accept it either telling me it needs to be int or byte or short !!
public class Main {
public static void main(String[] args) {
String var1=getInput("enter first variable");
String var2=getInput("enter second variable");
String var3=getInput("enter opertaor");
char c = var3.charAt(0);
double d1=Double.parseDouble(var1);
double d2=Double.parseDouble(var2);
switch(c){
case "+"://squiggly line appears & the bubble help says incompatible types
System.out.println(d1+d2);
break;
case "-"://squiggly line appears & the bubble help says incompatible
System.out.println(d1-d2);
break;
case "*"://squiggly line appears & the bubble help says incompatible
System.out.println(d1*d2);
break;
case "/"://squiggly line appears & the bubble help says incompatible
System.out.println(d1/d2);
break;
default:
System.out.println("Unrecognized operation");
break;
}
}
static String getInput(String prompt){
System.out.println("prompt");
Scanner sc=new Scanner(System.in);
return sc.nextLine();
}
}
You can use a String in case expressions, no need for a char. Change c like
String c = var3.substring(0, 1);
and your code would work. Alternatively, modify your case statements to use char. Like,
switch (c) {
case '+':
System.out.println(d1 + d2);
break;
case '-':
System.out.println(d1 - d2);
break;
case '*':
System.out.println(d1 * d2);
break;
case '/':
System.out.println(d1 / d2);
break;
default:
System.out.println("Unrecognized operation");
break;
}
You can use a char literal instead:
switch(c){
case '+':
System.out.println(d1+d2);
break;
...
The types are different so you can't directly compare them. They happen to be convertible in this particular case, but that's not true in general, so the compiler can't allow that.
Try out below code and it will execute
public static void main(String[] args) {
String var1 = getInput("enter first variable");
String var2 = getInput("enter second variable");
String var3 = getInput("enter opertaor");
char c = var3.charAt(0);
double d1 = Double.parseDouble(var1);
double d2 = Double.parseDouble(var2);
switch (c) {
case '+':
System.out.println(d1 + d2);
break;
case '-':
System.out.println(d1 - d2);
break;
case '*':
System.out.println(d1 * d2);
break;
case '/':
System.out.println(d1 / d2);
break;
default:
System.out.println("Unrecognized operation");
break;
}
}
static String getInput(String prompt) {
System.out.println("prompt");
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
Change case '+': instead of comparing String case "+": Also Check your Java version, From JDK v1.7 will allow you to use Strings in switch statement as what you did in your code snippet. Let me know if you are looking for another solution
You cant parse a String into a char because a String is to big to fit.
Think of it like a String consist of multiple chars, so its just like a char array in one piece.

switch with character, how to convert character to work with switch ?

when i try to print out the result with switch i can't get any result
so i thought to convert char to integer so the switch statement gona work but isn't so any ideas about how to find solution
echar guestGuess = input.next().charAt(0);
int x = (int)guestGuess;
switch(x){
case '1':
System.out.println(answer.isfirstGuessRight(guestGuess) + "\n");
break;
case '2:
'System.out.println("We are kontrol your answer" + answer.issecandGuessRight(guestGuess));
There's not problem in Java to use char for switch.
You don't have to cast to int for that.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter something, and I'll take the first char only");
char c = scan.next().trim().charAt(0);
switch (c) {
case '1':
System.out.println("1 for sure");
break;
case '2':
System.out.println("I think it's 2");
break;
default:
System.out.println("I don't know");
}
}

When I run my program i get the error java.lang.StringIndexOutOfBoundsException: String index out of range: 0

here is my code
import java.io.*;
import java.util.*;
public class weightOnOtherPlanets {
public static void main(String[] args) {
Scanner kbReader = new Scanner(System.in);
System.out.println("Enter your weight");
double weight = kbReader.nextDouble();
System.out.println("choose a planet by entering the corresponging letter\n");
System.out.println("A. Voltar");
System.out.println("B. Krypton");
System.out.println("C. Fertos");
System.out.println("D Servantos");
String choice = kbReader.nextLine( );
char p = choice.charAt(0);
String answerPhrase = "Your weight is " + " " ;
switch(p){
case 'A':
case 'a':
System.out.println(answerPhrase +(.091*weight));
break;
case 'B':
case 'b':
System.out.println(answerPhrase + (.720*weight));
break;
case 'C':
case 'c':
System.out.println(answerPhrase + (.865*weight));
break;
case 'D':
case 'd':
System.out.println(answerPhrase + (4.612*weight));
break;
default:
System.out.println("Please enter either A,B,C,or D");
break;
}
}
}
I have used almost the exact same code for another similar practice project and it worked just fine. When i run the program it goes to the point where it asks for a weight input, then it displays the choice list, but with the error message exception in "main":
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:686)
at weightOnOtherPlanets.main(weightOnOtherPlanets.java:14)
I don't know why it gives this error before allowing keyboard input for String choice.
This is because of the way Scanner scans lines.
After you enter a double (the weight), you press Return. This tells System.in to take all the characters you entered and pass them to the scanner. The scanner then reads the part that interests it - the double number - but leaves everything else waiting for the next operation.
This means the Return you pressed - the end-of-line - is still there. Now, the next thing is nextLine(). The scanner sees it, and it reads all the characters it has until it finds an end-of-line. But as we said, the end-of-line is right there. So it reads that, and gives you all the characters it found before it. Which is none at all, because there were no other characters between the double number and the end-of-line.
This means you get an empty string. And an empty string doesn't have a character at position 0, because that would mean it was not empty.
So what you should do is, after receiving the double, you should add a kbReader.nextLine(); - just like that, without putting the value anywhere. This will skip the end-of-line you entered for the double, and then you'll get the next line properly.
When you do your menu reading, though, you should be checking that the string is not empty before you call charAt(0). After all, the user can decide to press Return rather than make a valid choice. So your system should either ignore that or tell him that it's not a legal input, rather than fail with an exception.
You call nextDouble(); and after that you call nextLine() to get your answer phrase. But that call to nextLine(); will only consume the rest of the line on which you entered your double and it will be empty, therefore choice.charAt(0); will throw an exception.
Try doing something like this to consume the rest of the line and then call the nextLine() to get the answer phrase.
System.out.println("Enter your weight");
double weight = kbReader.nextDouble();
kbReader.nextLine(); // Consume the rest of the line
// ...
String choice = kbReader.nextLine(); // Get the actual input
char p = choice.charAt(0);
try this:
public static void main(String[] args) {
Scanner kbReader = new Scanner(System.in);
System.out.println("Enter your weight");
double weight = kbReader.nextDouble();
System.out.println("choose a planet by entering the corresponging letter\n");
System.out.println("A. Voltar");
System.out.println("B. Krypton");
System.out.println("C. Fertos");
System.out.println("D Servantos");
String choice = kbReader.nextLine();
if (choice.isEmpty()) {
choice = kbReader.nextLine();
}
char p = choice.charAt(0);
String answerPhrase = "Your weight is " + " ";
switch (p) {
case 'A':
case 'a':
System.out.println(answerPhrase + (.091 * weight));
break;
case 'B':
case 'b':
System.out.println(answerPhrase + (.720 * weight));
break;
case 'C':
case 'c':
System.out.println(answerPhrase + (.865 * weight));
break;
case 'D':
case 'd':
System.out.println(answerPhrase + (4.612 * weight));
break;
default:
System.out.println("Please enter either A,B,C,or D");
break;
}
}
You need to read for input differently. That's why you are throwing an exception.
char p = choice.charAt(0); // why not just do a string comparison instead?
is where the error comes up. This is because choice is null / has no charAt(0).
Irregardless I would use something like this instead
char p = ''
while(in.hasNext()) {
String input = in.nextLine();
if (p.length()>0){
p = choice.charAt(0);
}
//do whatever you wanted to with p
This should give you the behavior you are looking for.
Don't forget to consider changing the double input to work roughly the same though.

Java Switch Statement: creating a calculator error

I was wondering if anyone can see what is wrong with my code. It works except that the program is not acknowledging my switch statement - I searched lots of questions but as I am a novice I am clearly missing something.
import java.util.Scanner;
class Calmlr1 {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
String anotherOption = "y", operatorOpt= "a";
int no1=0, no2=0;
double result= 0;
System.out.println ("Welcome to the online calculator! Let's begin...");
while (anotherOption.equalsIgnoreCase ("y")) {
System.out.println ("Please enter your 1st number: ");
no1 = input.nextInt();
System.out.println ("Please confirm your operator:\n1 = +\n2 = - \n3 = *\n4 = /");
operatorOpt = input.next ();
System.out.println ("Please enter your 2nd number: ");
no2 = input.nextInt();
switch(no1) {
case 1:
result=no1+no2;
break;
case 2:
result=no1-no2;
break;
case 3:
result=no1*no2;
break;
case 4:
result=no1/no2;
default:
result = 0 ;
break;
}
System.out.println("Your total calculation is: "+result);
System.out.println("Would you like to do another sum? y/n ");
anotherOption=input.next();
}
}
}
You should be using switch(operatorOpt). Right now you are switching on the first number.
You also need to change:
int operatorOpt= 0;
operatorOpt = input.nextInt();
That is, if you want to keep your switch statement the same. Please also see #Daniel Imms answer for an additional bug fix.
Try adding a break at the end of case 4
case 4:
result=no1/no2;
break;
EDIT J L's answer is the main issue, but this is another problem that will break division.
Your switch should be on the operatorOpt and not on no1.
Also, you're missing a break in the case 4. So, if you want to do a division, you'll get 0 as result.
The input from the user for operatorOpt should be done with input.nextLine(). Or, if you want to keep the same switch statement, with input.nextInt().
It should be like this:
switch(operatorOpt)
{
case "+":
result=no1+no2;
break;
case "-":
result=no1-no2;
break;
case "*":
result=no1*no2;
break;
case "/":
result=no1/no2;
break;
default:
result = 0 ;
break;
}
Your switch statement should be on "operatorOpt" and not on "no1" as you suppose to check the operator and based on that you want to do the calculation. However, you must use JDK1.7 to use String in Switch statement since previous versions of JDK do not support String Switch.
Also, you should use "break" in case 4.
Your switch should be on the operatorOpt and not on no1.
You can use like this
switch(operatorOpt)
{
case "+":
result=no1+no2;
break;
case "-":
result=no1-no2;
break;
case "*":
result=no1*no2;
break;
case "/":
result=no1/no2;
break;
default:
result = 0 ;
break;
}

Categories