18 errors in this OMG
Re: 18 errors in this OMG
I don't understand whether you need further help or not. can you explain a bit more what is required at this stage.
Re: 18 errors in this OMG
Code: Select all
import java.util.Scanner;
/** made by gayan bandara.
date 07.08.2010
to make a calc*/
class Gayan_calculator{
public static void list(){
System.out.println("press + for Add numbers");
System.out.println("press - for Substract numbers");
System.out.println("press * for Multily numbers");
System.out.println("press / for Divide numbers");
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int selection = 0;
list();
System.out.println("\npress option from the list");
System.out.println("--------------------------");
selection = input.nextInt();
System.out.println("--------------------------");
if(selection == '+' || selection == '-' || selection == '*' || selection == '/'){
System.out.println("Please enter your first number.");
int num1 = input.nextInt();
System.out.println("Please enter your second number.");
int num2 = input.nextInt();
System.out.println("--------------------------");
if(selection == '+'){
int x = num1 + num2 ;
System.out.println(num1+" + "+num2+"="+x);
System.out.println("-----------------------------------");
}
if(selection == '-'){
int x = num1 - num2 ;
System.out.println(num1+" - "+num2+"="+x);
System.out.println("-----------------------");
}
if(selection == '*'){
int x = num1 * num2 ;
System.out.println(num1+" * "+num2+"="+x);
System.out.println("-----------------------");
}
if(selection == '/'){
int x = num1 / num2 ;
System.out.println(num1+" / "+num2+"="+x);
System.out.println("-----------------------");
}
}
else{
System.out.println("Can't recognize your option");
}
}
}
When I run the program I got this step correctly...
Code: Select all
>java -cp . Gayan_calculator
press + for Add numbers
press - for Substract numbers
press * for Multily numbers
press / for Divide numbers
press option from the list
--------------------------
this is the error...
Code: Select all
>java -cp . Gayan_calculator
press + for Add numbers
press - for Substract numbers
press * for Multily numbers
press / for Divide numbers
press option from the list
--------------------------
+
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Gayan_calculator.main(Gayan_calculator.java:25)
>Exit code: 1
Re: 18 errors in this OMG
with input.nextInt(), the scanner is waiting for a integer value. But you enter an character (char ) . It is the cause for the inputMismatchException.
There is no method for reading next character value. But there is a method named "next()". I think you will be able to use that and some casting ( conversion of data types) to read a character value. I will post my solution soon, if you like to see. I need to install JDK.
There is no method for reading next character value. But there is a method named "next()". I think you will be able to use that and some casting ( conversion of data types) to read a character value. I will post my solution soon, if you like to see. I need to install JDK.
Re: 18 errors in this OMG
Code: Select all
import java.util.Scanner;
/** made by gayan bandara.
date 07.08.2010
to make a calc*/
public class Gayan_calculator{
static void list(){
System.out.println("pre+ss + for Add numbers");
System.out.println("press - for Substract numbers");
System.out.println("press * for Multily numbers");
System.out.println("press / for Divide numbers");
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
char selection = 0;
list();
System.out.println("\npress option from the list");
System.out.println("--------------------------");
selection = input.next().toCharArray()[0];
System.out.println("--------------------------");
if(selection == '+' || selection == '-' || selection == '*' || selection == '/'){
System.out.println("Please enter your first number.");
int num1 = input.nextInt();
System.out.println("Please enter your second number.");
int num2 = input.nextInt();
System.out.println("--------------------------");
if(selection == '+'){
int x = num1 + num2 ;
System.out.println(num1+" + "+num2+"="+x);
System.out.println("-----------------------------------");
}
if(selection == '-'){
int x = num1 - num2 ;
System.out.println(num1+" - "+num2+"="+x);
System.out.println("-----------------------");
}
if(selection == '*'){
int x = num1 * num2 ;
System.out.println(num1+" * "+num2+"="+x);
System.out.println("-----------------------");
}
if(selection == '/'){
int x = num1 / num2 ;
System.out.println(num1+" / "+num2+"="+x);
System.out.println("-----------------------");
}
}
else{
System.out.println("Can't recognize your option");
}
}
}
I have change the type of the variable "selection" to char type.
And reading from scanner,
selection=input.next().toCharArray()[0];
Re: 18 errors in this OMG
It is great to see Herath is on it now. I too don't have JDK installed with me.
Keep up the good work!!!
Suggestion: It would be good to replace the inner "if" statements with a switch/case block
Keep up the good work!!!
Suggestion: It would be good to replace the inner "if" statements with a switch/case block
Code: Select all
switch (selection){
case '+':
int x = num1 + num2 ;
System.out.println(num1+" + "+num2+"="+x);
System.out.println("-----------------------------------");
break;
case '-':
int x = num1 - num2 ;
System.out.println(num1+" - "+num2+"="+x);
System.out.println("-----------------------------------");
break;
.....................
.....................
.....................
default:
System.out.println("Can't recognize your option");
break;
}
Re: 18 errors in this OMG
yeah bro..In this program I first start like that...
can you please correct this for me...
see my first post...it contain ints..but is had error when I use + - / * for my options in my program...G-sparkZ wrote:I found 18 errors when I compile this in a java editor.these are the errors I foundCode: Select all
import java.util.Scanner; /** made by gayan bandara. date 07.08.2010 to make a calc*/ class Gayan_calculator{ public static void list(){ System.out.println("press + for Add numbers"); System.out.println("press - for Substract numbers"); System.out.println("press * for Multily numbers"); System.out.println("press / for Divide numbers"); } public static void main(){ Scanner input=new Scanner(System.in); int selection=0; list(); System.out.println("\npress option from the list"); System.out.println("--------------------------"); selection = input.nextint(); System.out.println("--------------------------"); if(selection==+||selection==-||selection==*||selection==\){ System.out.println("Please enter your first number."); int num1 = input.nextInt(); System.out.println("Please enter your second number."); int num2 = input.nextInt(); System.out.println("--------------------------"); if(selection==+) { int x = num1 + num2 ; System.out.println(num1+" + "+num2+"="+x); System.out.println("-----------------------------------"); } if(selection==-) { int x = num1 - num2 ; System.out.println(num1+" - "+num2+"="+x); System.out.println("-----------------------"); } if(selection==*) { int x = num1 * num2 ; System.out.println(num1+" * "+num2+"="+x); System.out.println("-----------------------"); } if(selection==/) { int x = num1 / num2 ; System.out.println(num1+" / "+num2+"="+x); System.out.println("-----------------------"); } }else{ System.out.println("Can't recognize your option"); } } } }
I think that My selection has the main problem.can't I use + - * / as options to select..?can you please help me on this BIG problem....This is the biggest code I wrote in my life...Code: Select all
>javac Gayan_calculator.java Gayan_calculator.java:29: illegal start of expression if(selection==+||selection==-||selection==*||selection==\){ ^ Gayan_calculator.java:29: illegal start of expression if(selection==+||selection==-||selection==*||selection==\){ ^ Gayan_calculator.java:29: illegal start of expression if(selection==+||selection==-||selection==*||selection==\){ ^ Gayan_calculator.java:29: illegal start of expression if(selection==+||selection==-||selection==*||selection==\){ ^ Gayan_calculator.java:29: illegal character: \92 if(selection==+||selection==-||selection==*||selection==\){ ^ Gayan_calculator.java:29: illegal start of expression if(selection==+||selection==-||selection==*||selection==\){ ^ Gayan_calculator.java:29: ';' expected if(selection==+||selection==-||selection==*||selection==\){ ^ Gayan_calculator.java:37: illegal start of expression if(selection==+) ^ Gayan_calculator.java:44: illegal start of expression if(selection==-) ^ Gayan_calculator.java:51: illegal start of expression if(selection==*) ^ Gayan_calculator.java:51: illegal start of expression if(selection==*) ^ Gayan_calculator.java:57: illegal start of expression if(selection==/) ^ Gayan_calculator.java:57: illegal start of expression if(selection==/) ^ Gayan_calculator.java:65: illegal start of type }else{ ^ Gayan_calculator.java:65: ';' expected }else{ ^ Gayan_calculator.java:66: <identifier> expected System.out.println("Can't recognize your option"); ^ Gayan_calculator.java:66: illegal start of type System.out.println("Can't recognize your option"); ^ Gayan_calculator.java:69: class, interface, or enum expected } ^ 18 errors >Exit code: 1
can you please correct this for me...
Re: 18 errors in this OMG
Well, when you use "if" there should be a condition to check. The code inside the "if" block will only be evaluated only if the condition of the "if" block is true. I think you are aware of it.
Let's see your "if" block.
selection==+
This is a compilation error. You cannot compile. The LHS(Left hand side) of the expression is integer. But you have "+" on the right hand side. In (almost, i guess) every language + is a reserved operator. Here it basically denotes the mathematical addition. And '+' is a character since it has single quotes around it. The character form is what you should use. So the selection also should be character type(It can be of String type too) in order to carry out a comparison which would be correctly evaluated and give a boolean result.
This is going to be a long post if I explain all of it. Ask away the section you don't have much understanding. I am also not a professional in java. But I can be of help to a beginner.
Let's see your "if" block.
Code: Select all
if(selection==+||selection==-||selection==*||selection==\)
This is a compilation error. You cannot compile. The LHS(Left hand side) of the expression is integer. But you have "+" on the right hand side. In (almost, i guess) every language + is a reserved operator. Here it basically denotes the mathematical addition. And '+' is a character since it has single quotes around it. The character form is what you should use. So the selection also should be character type(It can be of String type too) in order to carry out a comparison which would be correctly evaluated and give a boolean result.
This is going to be a long post if I explain all of it. Ask away the section you don't have much understanding. I am also not a professional in java. But I can be of help to a beginner.
Re: 18 errors in this OMG
Code: Select all
import java.util.Scanner;
/** made by gayan bandara.
date 07.08.2010
to make a calc*/
public class Gayan_calculator{
static void list(){
System.out.println("1.Add numbers");
System.out.println("2.Substract numbers");
System.out.println("3.Multily numbers");
System.out.println("4.Divide numbers");
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int selection=0;
list();
System.out.println("\npress option from the list");
System.out.println("--------------------------");
selection = input.nextInt();
System.out.println("--------------------------");
if(selection==1||selection==2||selection==3||selection==4 ){
System.out.println("Please enter your first number.");
int num1 = input.nextInt();
System.out.println("Please enter your second number.");
int num2 = input.nextInt();
System.out.println("--------------------------");
if(selection==1)
{
int x = num1 + num2 ;
System.out.println(num1+" + "+num2+"="+x);
System.out.println("-----------------------------------");
}
if(selection==2)
{
int x = num1 - num2 ;
System.out.println(num1+" - "+num2+"="+x);
System.out.println("-----------------------");
}
if(selection==3)
{
int x = num1 * num2 ;
System.out.println(num1+" * "+num2+"="+x);
System.out.println("-----------------------");
}
if(selection==4)
{
int x = num1 / num2 ;
System.out.println(num1+" / "+num2+"="+x);
System.out.println("-----------------------");
}
}else{
System.out.println("Can't recognize your option");
}
}
}
Re: 18 errors in this OMG
Logically correct and simple. See https://robot.lk/viewtopic.php?f=11&t=1700 which the original code from Trebor has similar thought to put 1, 2... for the selection. I can't run here as I don't have JDK on my PC.
Replacing the inner set of "if" statements with "switch/case" would be ideal and nice.
Replacing the inner set of "if" statements with "switch/case" would be ideal and nice.
Re: 18 errors in this OMG
Yes. Switch block is the best way to handle this kind of work.
And some exceptions should also be handled. I didn't bother mentioning. He might not know about exceptions yet.
And some exceptions should also be handled. I didn't bother mentioning. He might not know about exceptions yet.