aspose file tools*
The moose likes Java in General and the fly likes File Scanner problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "File Scanner problem" Watch "File Scanner problem" New topic
Author

File Scanner problem

Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
Im making a file scanner that reads lines from a text file. The text file contains the information to construct a quiz question. The first line is the number of the question, the next is the actual question, and the rest of the lines are the answer choices. The question may have more than one answer, and in that case, the correct answers are separated from the incorrect answers by a blank line. For example:

1
What is this website?
helpful
awesome

bad
dumb

however, another example could be:

2
This website's name is?
coderanch.com

google.com
amazon.com

how would i make a scanner that could detect these blanks and save the answers into array lists that separate right and wrong answers? So it adds the correct answers and when it detects the split, it adds the wrong answers to an array as well.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

There are several different parts to your problem. You should break it down and work in the parts individually.

One thing you'll definitely want to do is define a class to hold the information. The class should store each part of a question separately--the number, the question, the right answers, the wrong answers. Note that you'll store the right answers separately from the wrong, so you can tell them apart.

As for how to detect blanks, when you read a line, it will be empty. String has a method for that.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2232
    
  47
As for how to detect blanks, when you read a line, it will be empty. String has a method for that.

And make sure you call trim() before checking if the line is empty in case there are any spurious whitespace characters in those seemingly blank lines.
Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
Everything is already in classes. To be specific, i have a Question class, and then a MultipleChoice subclass. The constructor for that subclass has 3 parts, a String for the text of the question, an ArrayList<String> for the right answers, and an ArrayList<String> for the wrong answers. I have read the entire file into its own arrayList already and i have been using that to construct the question classes. Is string.isEmpty the only way to detect a blank line?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ryan Callen wrote:Is string.isEmpty the only way to detect a blank line?


No, but it's the easiest. Why do you not want to do it that way?

It will be easier for people to help you if you just tell us what you're trying to do and what specific problems you're having. Doling out tidbits of information piecemeal is not real productive.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10246
    
    8

Ryan Callen wrote:Is string.isEmpty the only way to detect a blank line?

I would recommend you also consider what Tony suggested while using isEmpty(), else you are in for a surprise
Tony Docherty wrote:And make sure you call trim() before


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Scanner problem