Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #22645

Console Input Error-2

X-Received by 10.66.151.236 with SMTP id ut12mr1930164pab.22.1362125497844; Fri, 01 Mar 2013 00:11:37 -0800 (PST)
X-Received by 10.50.135.41 with SMTP id pp9mr1645357igb.2.1362125497806; Fri, 01 Mar 2013 00:11:37 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!jn4no2609850pbb.1!news-out.google.com!ov8ni25237pbb.1!nntp.google.com!jn4no2609847pbb.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.programmer
Date Fri, 1 Mar 2013 00:11:37 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=122.161.13.125; posting-account=6SonuQoAAACzSakS5dCECcJQe6ylLrzY
NNTP-Posting-Host 122.161.13.125
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <9a346b17-8bba-4e06-98ef-2ecf13a51a01@googlegroups.com> (permalink)
Subject Console Input Error-2
From subhabangalore@gmail.com
Injection-Date Fri, 01 Mar 2013 08:11:37 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.java.programmer:22645

Show key headers only | View raw


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


Thread

Console Input Error-2 subhabangalore@gmail.com - 2013-03-01 00:11 -0800

csiph-web