Skip to Main Content

Java and JavaScript in the Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

User_HWL0TOct 11 2021

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!

Comments

Post Details

Added on Oct 11 2021
1 comment
700 views