5 Replies - 484 Views - Last Post: 10 August 2017 - 03:36 AM Rate Topic: -----

#1 dragu_stelian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 18-June 15

Read input from console in Java

Posted 09 August 2017 - 12:14 PM

Hello!
In the next code I get an error when I try to enter data from the keyboard using Console Class.
I mentioned that I made the following code using Scanner Class and Buffered Reader Class,
and the code is running but I can not do with Console Class.

Please, give me a hint!

package myclass;
import java.io.IOException;

class FirstClass
{
    private String firstName;
    private String secondName;
    public FirstClass(final String firstName, final String secondName)
    {
        this.firstName = firstName;
        this.secondName = secondName;
    }
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public final String getFirstName()
    {
        return firstName;
    }
    public void setSecondName(String secondName)
    {
        this.secondName = secondName;
    }

    public final String getSecondName()
    {
        return secondName;
    }
};

public class MyClass
{
    public static void main(String[] args) throws IOException
    {
        FirstClass obj = new FirstClass("Dragu", "Stelian");
        System.out.println("Name is: " +obj.getFirstName() + " " +obj.getSecondName());

        String input = System.console().readLine();
        System.out.print("Enter First Name: ");
        obj.setFirstName(input);
        System.out.print("Enter Second Name: ");
        obj.setSecondName(input);

        System.out.println("Name is: " +obj.getFirstName() + " " +obj.getSecondName());
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Read input from console in Java

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13348
  • View blog
  • Posts: 53,159
  • Joined: 12-June 08

Re: Read input from console in Java

Posted 09 August 2017 - 12:19 PM

Please copy/paste the error message here.
Was This Post Helpful? 0
  • +
  • -

#3 Thomas1965  Icon User is online

  • D.I.C Head

Reputation: 52
  • View blog
  • Posts: 206
  • Joined: 09-September 16

Re: Read input from console in Java

Posted 09 August 2017 - 01:08 PM

The problem is a NullPointerException at line 39.
Not sure what the exact problem is, but it seems that sometimes there is no console.
A safe way around is to use the Scanner class.
https://www.javatpoi...m/Scanner-class
Was This Post Helpful? 0
  • +
  • -

#4 dragu_stelian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 18-June 15

Re: Read input from console in Java

Posted 09 August 2017 - 11:06 PM

View Postmodi123_1, on 09 August 2017 - 12:19 PM, said:

Please copy/paste the error message here.



Exception in thread "main" java.lang.NullPointerException
at myclass.MyClass.main(MyClass.java:41)
C:\Users\Stelian\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
Was This Post Helpful? 0
  • +
  • -

#5 IOExceptional  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 122
  • Joined: 15-September 16

Re: Read input from console in Java

Posted 10 August 2017 - 02:55 AM

The problem is not with your code, but (at least, in part) with Ant, which Netbeans uses as its default build tool.
The issue will likely not come up if you run the jar/class file directly using the 'java' command.

As far as fixing it goes, I've read of a partial workaround, but you're probably better off just using a Scanner.

You can find more information here and here.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,410
  • Joined: 20-September 08

Re: Read input from console in Java

Posted 10 August 2017 - 03:36 AM

Yes, Ant doesn't play nicely with stdin/stdout (Netbeans doesn't thoroughly either). But you're also not using Console properly either. You need something more like
    public static void main(String[] args) throws IOException {
        FirstClass obj = new FirstClass("Dragu", "Stelian");
        System.out.println("Name is: " + obj.getFirstName() + " " +
            obj.getSecondName());

        Console cons;

        if ((cons = System.console()) != null) {
            String input = cons.readLine("Enter first name%n");
            obj.setFirstName(input);
            input = cons.readLine("Enter second name%n");
            obj.setSecondName(input);

            System.out.println("Name is: " + obj.getFirstName() + " " +
                obj.getSecondName());
        }
    }


For that to work though, you might need to run it at the command line
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1