Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #22645
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2013-03-01 00:11 -0800 |
| Message-ID | <9a346b17-8bba-4e06-98ef-2ecf13a51a01@googlegroups.com> (permalink) |
| Subject | Console Input Error-2 |
| From | subhabangalore@gmail.com |
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 thought to ask the experts in the room.
Thanking You in anticipation,
Regards,
Subhabrata.
Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread
Console Input Error-2 subhabangalore@gmail.com - 2013-03-01 00:11 -0800
csiph-web