使用类在java中创建对象数组

时间:2015-12-07 00:50:40

标签: java arrays class

我正在开发一个项目,我有一个文本文件,第一行是我需要的数组大小,然后后续行按以下顺序包含课程信息:dept,num,标题。 (例如CSC 101 Basic Computing)我的代码符合但是当它运行时,数组中的第一个索引变为默认值(即没有),因此文本文件中的最后一行不会被存储或打印。我想知道如何解决这个错误。

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

public class Organizer {

    public static void main(String[] args) {
        Scanner fileScanner = null;
        String file;
        File f = null;

        //Create a Do While loop in order to prompt the user for a input file
        //and then continue prompting if the file entered does not exist.

        do {
            try {

                System.out.print("What is the name of the input file? ");
                Scanner inputReader = new Scanner(System.in);
                file = inputReader.nextLine();
                f = new File(file);
                fileScanner = new Scanner(new File(file));

                //Catch the exception and tell the user to try again
            } catch (FileNotFoundException e) {

                System.out.println("Error scanning that file, please try again.");

            }
        } while (!f.exists());

        //Testing the Make Array Method
        //System.out.print(makeArray(fileScanner));

        //Testing the print Array Method
        printArray(makeArray(fileScanner));

    }

    public static Course[] makeArray(Scanner s) {

        int arraySize = s.nextInt();
        String title = "";
        String dept = "";
        int num = 0;
        Course[] a = new Course[arraySize];
        for (int i = 0; i < a.length; i++) {

            a[i] = new Course(dept, num, title);
            String oneLine = s.nextLine();
            Scanner lineReader = new Scanner(oneLine);
            while (lineReader.hasNext()) {

                dept = lineReader.next();
                a[i].setDept(dept);
                num = lineReader.nextInt();
                a[i].setNum(num);
                while (lineReader.hasNext()) {
                    title = title + lineReader.next() + " ";
                }
                a[i].setTitle(title);
            }
            title = " ";
        }
        return a;
    }


    public static void printArray(Course[] arr) {

        for (int i = 0; i < arr.length; i++) {

            System.out.println(arr[i].toString());
        }
    }
}

这是我的另一堂课。

public static class Course {

    //INSTANCE VARIABLES
    private String dept = "";
    private int num = 0;
    private String title = "";

    //CONSTRUCTORS
    public Course(String dept, int num) {
        this.dept = dept;
        this.num = num;
    }

    public Course(String dept, int num, String title) {
        this.dept = dept;
        this.num = num;
        this.title = title;
    }

    public Course() {
        this.dept = "AAA";
        this.num = 100;
        this.title = "A course";
    }

    //SETTER AND GETTER METHODS
    public void setDept(String dept) {
        this.dept = dept;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDept() {
        return this.dept;
    }

    public int getNum() {
        return this.num;
    }

    public String getTitle() {
        return this.title;
    }

    //TOSTRING METHOD
    public String toString() {
        return dept + " " + num + ": " + title;
    }
}

2 个答案:

答案 0 :(得分:0)

不要忘记每次光标在扫描仪中的位置。 s.nextLine()将读取整行,然后光标将跳转到下一行,而s.nextInt()将从您的行读取一个int,然后保持在那里。它不会检查这是否是该行的最后一个输入。

只需将您的代码修改为:

int arraySize = s.nextInt();
s.nextLine();

你的代码应该运行得很好!

(另请考虑将a[i].setTitle(title);更改为a[i].setTitle(title.trim());,因为您的标题末尾将始终留有空白区域。)

答案 1 :(得分:0)

nextInt方法不使用新行字符。这是一个有效的解决方案:

public static Course[] makeArray(Scanner s){

    int arraySize = s.nextInt();
    String title = "";
    String dept = "";
    int num = 0;
    Course[] a = new Course[arraySize];
    s.nextLine();
    for (int i = 0; i < a.length; i++){

        a[i] = new Course(dept, num, title);
            String oneLine = s.nextLine();
            Scanner lineReader = new Scanner(oneLine);

            while (lineReader.hasNext()){

                dept = lineReader.next();
                a[i].setDept(dept);
                num = lineReader.nextInt();
                a[i].setNum(num);
                while (lineReader.hasNext()){
                    title = title + lineReader.next() + " ";
                }
                a[i].setTitle(title);
            }
            title = " ";
    }   
return a;

}