-2

I am trying to make this code work and it keep sending the error to the scaner. (Test.java:7: error: cannot find symbol)

class Test{ 
    public static void main(String[] args) {
        int x;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter number");
        x = in.nextInt();

        if (x<100)
           x=x +5; 

        if (x<500)
           x=x-2; 

        if (x>10)
           x++;  
        else
           x--;  
        System.out.println(x);

}

}
jack jay
  • 2,493
  • 1
  • 14
  • 27
ZeroOne
  • 9
  • 1
  • 4

3 Answers3

2

Correct your code to import java.util.Scanner; class like below and also change in.nextInt() to scanner.nextInt().

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        int x;

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter number");

        x = scanner.nextInt();

        if (x < 100)
        {
            x = x + 5;
        }

        if (x < 500)
        {
            x = x - 2;
        }

        if (x > 10)
        {
            x++;
        }
        else
        {
            x--;
        }

        System.out.println(x);
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
1

1. You have used Scannerclass to take input while, you haven't told java that you are using it. For This you should import this import java.util.Scanner.

2. The class which has public static void main(String[] args) should be public.why

3. I think you should use if-else-if construct instead of many if because in your case if x = 20 it will be modified in all the three cases.

Community
  • 1
  • 1
jack jay
  • 2,493
  • 1
  • 14
  • 27
0

If you look at the Java doc here: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

You will see that Scanner needs:

import java.util.Scanner

and not

import java.io.*
Omar
  • 943
  • 6
  • 9