我正在开展一个项目,我必须建立一个可以从文件加载课程的课程组织者。将文件加载到程序中,并将文件中的信息放入数组中,我遇到了一些麻烦。文件中的信息只是课程信息,也可以在程序本身中输入。我希望程序能够同时读取和写入同一文件的信息。关于我做错了什么的提示?我对java中的读/写不太熟悉。非常感谢任何帮助。
以下是我收到的错误:
C:\java\bin>javac CourseOrganizer.java
CourseOrganizer.java:248: missing method body, or declare abstract
private static void LoadCourses();
^
CourseOrganizer.java:253: cannot resolve symbol
symbol : class ListCourses
location: class CourseOrganizer
ListCourses lines = new ArrayList(ListCourses);
^
2 errors
以下是代码:
import java.io.*;
import java.util.*;
public class CourseOrganizer
{
static ArrayList ListCourses = new ArrayList();
public static void main(String[] args)
{
InputStreamReader inp = null;
BufferedReader input = null;
int nOption = 0;
try
{
inp = new InputStreamReader(System.in);
input = new BufferedReader(inp);
while(true)
{
System.out.println("1. Add Courses");
System.out.println("2. Modify Courses");
System.out.println("3. Delete Courses");
System.out.println("4. Sort Courses");
System.out.println("5. Show all Courses");
System.out.println("6. Load Courses");
System.out.println("7. Exit.");
System.out.println("\nChoose an option(1-6) >> ");
nOption = Integer.parseInt(input.readLine());
switch(nOption)
{
case 1:
AddCourses(input);
break;
case 2:
ModifyCourses(input);
break;
case 3:
DeleteCourses(input);
break;
case 4:
SortCourses(input);
break;
case 5:
ShowAllCourses();
break;
case 6:
System.out.println("Exiting program. Press any key to continue....");
input.read();
System.exit(0);
break;
}
}
}
catch(Exception exp)
{
}
}
//Setup Add Courses function for case 1
private static void AddCourses(BufferedReader input) throws IOException
{
CourseTemplate tmpObject = null;
while(true)
{
tmpObject = new CourseTemplate();
System.out.println("Course Name >> ");
tmpObject.CourseName = input.readLine().toString();
System.out.println("Day of Course "+tmpObject.CourseName+"(1...6) >> ");
tmpObject.Day = input.readLine().toString();
System.out.println("Time of Course Start "+tmpObject.CourseName+" (in HH:MM AM/PM format) >> ");
tmpObject.Time = input.readLine().toString();
System.out.println("Course Mark "+tmpObject.CourseName+"(0...100) >>");
tmpObject.Mark = input.readLine().toString();
if(tmpObject != null)
ListCourses.add(tmpObject);
System.out.println("\n Do you want to add another Course?(y/n) >>");
if(!input.readLine().toLowerCase().equals("y"))
break;
}
}
private static void ModifyCourses(BufferedReader input) throws IOException
{
CourseTemplate tmpObject = null;
System.out.println("Name of the Course to modify >> ");
String OldCourseName = input.readLine();
int index = getCourseIndexByName(OldCourseName);
if(index == -1)
{
System.out.println(" Course " + OldCourseName+ " not found.");
}
else
{
tmpObject = (CourseTemplate)ListCourses.get(index);
showCourse(tmpObject);
System.out.println("What you want to modify (Name|Day|Time|Mark)? >>");
String strOption = input.readLine();
if("name".equals(strOption.toLowerCase()))
{
System.out.println("New Name of the Course >> ");
tmpObject.CourseName = input.readLine().toString();
}
else if("day".equals(strOption.toLowerCase()))
{
System.out.println("New Day of the Course "+tmpObject.CourseName+"(1...6) >> ");
tmpObject.Day = input.readLine().toString();
}
else if("mark".equals(strOption.toLowerCase()))
{
System.out.println("New Mark of the Course "+tmpObject.CourseName+"(0...100) >> ");
tmpObject.Day = input.readLine().toString();
}
else if("time".equals(strOption.toLowerCase()))
{
System.out.println("New Time of the Course "+tmpObject.CourseName+" (in HH:MM AM/PM format) >> ");
tmpObject.Time = input.readLine().toString();
}
else
{
System.out.println("Unable to locate the propety entered..");
}
ListCourses.set(index, tmpObject);
}
}
private static int getCourseIndexByName(String Name)
{
int index = -1;
CourseTemplate tmp =null;
for(int i=0;i<ListCourses.size();i++)
{
tmp = (CourseTemplate)ListCourses.get(i);
if(tmp.CourseName.toLowerCase().equals(Name.toLowerCase()))
{
index = i;
break;
}
}
return index;
}
private static void showCourse(CourseTemplate tcourse)
{
System.out.println(tcourse.CourseName+"\t\t"+tcourse.Day+"\t"+tcourse.Time+"\t"+tcourse.Mark);
}
private static void DeleteCourses(BufferedReader input) throws IOException
{
System.out.println("Name of the Course to delete >> ");
String OldCourseName = input.readLine();
int index = getCourseIndexByName(OldCourseName);
if(index == -1)
{
System.out.println(" Course " + OldCourseName+ " not found.");
}
else
{
ListCourses.remove(index);
System.out.println(" Course " + OldCourseName+ "deleted successfully.");
}
}
private static void SortCourses(BufferedReader input) throws IOException
{
System.out.println("Enter the key to sort (Name|Day|Time|Mark)? >>");
String strOption = input.readLine();
int nSize = ListCourses.size();
String str1, str2;
if("name".equals(strOption.toLowerCase()))
{
for(int i = 0;i<nSize;i++)
{
for(int j = (i+1);j<nSize;j++)
{
str1 = ((CourseTemplate)ListCourses.get(i)).CourseName;
str2 = ((CourseTemplate)ListCourses.get(j)).CourseName;
if(str1.compareToIgnoreCase(str2) > 0)
{
CourseTemplate tmp = (CourseTemplate) ListCourses.get(i);
ListCourses.set(i, (CourseTemplate) ListCourses.get(j));
ListCourses.set(j, tmp);
}
}
}
}
else if("day".equals(strOption.toLowerCase()))
{
for(int i = 0;i<nSize;i++)
{
for(int j = (i+1);j<nSize;j++)
{
str1 = ((CourseTemplate)ListCourses.get(i)).Day;
str2 = ((CourseTemplate)ListCourses.get(j)).Day;
if(str1.compareToIgnoreCase(str2) > 0)
{
CourseTemplate tmp = (CourseTemplate) ListCourses.get(i);
ListCourses.set(i, (CourseTemplate) ListCourses.get(j));
ListCourses.set(j, tmp);
}
}
}
}
else if("time".equals(strOption.toLowerCase()))
{
for(int i = 0;i<nSize;i++)
{
for(int j = (i+1);j<nSize;j++)
{
str1 = ((CourseTemplate)ListCourses.get(i)).Time;
str2 = ((CourseTemplate)ListCourses.get(j)).Time;
if(str1.compareToIgnoreCase(str2) > 0)
{
CourseTemplate tmp = (CourseTemplate) ListCourses.get(i);
ListCourses.set(i, (CourseTemplate) ListCourses.get(j));
ListCourses.set(j, tmp);
}
}
}
}
else if("mark".equals(strOption.toLowerCase()))
{
for(int i = 0;i<nSize;i++)
{
for(int j = (i+1);j<nSize;j++)
{
str1 = ((CourseTemplate)ListCourses.get(i)).Mark;
str2 = ((CourseTemplate)ListCourses.get(j)).Mark;
if(str1.compareToIgnoreCase(str2) > 0)
{
CourseTemplate tmp = (CourseTemplate) ListCourses.get(i);
ListCourses.set(i, (CourseTemplate) ListCourses.get(j));
ListCourses.set(j, tmp);
}
}
}
}
else
{
System.out.println("Unable to locate the text entered..");
}
ShowAllCourses();
}
private static void LoadCourses();
public String[] readLines(String filename) throws IOException {
FileReader fileReader = new FileReader("Courses.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
ListCourses lines = new ArrayList();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
private static void ShowAllCourses()
{
System.out.println("Course List\n");
System.out.println("Name\t\tDay\tTime\tMark");
for(int i=0;i<ListCourses.size();i++)
{
showCourse((CourseTemplate)ListCourses.get(i));
}
}
}
class CourseTemplate
{
public String CourseName = "";
public String Day = "";
public String Time="";
public String Mark="";
}
答案 0 :(得分:0)
你已经宣布了
static ArrayList ListCourses = new ArrayList();
所以 ListCources 实际上是 ArrayList 对象的名称。
现在,在 readLines 方法中,您有:
ListCourses lines = new ArrayList();
这是没有意义的,因为 ListCources 是变量名而不是对象类型。要纠正此问题,必须将上述行更改为:
ArrayList lines = new ArrayList();
因此,您的方法将如下所示:
public String[] readLines(String filename) throws IOException {
FileReader fileReader = new FileReader("Courses.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
ArrayList lines = new ArrayList();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
我可以建议您使用特定类型声明 ArrayList 。这将有助于编译器查明未来的问题,通常是一种更好的做法。
ArrayList<String> lines = new ArrayList<String>();
修改强>
在 readLines() 方法之前,您已声明:
private static void LoadCourses();
此方法没有正文,因此编译器会抱怨。如果您不需要,请将其删除或实施。
答案 1 :(得分:0)
hei朋友,我还没有看到你把列表中的课程信息保存到磁盘上,你从不使用OutPutStream或BufferedWriter,所以没有文本文件,你怎么能用FileReader读取Courses.txt?(你应该在第一个创建并保存它... ...其次,我看到,在开关结构中,“case 6”与你的屏幕消息不同,供用户选择,是吗?