18 errors in this OMG

Java programming topics
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: 18 errors in this OMG

Post by Neo » Tue Aug 10, 2010 7:00 pm

I don't understand whether you need further help or not. can you explain a bit more what is required at this stage.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: 18 errors in this OMG

Post by Face » Tue Aug 10, 2010 8:22 pm

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");
          }
       }
    }

Thanks Bro herath1..I corrected It...

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
--------------------------
Then I enter a Option.Press enter after entering the option....then the error came there...

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
can you please tell me what is this error....
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: 18 errors in this OMG

Post by Herath » Tue Aug 10, 2010 8:31 pm

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. :D
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: 18 errors in this OMG

Post by Herath » Tue Aug 10, 2010 8:39 pm

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");
              }
           }
        }



This should work.
I have change the type of the variable "selection" to char type.
And reading from scanner,
selection=input.next().toCharArray()[0];
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: 18 errors in this OMG

Post by Neo » Tue Aug 10, 2010 8:40 pm

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

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;
}
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: 18 errors in this OMG

Post by Face » Tue Aug 10, 2010 8:42 pm

yeah bro..In this program I first start like that...
G-sparkZ wrote:

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... :D
see my first post...it contain ints..but is had error when I use + - / * for my options in my program...

can you please correct this for me...
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: 18 errors in this OMG

Post by Herath » Tue Aug 10, 2010 9:03 pm

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.

Code: Select all


if(selection==+||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. :)
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: 18 errors in this OMG

Post by Herath » Tue Aug 10, 2010 9:12 pm

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");
             }
          
          }

    }
This is a way that you could do the job if you want to use integer reads from scanner utility. Any other suggestion?. Anyone?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: 18 errors in this OMG

Post by Neo » Tue Aug 10, 2010 9:20 pm

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.
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: 18 errors in this OMG

Post by Herath » Tue Aug 10, 2010 9:23 pm

Yes. Switch block is the best way to handle this kind of work. :idea:
And some exceptions should also be handled. I didn't bother mentioning. He might not know about exceptions yet. :)
Post Reply

Return to “Java Programming”