Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #22617 > unrolled thread
| Started by | subhabangalore@gmail.com |
|---|---|
| First post | 2013-02-28 06:28 -0800 |
| Last post | 2013-02-28 16:55 -0500 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | subhabangalore@gmail.com |
|---|---|
| Date | 2013-02-28 06:28 -0800 |
| Subject | Input from Console |
| Message-ID | <c9d678fe-b7f0-4757-8d4c-ea7454f8b2fb@googlegroups.com> |
Dear Group, 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. Regards, Subhabrata.
[toc] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2013-02-28 09:34 -0500 |
| Message-ID | <512f6ae1$0$285$14726298@news.sunsite.dk> |
| In reply to | #22617 |
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
[toc] | [prev] | [next] | [standalone]
| From | subhabangalore@gmail.com |
|---|---|
| Date | 2013-02-28 06:47 -0800 |
| Message-ID | <6d604145-dbbe-4f89-87c8-7e8180dfbd6a@googlegroups.com> |
| In reply to | #22618 |
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.
[toc] | [prev] | [next] | [standalone]
| From | FredK <fred.l.kleinschmidt@gmail.com> |
|---|---|
| Date | 2013-02-28 07:32 -0800 |
| Message-ID | <30a1ddc3-a6dd-43ea-8ad3-5c2d8be23a98@googlegroups.com> |
| In reply to | #22619 |
On Thursday, February 28, 2013 6:47:47 AM UTC-8, subhaba...@gmail.com wrote:
> 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.
What did you type as input?
If the second line was not an integer, you will get that error.
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2013-02-28 08:42 -0800 |
| Message-ID | <kgo1c9$kp8$1@dont-email.me> |
| In reply to | #22619 |
On 2/28/2013 6:47 AM, subhabangalore@gmail.com wrote:
> Scanner in = new Scanner(System.in);
>
> name = in.nextLine();
> age=in.nextInt();
I'm having a very hard time using Scanner this way. I think it's
perhaps because I normally wrap System.in in a BufferedReader. But
let's in general introduce a third method of reading console input,
which is to use a BufferedReader.
BufferedReader input = new BufferedReader(
new InputStreamReader( System.in ) );
Now you can read lines from the console, which is a little more
intuitive and also works as many users expect. Then the trick is to
extract an integer (or whatever input you are looking for) from the line
of text, which can be done with Scanner.
Full code:
package quicktest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class InputTest {
public static void main( String[] args ) throws IOException {
BufferedReader input = new BufferedReader(
new InputStreamReader( System.in ) );
int age = -1;
do {
System.out.println( "Please enter your age");
String line = input.readLine();
Scanner scan = new Scanner( line );
if( scan.hasNextInt() ) {
age = scan.nextInt();
break;
}
} while( true );
System.out.println( "Age: "+age );
}
}
Study this carefully, and try entering some bad input when it asks for
age. I think you'll see how it works. This could be made more pithy, I
think, but I'll leave it like this because I think it's easier to trace
for someone who's just starting out.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2013-02-28 07:44 -0800 |
| Message-ID | <uluui81uohbjn3chrmlr5lt0fn9a11tdio@4ax.com> |
| In reply to | #22617 |
On Thu, 28 Feb 2013 06:28:23 -0800 (PST), subhabangalore@gmail.com wrote, quoted or indirectly quoted someone who said : >I found two ways to do it. One is by using import java.io.*; and the other is, >import java.util.Scanner; there is also out.println and in.readln. See http://mindprod.com/applet/fileio.html for sample code. -- Roedy Green Canadian Mind Products http://mindprod.com One thing I love about having a website, is that when I complain about something, I only have to do it once. It saves me endless hours of grumbling.
[toc] | [prev] | [next] | [standalone]
| From | subhabangalore@gmail.com |
|---|---|
| Date | 2013-02-28 08:21 -0800 |
| Message-ID | <2d3dc654-852f-4f3f-8a6f-7c4a7c1a3210@googlegroups.com> |
| In reply to | #22623 |
On Thursday, February 28, 2013 9:14:30 PM UTC+5:30, Roedy Green wrote: > On Thu, 28 Feb 2013 06:28:23 -0800 (PST), subhabangalore@gmail.com > > wrote, quoted or indirectly quoted someone who said : > > > > >I found two ways to do it. One is by using import java.io.*; and the other is, > > >import java.util.Scanner; > > > > there is also out.println and in.readln. > > > > See http://mindprod.com/applet/fileio.html for sample code. > > -- > > Roedy Green Canadian Mind Products http://mindprod.com > > One thing I love about having a website, is that when I complain about > > something, I only have to do it once. It saves me endless hours of > > grumbling. Thank you Sir. My input was wrong. Regards, Subhabrata.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2013-02-28 16:55 -0500 |
| Message-ID | <512fd250$0$289$14726298@news.sunsite.dk> |
| In reply to | #22623 |
On 2/28/2013 10:44 AM, Roedy Green wrote: > On Thu, 28 Feb 2013 06:28:23 -0800 (PST), subhabangalore@gmail.com > wrote, quoted or indirectly quoted someone who said : > >> I found two ways to do it. One is by using import java.io.*; and the other is, >> import java.util.Scanner; > > there is also out.println and in.readln. > > See http://mindprod.com/applet/fileio.html for sample code. in.readln is that a new thing in Java 9 alpha version or?? Arne
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web