Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-30-2009, 01:48 AM   PM User | #1
mia_tech
Regular Coder

 
Join Date: Jul 2007
Location: FL, USA
Posts: 153
Thanks: 13
Thanked 0 Times in 0 Posts
mia_tech can only hope to improve
parsing file with scanner...

guys, how could I parse a file that contains answers and questions in the form of:

Which of the following types are supertypes of Rectangle?
A) PrintStream
B) Shape
C) RectangularShape
D) Object
E) String
Select all that apply:
answer: BCD

my intent is to separate the question from the answer and put them both in an array that takes in question, answers, but I'm used to work with BufferedReader instead of Scanner which is the case here.... this is what I have so far, but is not doing the job
PHP Code:
public void read(Scanner inthrows IOException
    
{
       
String answers "answer:";
        while(
in.hasNextLine())
        {
            while(
in.nextLine())
            if(!
in.hasNext(answers))
            {
                
in.nextLine();//getting the question
            
}
            else
            {
                
in.useDelimiter("answer:");
                
in.next();//getting the answer
            
}
       
addQuestion(new Question(qa));
   } 
__________________
thanks
____________________
www.pctechtips.org
mia_tech is offline   Reply With Quote
Old 03-30-2009, 06:22 PM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
Once you have the line of answers, you could set up a loop to go over each character in that String.

Code:
answers = answers.trim();

for (int x = 0 ; x < answers.length ; ++x)
{
    switch (answers.charAt(x))
    {
        case 'A': //do PrintStream Logic
    }
}
This should work in principal, might have to play a bit.
brad211987 is offline   Reply With Quote
Old 03-31-2009, 04:23 AM   PM User | #3
mia_tech
Regular Coder

 
Join Date: Jul 2007
Location: FL, USA
Posts: 153
Thanks: 13
Thanked 0 Times in 0 Posts
mia_tech can only hope to improve
thanks finally got it working... did something like this
PHP Code:
while(in.hasNext)
{
line in.nextLine();
if(!
line.contains("answer:"))
   {
      
question += //adding question here
    
}
if(
line.contains("answer:"))
   {
       
answer //add answer here
    
}
addQuestion(new Question(questionanswer));

__________________
thanks
____________________
www.pctechtips.org

Last edited by mia_tech; 03-31-2009 at 04:26 AM..
mia_tech is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:46 PM.


Advertisement
Log in to turn off these ads.