How to use the Scanner class in java?

Total Post:341


 1634  View(s)
Rate this:

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

here's some code:

Scanner ob=new Scanner(;
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.


  1. Re: How to use the Scanner class in java?

    import java.util.Scanner;
    public class scanner
        public static void main (String[]args)
            int number1,int number2, sum;
            Scanner input = new Scanner (;
            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);

      Modified On Mar-31-2018 04:32:19 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.