尝试将input.txt文件用于Java

时间:2016-03-24 21:06:27

标签: java

我正在尝试通过打印到output.txt文件来测试我的应用程序。有一个input.txt文件,其中包含四名荣誉学生,至少两名具有相同的GPA为3.9,其中三名不是学生。结果应该发送到output.txt文件。 output.txt文件应包含: 1)所有学生 2)最好的学生 3)名单中的荣誉学生人数 4)表彰学生 我创建的input.txt文件包含以下(按顺序)姓氏,名字,ID,GPA和年份。

TestStudents类打印input.txt文件。但是,我需要它来利用input.txt文件来打印上面提到的output.txt文件。非常感谢你。

学生班 -

public class Student
{
    String lastName, firstName, id;
    double gpa;
    int year;

    public Student (String lastName, String firstName, String id, 
                    double gpa, int year)
    {
        this.lastName = lastName;
        this.firstName = firstName;
        this.id = id;
        this.gpa = gpa;
        this.year = year;
    }

    public String toString()
    {
        return this.lastName + ", " + this.firstName + ": " + this.id + " "
               + this.gpa + " " + this.year;
    }

    public double getGPA()
    {
        return gpa;
    }

    public boolean isBetter (Student s)
    {
        return (this.gpa > ((Student)s).getGPA());
    }

    public boolean isHonors()
    {
        if (this.gpa >= 3.5)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

CS152课程 -

import java.util.*;
import java.io.*;
public class CS152
{
    public static final int MAXSIZE = 22;
    private static int size = 0;

    public static Student[] createList (Scanner scan) throws IOException
    {       
        Student[] list = new Student [MAXSIZE];
        return populateList (list, scan);
    }

    private static Student[] populateList (Student[] list, Scanner scan)
    {
        Student s;
        if (size < MAXSIZE && scan.hasNext())
        {
            s = new Student (scan.next(), scan.next(), scan.next(),
                             scan.nextDouble(), scan.nextInt());
            list[size] = s;
            size++;
            System.out.println (s);
            return populateList (list, scan);
        }
        else
        {
            return list;
        }
    }

    public static int getSize()
    {
        return size;
    }

    // Returns String of all students. Variable n is actual size of the list.
    // Assume that n is positive. Recursive code.
    public static String toString (Student[] list, int n)
    {
        String s = " ";
        if (n == 1)
        {
            return s += list[0];
        }
        else
        {
            s += list[n].toString() + "\n";
            s += "\n";
        }
        return s + toString (list, n - 1);
    }

    // Returns the best student. Must use method isBetter in the code.
    // Variable n is actual size of the list. Assume that n is positive.
    public static Student findBestStudent (Student[] list, int n)
    {
        if (n == 1)
        {
            return list[0];
        }
        else if (list[n].isBetter (list[n - 1]))
        {
            return list[n];
        }
        else
        {
            return findBestStudent (list, n - 1);
        }        
    }

    // Returns the number of honor students in the list.
    // Must call the method isHonors(). Variable n is actual size of the list.
    // Assume that n is positive.
    public static int countHonors (Student[] list, int n)
    {
        if (n == 0)
        {
            return 0;
        }
        else if (list[n].isHonors())
        {
            return 1 + countHonors (list, n - 1);
        }
        else
        {
            return countHonors (list, n - 1);
        }
    }

    static ArrayList<Student> studentsList = new ArrayList<Student>();
    public static ArrayList <Student> honorsStuds (Student[] list, int n)
    {
        if (n == 0)
        {
            return studentsList;
        }
        else
        {
            boolean currentIsHonors = list[n - 1].isHonors();
            if (currentIsHonors)
            {
                studentsList.add(list[n - 1]);
                return honorsStuds (list, n - 1);
            }
            else
            {
                return honorsStuds (list, n - 1);
            }
        }
    }
}

TestStudents类 -

import java.io.*;
import java.util.*;
public class TestStudents
{
    public static void main (String[] args) throws IOException
    {
        File input = new File ("input.txt");
        Scanner scan = new Scanner (input);
        Student[] studentArray = CS152.createList (scan);
    }
}

我将FileWriter合并到TestStudents类中。现在显示所有学生的列表。我仍然难以尝试调用方法findBestStudent,countHonors和honorsStuds并将它们实现到TestStudents中。以下是修订后的TestStudents类:

TestStudents类 -

import java.io.*;
import java.util.*;
public class TestStudents
{
    public static void main (String[] args) throws IOException
    {
        File input = new File ("input.txt");
        Scanner scan = new Scanner (input);
        System.out.println ("All students: ");
        Student[] studentArray = CS152.createList (scan);


        File output = new File ("output.txt");
        FileWriter fWriter = new FileWriter (output);
        PrintWriter pWriter = new PrintWriter (fWriter);
        pWriter.println (input);
        pWriter.close();    
    }
}

1 个答案:

答案 0 :(得分:0)

要写入文件,您需要FileWriter

使用FileWriter和Try-with-resources,用法看起来像这样:

try(FileWriter w = new FileWriter(new File("output.txt"))) {
    w.append("Some string");
} catch (IOException ex) {
    Logger.getLogger(Output.class.getName()).log(Level.SEVERE, null, ex);
}

如果您不使用try-with-resources,请务必close() Writer以确保资源不会泄漏。实际上,您还应该确保关闭Scanner,因为保持未关闭会泄漏资源。

将来ask one question per post

要访问学生,您只需要从数组中读取它们。

System.out.println(studentArray[0].getGPA()); // prints the GPA of the first student
for (int i=0; i<CS152.getSize(); i++) {
    System.out.println(studentArray[i]); // prints every Student
}

[[注意,这个设计有一个带有null元素的长数组,CS152.class告诉你有多少被填充是糟糕的设计。我会让读取过程返回List <Student>,它管理自己的长度。鉴于该课程的名称是CS152,但这可能是由CS-152的老师给你的,或者之前完成的,所以我会用你所拥有的。]]