This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Runtime error  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, so I was trying to do a uvajudge question and got a runtime error when I submitted the code using Scanner, then I changed it to BufferedReader and it's accepted. Would like to ask for the difference between these two codes?
Question is uva 11559 Event Planning:->
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2595





 
Nathaniel Oyt
Ranch Hand
Posts: 30
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the question
pic.png
[Thumbnail for pic.png]
 
Marshal
Posts: 61721
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use StringTokenizer. This link says so.
Please supply more details. We need to know more than that there was an error; if you give us full details of the error, we shall have a good idea what caused it, because we have seen it before.
I think the reason for the confusion is about the concept of hasNextLine(), as opposed to readLine().
The Scanner looks ahead in its input to see whether there is a next line. A next line may be any length, so if you are still in the line. If you look at this old post, maybe you are in the position of being after “Apple Records” and before the ↩. Right, in that case, the Scanner will see that as constituting a next line, albeit a very very short line. The test for has next line is done before reading.
If you use a buffered reader, it never looks ahead. It leaps before it looks. It hasn't got any hasNextXXX() methods, only methods to read something. So you read a line. Its methods to read anything shorter are a real pain to use. What happens is that when there is no line left to read, it returnsnull. I am not sure what happens to the empty line at the end of a file; you know most text files end with a line end sequence. I have tried it and the Scanner object doesn't seem to recognise an empty line at the end of a file.
If you look at these old posts 1 (two posts of mine in same thread) 2 (also see post one before that). So, you can get to the end of your file and have hasNextLine() return true, whereas hasNext() wouldn't find a token to parse, and will return false. Whereas the buffered reader reads the line first and only afterwards tests whether it has found anything.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, I looked thru the link to not use StringTokenizer but couldnt find out why(could only manage to find the methods in the class), regarding the details I dont know much because when I submitted it the judge only shows runtime error, and when I run the program the expected output is the same as the sample output. The nextInt() nextLine() confusion shouldnt occur here as all I use was only nextInt(). A question here, should the while loop stop when the program cant find any more line ahead? (That was an online question so it had a sample input, condition to terminate wasn't mentioned so I think this is the only way).
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the StringTokenizer documentation wrote:. . . StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. . . .

when I submitted it the judge only shows runtime error, and when I run the program the expected output is the same as the sample output.

Please explain more. Are you getting different output on a website from your own computer?
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I should use split instead of StringTokenizer?

ya I got the output but when I submitted the code, the judge displays runtime error. (usually displays"Accepted"when correct). They say runtime error is because the program won't end, but shouldn't hasNextLine() terminate the program when there's no more line?
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathaniel Oyt wrote:. . . should the while loop stop when the program cant find any more line ahead?

Yes. But as explained in my older posts, which I gave links to, it is possible to find one more line when there is no more text in the file.

(That was an online question so it had a sample input, condition to terminate wasn't mentioned so I think this is the only way).

They obviously have the nous to make you think of the solution for yourself. That sort of question is much better when it makes you think.

Why are you using System.in? I thought you had those data in a file. Unless you do something daft, System.in implicitly always has a next line. What I have said only applies to file reading, Your instructions clearly say to use an input file, so you should use such a file.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathaniel Oyt wrote:the question


this is the question and they have a submit button down thr to submit the code, text file wasnt given.

and the sample input had no empty line.
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathaniel Oyt wrote:so I should use split instead of StringTokenizer?  . . .

I would use a Scanner and nextInt(). It is only possible to finish the loop without error from System.in by using a sentinel value, so you can say do {...} while (input != −1); or similar. Despite the thought they put into the question as I mentioned earlier, is it actually possible to run the question on their website  ? Have they won on the swings and lost on the roundabouts?
A lot of text editors automatically add a line end after the last line in the file, so there would be an empty line; some applications even require that empty line at the end of the file. As you will have seeen from my old posts, one way to sort that problem out is to test whether the Scanner hasNext() rather than hasNextLine(). The only way to get that sort of method to return false when reading System.in is to close System.in, and that is usually a big mistake.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!