Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is the import java.io.Console receives a warning & not able to run console?

 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am getting the following warnings on line 3 when editing RegexTestHarness.java tutorial using Java 7 & Netbeans 7.1.1 on Windows XP platform:

“Import section does not correspond to the specified code style rules”

Below is the content of RegexTestHarness.java:



It ran with the following output in Netbeans:

run:
No console.
Java Result: 1

What is wrong with the console import on line 3 and how RegexTestHarness invoke it within Netbeans to use it? It appears to have failed due to not able to locate / launch java.io.Console.
JConsole plugin has been added to Netbeans without success. I have done some Googling but could not find an answer for it.

I have been directed by Netbeans forum members back here since this is a general Java question.

Thanks,

Jack
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the Javadoc page of java.lang.System(). It tells you exactly when console() will return null.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: . . . It tells you exactly when console() will return null.
Not in my Java7 version, it doesn’t. You get a Console object for anything started with the java tool. Most IDEs (and executable jars) use the javaw tool to start, and there is likely to be no Console.
I am surprised it says “no console” rather than “Exception in thread "main" java.lang.NullPointerException...”

Replace all those console objects with a Scanner using System.in and a print instruction for the prompt.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: It tells you exactly when console() will return null.

I read java.lang.System() but still do not understand how console() is used but assumed that the reason why it return null because there are no console available.

Campbell Ritchie wrote: Replace all those console objects with a Scanner using System.in and a print instruction for the prompt.
Could you please elaborate a bit more using the above code as example since I do not know how to replace console with Scanner using System.ini or print instruction for prompt.

Thanks to you both for your advice,

Jack
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you never used a Scanner? Start by reading its API page. Except for nextLine(), its methods are all pretty intuitive and easy to use.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Bush wrote: . . . Scanner using System.ini . . .
Not System.ini. System.in. And the print instruction would look like this:
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Rob Spoor wrote: . . . It tells you exactly when console() will return null.
Not in my Java7 version, it doesn’t. You get a Console object for anything started with the java tool. Most IDEs (and executable jars) use the javaw tool to start, and there is likely to be no Console.

You're right, the description isn't in the Javadoc of System.console(). It's in the Javadoc of java.io.Console itself.

I am surprised it says “no console” rather than “Exception in thread "main" java.lang.NullPointerException...”

Jack checks for null and prints this message if that's the case.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: . . . Jack checks for null and prints this message if that's the case.
I really ought to learn to read the code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic