Console Input Error-2

S

subhabangalore

Dear Group,
I was trying to experiment something more on scanner tried to write some more code in it, I wrote the following code,

//import java.io.*;
import java.util.Scanner;
public class AddressBook {
public static void main(String[] args) {

System.out.println("The Name and Surname As Provided Are:");
name1();
System.out.println("\n");
address();

}

public static void name1(){
String name;
//int age;
String surname;
Scanner in = new Scanner(System.in);

name = in.nextLine();
surname=in.nextLine();
in.close();
System.out.println("Name Is :"+name);
System.out.println("Surname Is :"+surname);




}
public static void address(){
String h_name;
int h_no;
String st_name;
String city;
String municipality;
String district;
String state;
String country;
int pin;
Scanner in = new Scanner(System.in);
System.out.println("Insert House Name");
h_name = in.nextLine();
System.out.println("Insert House No");
h_no=in.nextInt();
System.out.println("Insert Street Name");
st_name=in.nextLine();
System.out.println("Insert City Name:");
city=in.nextLine();
System.out.println("Insert Municipality Name");
municipality=in.nextLine();
System.out.println("Insert District Name");
district=in.nextLine();
System.out.println("Insert State Name:");
state=in.nextLine();
System.out.println("Insert Country Name:");
country=in.nextLine();
System.out.println("Insert PIN Number:");
pin=in.nextInt();
in.close();

System.out.println("The House Name Is:"+h_name);
System.out.println("The House Number Is:"+h_no);
System.out.println("Street Name Is:"+st_name);
System.out.println("The City Name Is:"+city);
System.out.println("The Municipality Name Is:"+municipality);
System.out.println("The District Name Is:"+district);
System.out.println("The State Name Is:"+state);
System.out.println("The Country Name Is:"+country);
System.out.print("The Pin Code Is:"+pin);

}
}

It gave me the following error,
the first method is fine but the second gave me the following error,
The Name and Surname As Provided Are:
Subha
Banerjee
Exception in thread "main" Name Is :Subha
Surname Is :Banerjee


Insert House Name
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at AddressBook.address(AddressBook.java:41)
at AddressBook.main(AddressBook.java:9)

To deal with I closed the in.close(); in first method with an idea that if a file is called before every operation is done it should not be closed. Now, it ran fine. Is it a silly trick just worked, or a proper way. I thoughtto ask the experts in the room.

Thanking You in anticipation,
Regards,
Subhabrata.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top