0
import java.util.Scanner;

public class Main { 
    public static void main(String [] args) {
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter your name");
        String n=sc.nextLine();

        System.out.println("Enter your age");
        int age=sc.nextInt();

        System.out.println("Enter your gender");
        String g=sc.nextLine();

        System.out.println("Hailing From");
        String c=sc.nextLine();

        System.out.println("Welcome, " +n+ "!");
        System.out.println("Age:" +age);
        System.out.println("Gender:" +g);
        System.out.println("City:" +c);
    }
}

The code is not taking input for gender. Don't know what's wrong in the code?

Robert
  • 7,394
  • 40
  • 45
  • 64
  • 3
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Ashutosh Mar 30 '20 at 16:50
  • use sc.nextLine(); after *int age=sc.nextInt();* – Ashutosh Mar 30 '20 at 16:53

1 Answers1

0

When you get nextInt, you still need to go to the end of the line. Try the following:

import java.util.Scanner; public class Main {

public static void main(String [] args)      {
Scanner sc=new Scanner(System.in);

System.out.println("Enter your name");
String n=sc.nextLine();

System.out.println("Enter your age");
int age=sc.nextInt();
    sc.nextLine();   //YOU NEED TO ADD THIS LINE!

System.out.println("Enter your gender");
String g=sc.nextLine();

System.out.println("Hailing From");
String c=sc.nextLine();

System.out.println("Welcome, " +n+ "!");
System.out.println("Age:" +age);
System.out.println("Gender:" +g);
System.out.println("City:" +c);
}} 
DCR
  • 14,737
  • 12
  • 52
  • 115