Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

java input help (noobie)

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

If you intended to correct an error in the post then please contact us.

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? ");
name = con.readLine();

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

}
[/source]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

}

Share this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!