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


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

Re: Input from Console

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 <c9d678fe-b7f0-4757-8d4c-ea7454f8b2fb@googlegroups.com> <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> (permalink)
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

Show key headers only | View raw


On Thursday, February 28, 2013 8:04:08 PM UTC+5:30, Arne Vajhøj wrote:
> On 2/28/2013 9:28 AM, subhabangalore@gmail.com wrote:
> 
> > I am trying to learn Java. I was trying to write a code for input from Console.
> 
> >
> 
> > I found two ways to do it. One is by using import java.io.*; and the other is,
> 
> > import java.util.Scanner;
> 
> >
> 
> > If I assign a class name and start the console input from public static void main
> 
> > both works fine.
> 
> >
> 
> > But as I want to declare one function for it and like to call it in main body.
> 
> >
> 
> > I am not being able to do that.
> 
> >
> 
> > If any one of the learned members can kindly suggest it.
> 
> 
> 
> Could you post the code that is not working?
> 
> 
> 
> 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="Subhabrata";
	   String s2="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 = new Scanner(System.in);
       
	   name = in.nextLine();
	   age=in.nextInt();
       in.close(); 
       System.out.println("Name :"+name);
       System.out.println("Age :"+age);
	   

	   
	   
	   
   }
}

It is compiling and running fine but if I want to give any input it is giving 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.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Input from Console subhabangalore@gmail.com - 2013-02-28 06:28 -0800
  Re: Input from Console Arne Vajhøj <arne@vajhoej.dk> - 2013-02-28 09:34 -0500
    Re: Input from Console subhabangalore@gmail.com - 2013-02-28 06:47 -0800
      Re: Input from Console FredK <fred.l.kleinschmidt@gmail.com> - 2013-02-28 07:32 -0800
      Re: Input from Console markspace <markspace@nospam.nospam> - 2013-02-28 08:42 -0800
  Re: Input from Console Roedy Green <see_website@mindprod.com.invalid> - 2013-02-28 07:44 -0800
    Re: Input from Console subhabangalore@gmail.com - 2013-02-28 08:21 -0800
    Re: Input from Console Arne Vajhøj <arne@vajhoej.dk> - 2013-02-28 16:55 -0500

csiph-web