• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Console

 
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why above code doesn't work?

While below one works?


I am highly confused with format function, since I don't understand what all possible first String argument can be there in it?
 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dishi ,

When i run this code

i Got the output:
Please enter your name: Hareendra
java.io.Console@9304b1Hello!

readLine method is overloaded in Console class which behave similar to the format function but it also
returns the a line input text ...
And imagine format function as similar to printf of c language..
toString() is not overriden in Concole so it prints the class name and hashcode...


And in second case

It compiled with warnings but it prints "Hello"..
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Upon running,


I always get console referring to null.
What settings are required to change?
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Dishi ,
Your doubt just helped me solving a problem ..
console() may return null if you try to use an IDE so try from command line
But this is a common problem and you can use search feature...
 
Sheriff
Posts: 9690
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try not to use any IDE for your preparation of SCJP exam. Not only does it sometimes hinder in learning by doing things automatically, the behavior of some code may differ when run from IDEs and from the normal java command of the JDK...
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hareendra and Ankit,
I will do that..
reply
    Bookmark Topic Watch Topic
  • New Topic