• Post Reply Bookmark Topic Watch Topic
  • New Topic

code showing some arbitrary value  RSS feed

 
Nitish Mittal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hey I am using this code but it is showing taking some arbitrary value and I am not able debug the problem. Please help me with this code.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "arbitrary"? I'm sure it isn't...but we don't know:

What you are inputting
What you are seeing
What you expect to see

You have to remember that ItDoesntWorkIsUseless, so please TellTheDetails.

At the very least, put some System.out.println statements in there to print out various things - what K is, what i is, when your loop starts a new iteration, etc.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to read System.in in two different ways, and I suspect they are interfering with each other. The BufferedReader/InputStreamreader combo and the Scanner are fighting over who gets to read the next line. I suggest you remove one of those two things from the code, and use whatever remains to do all of your reading. I would probably just use the Scanner for everything.

Also, when you read things in the loop, how much are you trying to read at once? The read() method that you're calling only reads one character at a time, and what it reads is the Unicode value (which is often the ASCII value) of the character you've typed. Probably not what you want here. Try using the Scanner, and using nextInt().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!