X-Received: by 10.66.86.133 with SMTP id p5mr1618087paz.23.1362062867827; Thu, 28 Feb 2013 06:47:47 -0800 (PST) X-Received: by 10.50.186.202 with SMTP id fm10mr1070462igc.10.1362062867728; Thu, 28 Feb 2013 06:47:47 -0800 (PST) Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!jn4no16307191pbb.1!news-out.google.com!ov8ni22355pbb.1!nntp.google.com!jn4no16307182pbb.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Thu, 28 Feb 2013 06:47:47 -0800 (PST) In-Reply-To: <512f6ae1$0$285$14726298@news.sunsite.dk> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=122.161.206.44; posting-account=6SonuQoAAACzSakS5dCECcJQe6ylLrzY NNTP-Posting-Host: 122.161.206.44 References: <512f6ae1$0$285$14726298@news.sunsite.dk> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6d604145-dbbe-4f89-87c8-7e8180dfbd6a@googlegroups.com> Subject: Re: Input from Console From: subhabangalore@gmail.com Injection-Date: Thu, 28 Feb 2013 14:47:47 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.programmer:22619 On Thursday, February 28, 2013 8:04:08 PM UTC+5:30, Arne Vajh=F8j wrote: > On 2/28/2013 9:28 AM, subhabangalore@gmail.com wrote: >=20 > > I am trying to learn Java. I was trying to write a code for input from = Console. >=20 > > >=20 > > I found two ways to do it. One is by using import java.io.*; and the ot= her is, >=20 > > import java.util.Scanner; >=20 > > >=20 > > If I assign a class name and start the console input from public static= void main >=20 > > both works fine. >=20 > > >=20 > > But as I want to declare one function for it and like to call it in mai= n body. >=20 > > >=20 > > I am not being able to do that. >=20 > > >=20 > > If any one of the learned members can kindly suggest it. >=20 >=20 >=20 > Could you post the code that is not working? >=20 >=20 >=20 > Arne Dear Sir, Thank you for your kind reply. The Code is as follows: import java.io.*; import java.util.Scanner; public class AddressBook { public static void main(String[] args) { name(); System.out.print("\n"); name1(); } public static String name(){ String s1=3D"Subhabrata"; String s2=3D"Banerjee"; System.out.print("The First Name is:"+s1); System.out.println("\n"); System.out.print("The Surname Is:"+s2); return s1; } public static void name1(){ String name; int age; Scanner in =3D new Scanner(System.in); =20 name =3D in.nextLine(); age=3Din.nextInt(); in.close();=20 System.out.println("Name :"+name); System.out.println("Age :"+age); =20 =20 =20 =20 } } It is compiling and running fine but if I want to give any input it is givi= ng error as, Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at AddressBook.name1(AddressBook.java:24) at AddressBook.main(AddressBook.java:7) Regards, Subhabrata.