如何从文件中填充ArrayList并格式化输出。

时间:2013-09-20 01:16:00

标签: java file-io arraylist formatting

我正在这里为学校做这个项目,我真的很困难。基本上我必须要一个文本文件。然后我必须用它里面的信息填充一个arraylist。之后我需要正确格式化,以便我可以将它用于分类,问题和答案。我不知道该怎么做,所以下面是教师问我们是否有助于进一步澄清它。

  

“在此作业中,您将编写一个模拟a的应用程序   琐事游戏。您将获得一个包含一组的文本文件   六个不同类别的问答。你的程序会   从每个类别中选择一个随机问题来回答   播放器。分数将在每次运行结束时显示。

     

输入数据格式:输入文件包含问题和答案   不同的类别。对于每个类别,第一行表示   类别的名称。这一行后面会有很多对   的线条。这一对的第一行是问题,第二行是问题   行是它的相应答案。一个空行分隔   类别。技术要求:

     

创建一个名为TriviaQuestion的类,代表一个典型的问题对象。

     

读取数据后,将信息存储在ArrayList(s)中。

     

输入/输出方面的用户友好性。忽略案件。如果可能,接受部分正确答案。“

现在我的代码将显示我仍然坚持如何以他指定的格式将信息添加到ArrayList。我不希望任何人为我做我的工作。我只需要朝着正确的方向轻推。我不知道每个类别是否需要很多arraylists,如果我这样做,我是否需要他们在单独的课程?这只是我的第二个学期,所以我仍然对这些事情感到非常困惑。还有其他jave琐事游戏在线和在这个论坛上,但我找不到任何这种方式的工作。无论如何,这里是代码和文本文件....

import java.io.File;
import java.util.ArrayList;


public class TriviaGame {

/**
 * I want to make several array lists and then use a random question for each         category. Then compare the user
 * answer to the real answer to see if they won. Keep a count of the correct answers. Ignore case. 
 */
//instance fields
private String player; // The player
private int points;        // Player's number of points
private String currentAnswer; // Current typed answer
private File gameFile = new File ("trivia.txt");


//constructors

public TriviaGame(String playerName)
{
    playerName = player;
    points = 0;
}


public TriviaGame(String player, int points, String currentAnswer,
        File gameFile, ArrayList<String> triviaQuestion) {
    super();
    this.player = player;
    this.points = points;
    this.currentAnswer = currentAnswer;
    this.gameFile = gameFile;
    }


//Getters and Setters
/**
 * @return the player
 */
public String getPlayer() {
    return player;
}


/**
 * @param player the player to set
 */
public void setPlayer(String player) {
    this.player = player;
}


/**
 * @return the points
 */
public int getPoints() {
    return points;
}


/**
 * @param points the points to set
 */
public void setPoints(int points) {
    this.points = points;
}


/**
 * @return the currentAnswer
 */
public String getCurrentAnswer() {
    return currentAnswer;
}


/**
 * @param currentAnswer the currentAnswer to set
 */
public void setCurrentAnswer(String currentAnswer) {
    this.currentAnswer = currentAnswer;
}


/**
 * @return the gameFile
 */
public File getGameFile() {
    return gameFile;
}


/**
 * @param gameFile the gameFile to set
 */
public void setGameFile(File gameFile) {
    this.gameFile = gameFile;
}


//To String Method

/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return "TriviaGame [player=" + player + ", points=" + points
            + ", currentAnswer=" + currentAnswer + ", gameFile=" +     gameFile
            + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
            + ", toString()=" + super.toString() + "]";
}




}

然后是测试人员......

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;



public class TriviaGamePlayer {

/**
 * @param args
 */
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    File gameFile = new File ("trivia.txt");
    ArrayList<String> triviaQuestion = new ArrayList<String>();

    Scanner infile = new Scanner(gameFile);
    String line;


    while(infile.hasNext()){
        line = infile.nextLine();
        triviaQuestion = line.split();

}

}


}

最后伴随它的一些文本文件.....

Arts & Literature
What are bongo drums traditionally held between for playing?
The knees
The Hugo Awards are given for the best literature in which genre?
Science fiction
What four-letter girl's name gave Jane Austen the title of a comic novel?
Emma
Andy Warhol was born in what US city?
Pittsburgh
Who wrote the novel "The Chocolate War"?
Robert Cormier
What surname for John in "The Importance of Being Earnest" did Oscar Wilde take from the seaside town he vacationed at?
Worthing

Geography
What is the modern day equivalent of Dacia?
Romania
What is the capital of Ontario?
Toronto
Which island boasts of being Napoleon's birthplace?
Corsica
What nationality is a Breton?
French
A person from North Carolina is properly known as a what?
North Carolinian
In which country would you find St. Basil's cathedral?
Russia

1 个答案:

答案 0 :(得分:0)

您需要一个ArrayList或TriviaQuestion对象

List<TriviaQuestion> triviaQuestions = new ArrayList<TriviaQuestion>();

以最简单的形式创建一个TriviaQuestion类,如

public class TriviaQuestion {
  String category;
  String question;
  String answer;
}

您可以像这样填充

public static void main(String[] args) throws IOException {
    File gameFile = new File("trivia.txt");
    List<TriviaQuestion> triviaQuestions = new ArrayList<TriviaQuestion>();
    Scanner infile = new Scanner(gameFile);
    String lastKnownCategory = "";

    while (infile.hasNextLine()) {
        String currentLine = infile.nextLine();

        if (!currentLine.isEmpty()) {
            TriviaQuestion currentQuestion = new TriviaQuestion();

            if (currentLine.endsWith("?")) {
                currentQuestion.category = lastKnownCategory;
                currentQuestion.question = currentLine;
                currentQuestion.answer = infile.nextLine();
            } else {
                currentQuestion.category = currentLine;
                currentQuestion.question = infile.nextLine();
                currentQuestion.answer = infile.nextLine();
                lastKnownCategory = currentLine;
            }
            triviaQuestions.add(currentQuestion);
        }
    }
}