Java Java Basics Getting Started with Java IO

Stucking here

String firstName = console.readLine ("First name? "); String lastName = console.readLine (" last name? "); console.printf("First name: %s", firstName); console.printf("Last name: %s", lastName);

3 Answers

Tonnie Fanadez
Tonnie Fanadez
UX Design Techdegree Graduate 22,342 Points

Hello Md. Jafrin Hossain

I checked the code that you provided above and it works just fine when I tested every line.

Anyway here is my code which is similar to yours.

String firstName =console.readLine ("what is your first name?");
String lastName =console.readLine ("what is your first name?");
console.printf("First name: %s", firstName);
console.printf("Last name: %s", lastName);

Thank you so much for the reply Tonnie Fanadez . Would you kindly help me in the following section

Code in my ide(eclipse):

package treehouse; import java.io.*;

public class Chechk { public static void main(String[] args) { String fn = "Jafrin"; System.out.println(fn);

    Console console = System.console();
    String name = console.readLine("Enter your name ");     
    String adjective = console.readLine("Enter an adjective ");
    console.printf("%s is very %s", name, adjective);


}

}

output: Exception in thread "main" java.lang.NullPointerException at treehouse.Chechk.main(Chechk.java:10) Jafrin

Why it shows NullPointerException?

Tonnie Fanadez
Tonnie Fanadez
UX Design Techdegree Graduate 22,342 Points

Hi Md. Jafrin Hossain

Instead of working with console object please replace it with Scanner Object for Eclipse IDE. You will also need to import the Scanner Object using this line _ import java.util.Scanner; _

// Console console = System.console(); remove the Console Object and replace it with Scanner
   Scanner scanner = new Scanner(System.in);
   System.out.println("Enter your name");
    String name = scanner. nextLine(); 
System.out.println("Enter an adjective");
    String adjective = scanner. nextLine(); 
   System.out.printf("%s is very %s", name, adjective);

Please let me know if this helps

Thank you so much for your help brother.

Tonnie Fanadez
Tonnie Fanadez
UX Design Techdegree Graduate 22,342 Points

Md. Jafrin Hossain glad it worked, the only disadvantage of the Scanner Object is you have to prompt the user manually using System.out.println() method.

Yes, I have got it.