user2408746
user2408746

Reputation:

scanner in java not working on my machine

public class CollectionTest {

    public static void main(String args[]) {

        //create Scanner instance to get input from User
        Scanner scanner = new Scanner(System.in);

        System.err.println("Please enter first number to add : ");
        int number = scanner.nextInt();

        System.out.println("Enter second number to add :");
        int num = scanner.nextInt();

        //adding two numbers in Java by calling method
       int result = add(number, num);

       System.out.printf(" Addition of numbers %d and %d is %d %n", number, num, result);
    }

    public static int add(int number, int num){
        return number + num;
    }
}

I wonder what's wrong with this code.When I run this program on my new laptop this works fine. But on my old pc it gives some error. Please help me out.Thnx in advance.d

Upvotes: 0

Views: 119

Answers (2)

Rishabh Goyal
Rishabh Goyal

Reputation: 99

Check your pc's java version.

Java Scanner is included from a specific version.java.util.Scanner was introduced in version 1.5 ("Java 5"). Make sure you have the same the java version 1.5 or higher on your old machine.

Download new version of jvm and jre from their site and then try.

Upvotes: 2

Juned Ahsan
Juned Ahsan

Reputation: 68715

java.util.Scanner was introduced in version 1.5 ("Java 5"). Make sure you have the same the java version 1.5 or higher on your old machine.

Upvotes: 0

Related Questions