hi!

hi!

how can i write aprogram in java by using scanner when asking user to enter element user will be free to enter any type that he/she wants to to enter, like(int,double,float,String,....) thanx for answering....

View Answers

January 27, 2011 at 11:20 AM

Hi,

Try this:

import java.util.*;
class  ScannerExample
{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter integer: ");
        int i=input.nextInt();
        System.out.print("Enter double: ");
        double d=input.nextDouble();
        System.out.print("Enter float: ");
        float f=input.nextFloat();
        System.out.print("Enter string: ");
        String s=input.next();
        System.out.println(i);
        System.out.println(d);
        System.out.println(f);
        System.out.println(s);

    }
}

Thanks









Related Tutorials/Questions & Answers:

Ads