• Post Reply Bookmark Topic Watch Topic
  • New Topic

please help me on this file reading program  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading questions from a file named questions.txt and each time a question is read the user is prompted to provide an answer to it, but the problem is that the first and second questions are read together before the user is prompted to provide an answer.....please let me know if there is anything am doing wrong..Thank You


//this is the constructor which accepts a Scanner object, so the questions are read when the class is instantiated////////////////////


 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move most of that code out of the constructor, I suggest. Don't pass a Scanner to the constructor like that. That sort of code should be in a method; if you want the method to be called automatically when the object is created, do this sort of thingIs the file you are reading from a configuration file? In which case will the results be the same for all instances of your class? In which case don't you need one copy of the results? In which case it looks like something which might well be static. So let's have a static field to hold the results. I shall try a List<String>. In this thread, you can see examples of how to create a List so it can never be altered.Unmodifiable means you can only change the List from its original location. Since it was a local variable, the original location has disappeared, and you can no longer change anything.

It is probably easier to use a Scanner for text files because you don't suffer IO exceptions but line 8 may throw a file not found exception which you must handle.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!