Try this:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Sysin {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter you name: ");
String name = br.readLine();
System.out.println("Hello " + name);
}
}
Regards,
Martin.
> Hello
>
[quoted text clipped - 8 lines]
>
> Angus
martin.lansler@gmail.com - 16 Nov 2006 19:15 GMT
One more thing...
In Java 5 one can make a static import:
import static java.lang.System.out;
allowing one to write:
out.println("Enter you name: ");
/Martin Lansler
martin.lans...@gmail.com wrote:
> Try this:
>
[quoted text clipped - 26 lines]
> >
> > Angus
> Hello
>
[quoted text clipped - 8 lines]
>
> I thought I should be able to use something like System.in.read ? No?
As you've noticed, there are several ways that work and are in common usage.
One approach that seems quite popular for those on Java 1.5 or later is to
use the Scanner class. I know a guy who is just learning Java at college and
they use Scanner for all their console I/O.
--
Rhino
Taria - 17 Nov 2006 11:02 GMT
> One approach that seems quite popular for those on Java 1.5 or later is to
> use the Scanner class. I know a guy who is just learning Java at college and
> they use Scanner for all their console I/O.
>
> --
> Rhino
I'm just learning java this semester and Scanner is pretty quick and
easy to use. The way to implement this into your program is to do the
following things:
1) type this line at the top of your program before any variables,
classes, main body is declared:
import java.util.Scanner;
2) You can use the API manual that will tell you all the methods that
are connected with Scanner but here are examples how you can use it to
take in input from a user.
Scanner input = new Scanner(System.in);
System.out.print("Enter your sentence: ");
String sentence = input.nextLine();
If you want to read in just a word and not a series of words, then
change "nextLine" to "next". To input an integer then use for example:
System.out.print("\nEnter the choice, followed by 2
integers: ");
String choice = input.next();
// evaluate choice then input 2 integers, expected
if (choice.equalsIgnoreCase("q")) {
quit = true;
System.out.println ("Adios mi amigo.");
}
else
{
int x = input.nextInt();
int y = input.nextInt();
// ... morec ode here
}