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
  • 4,805
  • 16
  • 34
  • 46

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
  • 10,658
  • 7
  • 38
  • 86