Advertise with us

How to use the Scanner class in java?

I was doubting how, I could read input from the console using the Scanner class?

here's some code:

Scanner ob=new Scanner(System.in);
System.out.println("Enter your username: ");
String username=ob.nextLine();
System.out.println("Enter the value of a");
int a=ob.nextInt();

So basically all I want to do is have the scanner read an input for the username, and assign the input to a String variable.

Thanks!

Last updated:10/4/2013 9:27:26 AM

1 Answers

jacob rasel
jacob rasel

import java.util.Scanner;
public class scanner
{
    public static void main (String[]args)
    {
        int number1,int number2, sum;
        Scanner input = new Scanner (System.in);
        System.out.print("Enter First multiple");
        number1 = input.nextInt();
        System.out.printlin ("Enter second multiple");
        Number2 = input.nextInt();
        sum = number1 * number2;
        System.out.printf ("The product of both number is %d" , sum);
    }
}

Last updated:10/4/2013 9:27:26 AM

Answer