java input help (noobie)

Started by
6 comments, last by NUCLEAR RABBIT 13 years, 1 month ago
hello, im trying to create a very simple app to get a users name and then display it back but for some reason i can't get it to work... any help would be greatly appreciated!

[source]
package names;
import java.io.Console;

public class Main {

public static void main(String[] args) {
String name = "";

Console con = System.console();

System.out.println("Whats ur name? ");
name = con.readLine();

System.out.println("ur name is: " + name);
}

}
[/source]
Advertisement
Some more information about the problem would be nice...

The code you posted works fine for me. Your environment probably does not have System.console, so the call returns null. This is the case, for example, when trying to execute that code within Eclipse. My suggestion would be to add a null check for con and use the following code in that case:

if (con == null) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine();
} else {
name = con.readLine();
}

You can, of course, just skip the Console class and always use this. The only time that I really needed the Console class was when I wanted to read a password from the console without echoing the input.

Some more information about the problem would be nice...

The code you posted works fine for me. Your environment probably does not have System.console, so the call returns null. This is the case, for example, when trying to execute that code within Eclipse. My suggestion would be to add a null check for con and use the following code in that case:

if (con == null) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine();
} else {
name = con.readLine();
}

You can, of course, just skip the Console class and always use this. The only time that I really needed the Console class was when I wanted to read a password from the console without echoing the input.


i tried using the bufferedreader but i also got an error for that one as well. idk how to fix it :/ heres a screen shot of the error im getting: (im also programming java on a mac using the NetBeans IDE if that matters for some reason)

screenshot20110309at715.png

i tried using the bufferedreader but i also got an error for that one as well. idk how to fix it :/ heres a screen shot of the error im getting: (im also programming java on a mac using the NetBeans IDE if that matters for some reason)



Are you still just importing java.io.console? If so, switch that to java.io.* and see if that works. I find Scanner to be a particularly useful tool when working with input (user provided/ports/files) as well.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

You need to add

import java.io.BufferedInputStream;
import java.io.InputStreamReader;

to your imports.

Also it is

System.in

(without braces)

So the whole line needs to look like this:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

You also skipped the InputStreamReader in your code.

You need to add

import java.io.BufferedInputStream;
import java.io.InputStreamReader;

to your imports.

Also it is

System.in

(without braces)

So the whole line needs to look like this:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

You also skipped the InputStreamReader in your code.


thanks, that helped me! i also had to add a throws IOException in after the main parameters. im not sure why that made it work. could someone explain? heres the code that i got to work:

[package names;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

public static void main(String[] args) throws IOException {
String name = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Whats ur name? ");
name = br.readLine();

System.out.println("ur name is: " + name);
}

}
When some kind of error occurs when reading from the InputStream an Exceptionof the type IOException is thrown. When reading from System.in this is unlikely to happen, but the same mechanism yould also be used to read from a network socket, for example, which would throw an exception when the network connection is lost. If that case, you need to define how to handle that error. In your case you forward the throwing of the exception by adding the throws declaration to your main method. This tells the surrounding method that it has to handle the exception. Since the exception is thrown from the main method the JVM will take care of the exception (by exiting your program).
You should read up on exceptions and how to handle them (keywords: try, catch, finally) as it is quite important when programming Java. All error handling in Java is done through exceptions....

When some kind of error occurs when reading from the InputStream an Exceptionof the type IOException is thrown. When reading from System.in this is unlikely to happen, but the same mechanism yould also be used to read from a network socket, for example, which would throw an exception when the network connection is lost. If that case, you need to define how to handle that error. In your case you forward the throwing of the exception by adding the throws declaration to your main method. This tells the surrounding method that it has to handle the exception. Since the exception is thrown from the main method the JVM will take care of the exception (by exiting your program).
You should read up on exceptions and how to handle them (keywords: try, catch, finally) as it is quite important when programming Java. All error handling in Java is done through exceptions....


thanks a lot! I will read up on that like you suggested!

This topic is closed to new replies.

Advertisement