Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #22619
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2013-02-28 06:47 -0800 |
| References | <c9d678fe-b7f0-4757-8d4c-ea7454f8b2fb@googlegroups.com> <512f6ae1$0$285$14726298@news.sunsite.dk> |
| Message-ID | <6d604145-dbbe-4f89-87c8-7e8180dfbd6a@googlegroups.com> (permalink) |
| Subject | Re: Input from Console |
| From | subhabangalore@gmail.com |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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