• Post Reply Bookmark Topic Watch Topic
  • New Topic

Output problem  RSS feed

 
Mohamed Said
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I was coding this and the output i got was
Enter your Course:

Which Year are you in?

Which Campus are you in?

My Name is CRISTIANO MESSInulldoing BBITnullyear 2.2nullin NRB CBD Campusnull

Am asking why is that there is a "null" between what i wrote
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a dangerous error in your code, but it isn't the cause of your current problem. Never use an empty catch. There are also some minor problems, but none of them causes you to print null.
The only way I can think of your getting null is if System.in has been closed by somebody writing ctrl‑D or ctrl‑Z somewhere. If you actually enter a real name, it prints it out. If you use one of the control characters instead of a real entry, you close System.in and the input points to null.

Don't write names into the print statement directly: I don't think it shou‍ld say CRISTIANO MESSI.
 
John Joe
Ranch Hand
Posts: 439
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested your code by just pressing Enter key, here the final output I get
I didn't get any "null" value.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:I tested your code by just pressing Enter key . . . .
That is because the reader interprets the enter key as line end, and reads whatever precedes it, in this case the empty String.
 
John Joe
Ranch Hand
Posts: 439
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That is because the reader interprets the enter key as line end, and reads whatever precedes it, in this case the empty String.
But why OP will get null value?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said: I think the only way that reader is going to return null is if you close System.in by pushing ctrl‑D or ctrl‑Z.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually: If I push one of those control sequences (which did make the app display null when I tried it earlier), what am I closing? If I push ctrl‑D it only seems to return null once; if I push enter next time, I get the empty String (which I can't see) printed.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell: you're on Linux, right?  On Windows, Ctrl-z produces the nulls the OP is talking about.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the nulls by pushing ctrl‑D; I said, “ctrl‑D or ctrl‑Z,” to cover Unix‑like systems and Windows®‑like systems.

I have to push ctrl‑D 4× to see all four nulls printed in their full glory; that made me wonder what exactly I am closing. I know that I would get an exception if I tried ctrl‑D even once with a Scanner, so am I closing the reader or System.in or what? Or is ctrl‑D simply terminating the input?
If I push ctrl‑Z on a Linux box, it stops execution completely.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Regardless, if this is the root cause, then why didn't the OP mention this? After all, if you are going to do something as abnormal as entering control-D, perhaps it may be related? And perhaps it would be a good idea to mention it? ...

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!