Home > DeveloperSection > Forums > How to use the Scanner class in java?
Ankit Singh

Total Post:341

Posted on    October-04-2013 9:14 AM

 Java Java 

 1 Reply(s)
 669  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.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.


jacob rasel
jacob rasel

Total Post:88

Posted on    October-04-2013 9:27 AM

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



Don't want to miss updates? Please click the below button!

Follow MindStick