Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Input from Console

Reply
Thread Tools

Input from Console

 
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      02-28-2013
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.
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-28-2013
On 2/28/2013 9:28 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      02-28-2013
On Thursday, February 28, 2013 8:04:08 PM UTC+5:30, Arne Vajh°j wrote:
> On 2/28/2013 9:28 AM, (E-Mail Removed) 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 staticvoid 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.
 
Reply With Quote
 
FredK
Guest
Posts: n/a
 
      02-28-2013
On Thursday, February 28, 2013 6:47:47 AM UTC-8, (E-Mail Removed) wrote:
> On Thursday, February 28, 2013 8:04:08 PM UTC+5:30, Arne Vajh°j wrote: > On 2/28/2013 9:28 AM, (E-Mail Removed) 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 worksfine. > > > > > > But as I want to declare one function for it and like tocall 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 foryour 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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-28-2013
On Thu, 28 Feb 2013 06:28:23 -0800 (PST), (E-Mail Removed)
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.
 
Reply With Quote
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      02-28-2013
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), (E-Mail Removed)
>
> 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.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      02-28-2013
On 2/28/2013 6:47 AM, (E-Mail Removed) 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.


 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-28-2013
On 2/28/2013 10:44 AM, Roedy Green wrote:
> On Thu, 28 Feb 2013 06:28:23 -0800 (PST), (E-Mail Removed)
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading console output and writing to console jan.rebada@gmail.com C++ 5 03-04-2008 09:00 AM
Preventing the console window from closing when writing a console program in C++ Miktor C++ 7 08-09-2005 09:22 PM
Re: console mp3->wav decoder for $indows or GUI one which supports console Serge Terekhoff Python 0 06-30-2003 12:36 PM
Re: console mp3->wav decoder for $indows or GUI one which supports console Egor Bolonev Python 0 06-30-2003 10:43 AM
Re: console mp3->wav decoder for $indows or GUI one which supports console Ben Finney Python 2 06-30-2003 05:43 AM



Advertisments