New course! Every coder should learn Generative AI!
Try a free lesson+ 2
Dont accept String name
import java.util.Scanner; class A { int hno; String city,state; void getdata() { Scanner sc = new Scanner(System.in); System.out.println("\n\t enter the house number"); hno = sc.nextInt(); System.out.println("\n\t enter the city name"); city = sc.nextLine(); System.out.println("\n\t enter the state name"); state = sc.nextLine(); } void display() { System.out.println("House no : "+hno +""+"\n"+ "City name : "+city +""+"\n"+ "state name: "+state); } } public class SimpleI extends A { public static void main(String[] args) { SimpleI hcs = new SimpleI(); hcs.getdata(); hcs.display(); } }
3 Answers
+ 3
I don't understand the question.
Anyway, if there's an issue with your code its probably how you're putting in the input.
Add a blank nextLine() after taking the house number as input.
Like so:
sc.nextLine();
city = sc.nextLine();
This is since the input stream hasn't move to the next line yet after you took the int as input.
So you could also just input it like this instead of adding the extra nextLine():
7261 myWord
LastThing
instead of:
7261
myWord
LastThing
+ 1
wait i will check
+ 1
thankew sir.....