+ 2

How to input two strings simultaneously in Java?

import java.util.Scanner; class Customer { public static void main(String[] args) { Scanner sc =new Scanner(System.in); System.out.print("Enter your name:"); String name = sc.nextLine(); System.out.print("Enter age:"); int age = sc.nextInt(); System.out.print("Enter gender:"); String gender = sc.nextLine(); System.out.print("Hailing from:"); String city = sc.nextLine(); System.out.println("Welcome, "+name+"!"); System.out.println("Age:"+age); System.out.println("Gender:"+gender); System.out.println("City:"+city); } } In the above code, only one string city(out of gender and city) is taking the input. Why not the gender? Console takes input name, age and skips for gender. Is there any other way to take two lines of strings one after the other?

7th Mar 2020, 10:44 AM
Narasimha Murthy D V
Narasimha Murthy D V - avatar
5 Antworten
+ 3
I'll try with that
7th Mar 2020, 10:56 AM
Narasimha Murthy D V
Narasimha Murthy D V - avatar
+ 2
Use sc.next(); OR give sc.nextLine(); after age input.
7th Mar 2020, 10:55 AM
Avinesh
Avinesh - avatar
+ 2
Nope, its not working
8th Mar 2020, 3:24 PM
Narasimha Murthy D V
Narasimha Murthy D V - avatar
+ 1
Ask for age after city
7th Mar 2020, 3:14 PM
Ashutosh Mulky
Ashutosh Mulky - avatar
0
anyone done this q?
8th May 2021, 5:56 PM
Abhishek Jain
Abhishek Jain - avatar