Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPrabh Bajwa
Front End Web Development Techdegree Student 1,665 PointsI'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
57,712 PointsHi there,
Two typing errors - change cosole
to console
. The rest looks OK, I think.
Steve.
Steve Hunter
57,712 PointsHi 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
57,712 PointsThe 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
Front End Web Development Techdegree Student 1,665 PointsSir, I've changed everything but still, it's not working, I've updated the question
Steve Hunter
57,712 PointsYou now have:
Console Console = ...
Change to:
Console console = ...
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 PointsSo, Sir, if I want to make this program work on my machine what I have to change in that program?
Steve Hunter
57,712 PointsI'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
57,712 PointsThis code works fine:
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
Front End Web Development Techdegree Student 1,665 Pointsnow I'm getting this error Exception in thread "main" java.lang.NullPointerException at Introductions.main(Introductions.java:9) [Finished in 0.7s]
Steve Hunter
57,712 PointsDid your code run at all, or just throw the error?
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 PointsYes, 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
57,712 PointsSo 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
Front End Web Development Techdegree Student 1,665 Pointsimport 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
57,712 PointsI 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
?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI added a comment in your code to highlight where that little issue is.
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 PointsPrabh Bajwa
Front End Web Development Techdegree Student 1,665 PointsHello, 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.