Java Java Basics Getting Started with Java Receiving Input

Prabh Bajwa
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

I'm having this issue with the code which works fine in Workspaces but not in my System's command prompt?

Hello, Dear Sir/Ma'am, I'm having this issue with the Java program it works fine in Workspaces but now in my Command prompt. please help me I really need to make it work in my System as well.

Thanks in Advance and Sorry I'm not that good in English that's why there could be lots of mistakes.

import java.io.Console;

public class Introductions {

    public static void main(String[] args) {
        Console Console = System.Console();
        //Welcome to the Introductions program! Your code goes below here
        String firstName = Console.readLine("What is your name?  ");
        //thisIsAnExmapleOfCamelCasing
        Console.printf("Hello, my name is %s\n", firstName);
        Console.printf("%s is learning how to write Java\n", firstName);
    }
}

I'm trying to make this code run in my System's command prompt.

Error I'm getting is

Introductions.java:6: error: cannot find symbol Console Console = System.Console(); ^ symbol: method Console() location: class System 1 error [Finished in 0.6s]

7 Answers

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Hi there,

Two typing errors - change cosole to console. The rest looks OK, I think.

Steve.

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

I added a comment in your code to highlight where that little issue is.

Prabh Bajwa
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

Hello, Sir thanks for the answer as I corrected the Typo but still it's not running in my System's command prompt. I hope you can help sir.

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Hi again,

Class names are capitalised - so change:

console console = System.console();

to

Console console = System.console();

That should fix it - let me know if it doesn't.

Steve.

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

The import needs cpaitalising too:

import java.io.Console;

However, you need to make sure you are creating a console application else this won't work on your machine.

Prabh Bajwa
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

Sir, I've changed everything but still, it's not working, I've updated the question

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

You now have:

Console Console = ...

Change to:

Console console = ...
Prabh Bajwa
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

So, Sir, if I want to make this program work on my machine what I have to change in that program?

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

I'm assuming this is a Windows machine?

In your command line make sure that java -version gives you output similar to this:

java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

If it does, then creating a file called Introductions.java (make sure it isn't a .txt file) in that folder should work the same way as your Workspace. You should be able to type java Introductions.java && java Introductions which should run your program. I can confirm your code works here with no problem.

Steve.

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

This code works fine:

Introductions.java
import java.io.Console;

public class Introductions{

    public static void main(String[] args) {
    // write your code here
        Console console = System.console();
        //Welcome to the Introductions program! Your code goes below here
        String firstName = console.readLine("What is your name?  ");
        //thisIsAnExmapleOfCamelCasing
        console.printf("Hello, my name is %s\n", firstName);
        console.printf("%s is learning how to write Java\n", firstName);
    }
}
Prabh Bajwa
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

now I'm getting this error Exception in thread "main" java.lang.NullPointerException at Introductions.main(Introductions.java:9) [Finished in 0.7s]

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

Did your code run at all, or just throw the error?

Prabh Bajwa
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

Yes, I'm using windows computer and this is my Java -version java version "1.8.0_162" Java(TM) SE Runtime Environment (build 1.8.0_162-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

So Java will work in that folder (and probably all others) - so there's a problem with your code. Can you post your current code, please?

Prabh Bajwa
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points
import java.io.Console;

public class Introductions{

    public static void main(String[] args) {
    // write your code here
        Console console = System.console();
        //Welcome to the Introductions program! Your code goes below here
        String firstName = console.readLine("What is your name?  ");
        //thisIsAnExmapleOfCamelCasing
        console.printf("Hello, my name is %s\n", firstName);
        console.printf("%s is learning how to write Java\n", firstName);
    }
}
Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

I can't see any reason for a NullPointerException in there? It seems to be as you access the console with readLine - but I can't see that there's anything wrong with your code.

This code definitely works with no exceptions - copy it into your file and see whether it works for you:

import java.io.Console;

public class Introductions{

    public static void main(String[] args) {
        Console console = System.console();
        String firstName = console.readLine("%nWhat is your name?: ");
        console.printf("%nHello, my name is %s.%n%n", firstName);
        console.printf("%s is learning how to write Java.%n", firstName);
    }
}

Is your file called Introductions.java?