Public Group

# java input help (noobie)

This topic is 2834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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? ");

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

}
[/source]

##### Share on other sites

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.

##### Share on other sites

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)

##### Share on other sites

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.

##### Share on other sites
 import java.io.BufferedInputStream; import java.io.InputStreamReader; 

Also it is
 System.in 
(without braces)

So the whole line needs to look like this:
 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 

##### Share on other sites

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

Also it is
 System.in 
(without braces)

So the whole line needs to look like this:
 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 

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); } }

##### Share on other sites
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....

##### Share on other sites

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!

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633697
• Total Posts
3013407
×