HOW TO USE THE SCANNER CLASS IN JAVA?

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
Java 
 1042  View(s)
Ratings:
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.

Thanks!

  1. jacob rasel

    Post:88

    Points:616
    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.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);

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!