简体   繁体   中英

Trouble determining various elements in a text-file read in program (java)

I am having trouble determining how to split separate elements in a text file in a usable way. My program is meant read in a text file to an arraylist, display the possible answers, ask for an answer and move on only when a correct answer is given. The trouble is, the text files must have different numbers of answers, so there is no numerical way to determine when a question is found in the text file and how many questions there are. Here is an example of a quiz text file:

How many licks does it take to get to the tootsie roll center of a tootsie pop?( QUESTION )

4( NUMERICAL VALUE ASSIGNED TO CORRECT ANSWER )

( POSSIBLE ANSWERS )

one

two

three

four

What is your name?

3

Arthur, King of the Britons

Sir Lancelot the Brave

Sir Robin the Not-Quite-So-Brave-As-Sir Lancelot

Who's on first?

5

What

Why

Because

Who

I don't know

I hate asking you guys a broad logic question as such, but I'm really stuck on this. Here is my code:

import java.util.*;
import java.io.*;
public class JavaApplication8 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException{

    Scanner inScan = new Scanner(System.in);

    String file_name;
    System.out.print("What is the full file path name?\n>>");
    file_name = inScan.next();

    Scanner fScan = new Scanner(new File(file_name));
    ArrayList<String> Questions = new ArrayList();



    while (fScan.hasNextLine()) 
    {
        Questions.add(fScan.nextLine());
    }

    System.out.println(Questions.get(0));
    System.out.println(Questions.get(2));
    System.out.println(Questions.get(3));
    System.out.println(Questions.get(4));
    System.out.println(Questions.get(5));


    String guess;
    System.out.print("What is your answer?\n>>");
    guess = inScan.next();

    if (guess == Questions.get(1))
    {
        System.out.println("Correct");
    }

    else

        System.out.println("Incorrect");






}

**I did not code for the additional questions, only the first one.

It appears the only instance of a question mark ? is when there is a question. Test each line from the file to see if there is a question mark in that line. If so, then that's a new question/answer set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM