Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    FL, USA
    Posts
    153
    Thanks
    13
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    FL, USA
    Posts
    153
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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));

    Last edited by mia_tech; 03-31-2009 at 05:26 AM.
    thanks
    ____________________
    www.pctechtips.org


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •