• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the Javadoc page of java.lang.System(). It tells you exactly when console() will return null.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic