数组读数问题

时间:2014-11-13 21:34:35

标签: java arrays arraylist

美好的一天。我有以下几部分代码,我正在努力工作。我有一个带有问题的文本文件,它的结构方式是文本文件中的每个问题占用10行但我只想显示每个问题中的6个第一行然后隐藏剩余的。

我在texfile中的问题如下:

Collections
Which of these is not an example of a "real-life" collection?
a. The cards you hold in a card game.
b. Your favorite songs stored in your computer.
c. The players on a soccer team.
d. The number of pages in a book. 

d. /// answer


Multithreading
Indefinite postponement is often referred to as __________.
a. deadlock.
b. indigestion.
c. starvation.
d. None of the above.

c. /// answer

基本上它只是显示问题而不是答案。那是我到目前为止想要实现的目标。

任何帮助将不胜感激。

File filing = new File(file);


        pattern.setFileName(filing.getAbsolutePath());

        if(filing.getAbsoluteFile().exists())
        {
            try{

            ReadFile myFile = new ReadFile(pattern.getFileName());

            String[ ] aryLines = myFile.OpenFile( );

             int i;


            for ( i=0; i < aryLines.length; i++ ) 
                {
                    System.out.println( aryLines[ i ] ) ;

                    if(i == 6)
                        i = i+3;
                }

ReadFile类

import java.io.IOException;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile 
{
    private String path;

     ///////////// set file name

    public ReadFile(String filePath) 
    {
        path = filePath;
    }

    public String[] OpenFile() throws IOException
    {
        FileReader fr = new FileReader(path);

        BufferedReader textReader = new BufferedReader(fr);


        int numberOflines = path.length();

        String[] textData = new String[numberOflines];

        int count;

        for(count = 0; count < numberOflines; count++)
        {
            textData[count] = textReader.readLine();


        }

        textReader.close();
        return textData;
    }

    int readLines() throws IOException
    {
        FileReader fileToRead = new FileReader(path);

        BufferedReader br = new BufferedReader(fileToRead);

        String aLine;

        int numberOfLines = 0;

        while ( ( aLine = br.readLine( ) ) != null ) 
            {
                numberOfLines++;
            }

            br.close();

            return numberOfLines;
    }
}

2 个答案:

答案 0 :(得分:2)

您可以尝试迭代嵌套for循环:

    for (int i = 0; i < aryLines.length; i += 10) {
        // every iteration brings you to the beginning of a new question
        for (int j = 0; j < 6; j++) {
            System.out.println(aryLines[i + j]);
        }
    }

答案 1 :(得分:1)

您实际上甚至不需要ReadFile类来读取文件中的所有行。 Java在Files包中为您提供java.nio.file实用程序类,其中包含readAllLines(Path)方法。另外,注释中已经提到的i==6将只允许您处理第六行,但它不会处理第16行,第26行。要处理这些情况,您可以创建两个循环,例如gutenmorgenuhu's answer

for (int questionNumber; ...)
   for (1..6 lines of each question)
       printLine

或只检查最后一位是6。要获得最后一位数字,您可以使用模数(提醒)运算符%,以便您可以将条件重写为if ( i % 10 == 6)

从你的问题处理问题的简化代码很少看起来像

List<String> lines = Files.readAllLines(Paths.get("input.txt"));
// or in Java 7
// List<String> lines = Files.readAllLines(Paths.get("input.txt"),StandardCharsets.UTF_8);

for (int i = 0; i < lines.size(); i++) {
    System.out.println(lines.get(i));
    if (i % 10 == 6)
        i = i + 3;// or +4 if you don't want to separate questions with empty line
}