• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange values in ArrayList  RSS feed

 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the following code to enter elements in an ArrayList using BufferedReader()


After running it i entered the value of the variable "i" as 3, then i entered the value of the first element as "2", but just after that i got the following output with elements that i never entered and the for loop did not even complete three iterations


will anyone please explain this.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could it be that inside the loop you're using read() instead of readLine() and it doesn't wait for input? Actually I just noticed 13 and 10 in the mysterious output, which are the CR and LF characters. Might be a clue...
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct, GI.

Avoid the read() method like the plague. It is a dreadful method.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indeed, read() is not very useful,but i want to assign two integer variables at runtime at the same line,is there anything faster than using a scanner to accomplish that.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What has faster got to do with it? Whichever code you use will take 0.05μs to parse the text and add it to the List. It may take 1ms to transfer it from screen to text. And it will take 2s for you to type the number, so you can forget all aboiut the word “faster”.
I would suggest using a Scanner is simplest, however.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!