Page 1 of 3
18 errors in this OMG
Posted: Sat Aug 07, 2010 8:59 pm
by Face
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(){
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 found 18 errors when I compile this in a java editor.these are the errors I found
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
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...

Re: 18 errors in this OMG
Posted: Sun Aug 08, 2010 12:45 am
by Neo
if(selection==+||selection==-||selection==*||selection==\){
Here +, -, etc... are operators. They do not represent an integer value.
The best thing you can do is, define "selection" as char and change all your "if"s as selection == '+' and so on.
Let me know if you get further errors.
Re: 18 errors in this OMG
Posted: Sun Aug 08, 2010 8:21 am
by Face
wow...I change according to you idea..it works..now only one error
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(){
Scanner input=new Scanner(System.in);
char 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");
}
}
}
}
this is the error i got in editor..
Code: Select all
>javac Gayan_calculator.java
Gayan_calculator.java:71: class, interface, or enum expected
}sub.nul.nul.nul.nul.nul.nul.nul.nul.nul.
Re: 18 errors in this OMG
Posted: Sun Aug 08, 2010 9:27 am
by Neo
There was an extra bracket after the last else. You missed it as the indents are not correctly maintained. I corrected the indents as below and removed the extra curly bracket.
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(){
Scanner input = new Scanner(System.in);
char 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");
}
}
}
Re: 18 errors in this OMG
Posted: Sun Aug 08, 2010 12:44 pm
by Face
still have an error...
Code: Select all
>javac Gayan_calculator.java
Gayan_calculator.java:25: cannot find symbol
symbol : method nextint()
location: class java.util.Scanner
selection = input.nextint();
^
1 error
>Exit code: 1
I think that is should be like this
because we changed the variable type....right?
Re: 18 errors in this OMG
Posted: Sun Aug 08, 2010 12:48 pm
by Face
OMG........It has the same error...again...
I changed that like this...
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(){
Scanner input = new Scanner(System.in);
char selection = 0;
list();
System.out.println("\npress option from the list");
System.out.println("--------------------------");
selection = input.nextchar();
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");
}
}
}
This is the new error I got...
Code: Select all
>javac Gayan_calculator.java
Gayan_calculator.java:25: cannot find symbol
symbol : method nextchar()
location: class java.util.Scanner
selection = input.nextchar();
^
1 error
>Exit code: 1
Re: 18 errors in this OMG
Posted: Mon Aug 09, 2010 10:26 am
by Neo
the problem is nextint which should be changed to nextInt (notice the capital 'I').
Java is a case sensitive language just as C/C++ so those things need to be considered carefully.
Re: 18 errors in this OMG
Posted: Mon Aug 09, 2010 7:28 pm
by Face
oh BRO I corrected it now..then there are no compiling errors..But it doesn't work...
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(){
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");
}
}
}
This is the massage I got when trying to run the program..
Code: Select all
>java -cp . Gayan_calculator
java.lang.NoSuchMethodError: main
Exception in thread "main" >Exit code: 1
Re: 18 errors in this OMG
Posted: Mon Aug 09, 2010 9:23 pm
by Face
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(String args[]){
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");
}
}
}
Now It runs...But Half.........

Re: 18 errors in this OMG
Posted: Tue Aug 10, 2010 11:06 am
by Herath
Half means?.
Normally, the argument list is a parameter of the main method.
So,
Code: Select all
public static void main(String[] args)
{
}