使用类在java中创建对象数组
我正在做一个项目,我有一个文本文件,第一行是我需要的数组的大小,接下来的行有课程信息,顺序如下:dept,num,title。(例如,CSC 101 Basic Computing)我的代码符合要求,但当它运行数组中的第一个索引时,将成为默认索引(即无),因此文本文件中的最后一行不会被存储或打印。我想知道怎样才能纠正这个错误使用类在java中创建对象数组,java,arrays,class,Java,Arrays,Class,我正在做一个项目,我有一个文本文件,第一行是我需要的数组的大小,接下来的行有课程信息,顺序如下:dept,num,title。(例如,CSC 101 Basic Computing)我的代码符合要求,但当它运行数组中的第一个索引时,将成为默认索引(即无),因此文本文件中的最后一行不会被存储或打印。我想知道怎样才能纠正这个错误 import java.util.Scanner; import java.io.*; public class Organizer { public stat
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());
}
}
}
不要忘记你的光标在扫描仪中的每一时刻。
s.nextLine()。它不会检查这是否是该行的最后一次输入
只需将代码修复为:
int arraySize = s.nextInt();
s.nextLine();
你的代码应该运行得很好
(也考虑更改<代码> A[i] .SiteTitle(title);< /COD>到<代码> A[i] .SiteTitle(title .Times);,因为在title的结尾总是留有一个空白空间)
< P> NEXTIN方法不消耗新行字符。以下是一个有效的解决方案:
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;
publicstaticcourse[]makeArray(扫描器){
int arraySize=s.nextInt();
字符串标题=”;
字符串dept=“”;
int num=0;
课程[]a=新课程[排列];
s、 nextLine();
for(int i=0;i
}
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;