Forum Stats

  • 3,759,510 Users
  • 2,251,557 Discussions
  • 7,870,689 Comments

Discussions

How can I get Input from User In Java using Scanner Class?

Hi, all I was reading this article and was trying Scanner Class to take input from users in Java -

  • Now the problem is I am facing an issue in execution -
import java.util.Scanner;
public class a
{
public static void main()
{
    Scanner b = new Scanner(System.in);
    System.out.println("Enter the integer");
    int c = b.nextInt();
    System.out.println("Enter the string");
    String d = b.nextLine();
    System.out.println();
    System.out.println(" The integer you entered is" + a);
    System.out.println(" The string you entered is "+b);
    }
}


Can anyone help me to rectify where I have done the mistake?

  • What would be the output of this code -
import java.util.Scanner;
 
public class scannerInput {
   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter an integer");
       int a=sc.nextInt();
       System.out.println("Enter a string");
       String s=sc.nextLine();
       System.out.println("String is "+ s);
   }
}

Please give your suggestions, It will help me in my assignment

Thank you!

Tagged: