私は、ファイルからコースをロードできるコースオーガナイザーを構築する必要があるプロジェクトに取り組んでいます。ファイルをプログラムにロードし、ファイル内の情報を配列に入れるのに少し問題があります。ファイル内の情報は、プログラム自体にも入力できるコース情報です。私の望みは、同じファイルに対して情報の読み取りと書き込みの両方ができるようにプログラムする必要があることです。私が間違っていることについてのヒントはありますか?私は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="";
}