【问题标题】:File IO Help - Reading in文件 IO 帮助 - 读入
【发布时间】:2016-10-11 10:05:47
【问题描述】:

我编写了一段代码,它可以读取包含此信息的 csv 文件。它包含学生姓名和考试成绩。

克里斯托弗·李,54.0

斯坦利·赖特,90.5

奥利弗·斯图尔特,75.8

Jessica Chang,34.65

读入文件时,文件名由用户作为字符串输入。 该文件的内容必须同时存储在两个数组中,其中学生姓名将存储为字符串,而考试成绩将存储为实数。我有一个包含两个选项的选择菜单,显示标记和退出。 When "Display Marks" is selected the user inputs the name of the student and the program locates the index number of the name in the array and then the program prints out that same index number but in the array containing the test marks, thus printing out相应的测试标记。

我已经完成了大部分代码,但我需要一些帮助来解决它并修复 IO 逻辑。我是文件 IO 的新手。谢谢

代码如下:

import java.util.*;
import java.io.*;

public class MarksIO
{

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String fileName;

        fileName = sc.nextLine();
        boolean valid;
        valid = false;
        while (valid)
        {
            int choice = 0;
            System.out.println("Select an Option: ");
            System.out.println("Display Mark = 1.\nExit = 2.");
            if (choice == 1)
            {
                System.out.println("Enter the name of the Student.");
                String name;
                name = sc.nextLine();
                int lines = getNumLines(fileName);
                String[] arrayString = new String[lines];
                double[] arrayReal = new double[lines];
                readFile(arrayString, arrayReal, fileName);
                System.out.println("Test Mark is: " + arrayReal[index]);
            }
            else if (choice == 2)
            {
                System.exit(0);
            }
        }

    }

    public static int indexCalc(String name, double[] arrayReal,
            String[] arrayString)
    {
        for (int index = 0; index < arrayString.length; index++)
        {
            if (arrayString[index].equals(name))
            {
                return index;
            }
        }
    }

    public static void readFile(String fileName, double[] arrayReal,
            String[] arrayString)
    {
        FileInputStream fileStream = null;
        InputStreamReader reader;
        BufferedReader bufferedReader;
        int lineNum;
        String line;

        try
        {
            fileStream = new FileInputStream(fileName);
            reader = new InputStreamReader(fileStream);
            bufferedReader = new BufferedReader(reader);

            for (int i = 0; i <= arrayString.length; i++)
            {
                lineNum = 0;
                line = bufferedReader.readLine();
                while (line != null)
                {
                    lineNum++;
                    arrayString[i] = processString(line);
                    arrayReal[i] = processReal(line);
                }
            }
            fileStream.close();
        }
        catch (IOException ex)
        {
            if (fileStream != null)
            {
                try
                {
                    fileStream.close();
                }
                catch (IOException ex2)
                {

                }
                System.out.println("Error in file processing: "
                        + ex.getMessage());
            }

        }

    }

    public static String processString(String line, String[] arrayString)
    {
        String[] stringArray = line.split(",");
        String string = stringArray[0];
        return string;
    }

    public static double processReal(String line, double[] arrayReal)
    {
        double[] realArray = line.split(",");
        double real;
        real = (double)(realArray[1]);
        return real;

    }

    public static int getNumLines(String fileName)
    {
        FileInputStream fileStream;
        InputStreamReader reader;
        BufferedReader bufferedReader;
        try
        {
            fileStream = new FileInputStream(fileName);
            reader = new InputStreamReader(fileStream);
            bufferedReader = new BufferedReader(reader);
        }
        catch (FileNotFoundException e)
        {
            System.out.println("Error in file processing: " + e.getMessage());
        }
        int numLines;
        numLines = 0;
        int lineNum = 0;
        String line = bufferedReader.readLine();
        while (line != null)
        {
            lineNum++;
            numLines = numLines + 1;
            line = bufferedReader.readLine();

        }

    }

}

【问题讨论】:

  • 你的代码连编译都没有?
  • 您有一些编译错误,例如 double[] realArray = line.split(",");String.split(...) 返回 String[] 而不是 double[])和 System.out.println(... arrayReal[index]);(在此语句所在的范围内没有 index 变量是)。

标签: java arrays io try-catch


【解决方案1】:

您可以使用 Scanner 类来完成这项工作。喜欢

import java.io.*;
import java.util.Scanner;

public class ScanXan {
    public static void main(String[] args) throws IOException {

        Scanner s = null;

        try {
            s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));

            while (s.hasNext()) {
                System.out.println(s.next());
            }
        } finally {
            if (s != null) {
                s.close();
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-24
    • 2017-01-03
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多