Input from Console

S

subhabangalore

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.
 
A

Arne Vajhøj

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
 
S

subhabangalore

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.
 
F

FredK

What did you type as input?
If the second line was not an integer, you will get that error.
 
M

markspace

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top