好吧,我得到一個文本文件,對它進行標記,然後嘗試將其寫入另一個文件。如何從文本文件中將文本轉換爲對象的字段
我有一類叫做當然它有4個參數:名稱,層次,代碼,一年
我劃分了文本文件的每一行到另一條線路
@Edit:應該提到這較早,但我已存儲在一個文本文件
public void readFromFile() throws IOException
{
int index = 0;
int j = -1;
int spaceCount = 0;
courseNumber++;
setCourseFields();
BufferedReader inputFile = new BufferedReader(new FileReader(INPUT_FILE));
PrintWriter outputFile = new PrintWriter(new FileWriter(OUTPUT_FILE));
String lineOfText = inputFile.readLine();
while (lineOfText != null)
{
outputFile.println(lineOfText);
lineOfText = inputFile.readLine();
for (char c : lineOfText.toCharArray())
{
if (c == ' ')
{
spaceCount++;
}
}
if(spaceCount == 1)
{
delimiter = DELIMITER_SPACE;
}
else if(spaceCount == 2)
{
delimiter = DELIMITER_TAB;
}
System.out.println("Course" + courseNumber + "\n");
// for each token in the string
while ((j = lineOfText.indexOf(delimiter, (index = ++j))) != -1)
{
System.out.println(courseField[k] + ": " + lineOfText.substring(index, j));
counter++;
k++;
}
// extract the last token
if (index > 0)
{
System.out.println("Year: " + lineOfText.substring(index));
++courseNumber;
}
if(k == 3)
{
k = k - k;
index = 0;
}
delayDisplay();
} // while(lineOfText != null)
inputFile.close();
outputFile.close();
}
3000門課程然後我會得到這樣的事情
Course1
Name: Computer Engineering Technology
Level: IB
Code: TEJ4M
Year: 2017
Course2
Name: Communications Technology
Level: Special Education
Code: TGJ2O
Year: 2002
每門課程都是獨一無二的,我需要幫助,以便我可以將上述每個字符串分配給一個變量。
每門課程有4個實例字段+一個唯一的序列號(課程編號)
的4個領域是:
String name;
char level;
String code;
int academicYear;
我要讓課程出每個4線的
這是我的課程班
public class Course extends Object implements Serializable
{
// class constants
public static final String DEFAULT_CODE = "AAA10";
public static final char DEFAULT_LEVEL = 'X';
public static final String DEFAULT_NAME = "unassigned";
public static final int DEFAULT_YEAR = 1900;
private static final char ACADEMIC_LEVEL = '1';
private static final char ENGLISH_LANGUAGE_LEARNERS_LEVEL = '9';
private static final int ERROR_CODE = -1;
private static final char IB_LEVEL = '7';
private static final int MAXIMUM_YEAR = 2014;
private static final int MINIMUM_YEAR = 1900;
private static final char SPECIAL_EDUCATION_LEVEL = '8';
// instance variables
private int academicYear;
private String code;
private static int counter = 0;
private char level;
private String name;
private int serialNumber;
/**
* Constructs a course with default characteristics.
*/
public Course()
{
this.academicYear = DEFAULT_YEAR;
this.code = DEFAULT_CODE;
this.level = DEFAULT_LEVEL;
this.name = DEFAULT_NAME;
serialNumber = ++counter;
} // end of constructor Course()
/**
* Constructs a course with the specified characteristics.
*
* @param name - the Ministry-assigned name of this course;
* ex: Introduction to Computer Science
* @param code - the Ministry-assigned code of this course; ex: ICS3U
* @param level - one of the enumerated levels ex: '7', '1', '9', '8'
* @param academicYear - a 4-digit year of the Gregorian calendar
* set between a minimum and maximum year
*/
public Course(String name, String code, char level, int academicYear)
{
if(name == null)
{
this.name = DEFAULT_NAME;
}
else
{
this.name = name;
} // end of if(name == null)
if(code == null)
{
this.code = DEFAULT_CODE;
}
else
{
this.code = code;
} // end of if(code == null)
if (level == IB_LEVEL || level == ACADEMIC_LEVEL
|| level == ENGLISH_LANGUAGE_LEARNERS_LEVEL
|| level == SPECIAL_EDUCATION_LEVEL)
{
this.level = level;
}
else
{
this.level = DEFAULT_LEVEL;
} // end of if (level == IB_LEVEL || level == ACADEMIC_LEVEL || level ==
ENGLISH_LANGUAGE_LEARNERS_LEVEL || level == SPECIAL_EDUCATION_LEVEL)
if (academicYear >= MINIMUM_YEAR && academicYear <= MAXIMUM_YEAR)
{
this.academicYear = academicYear;
}
else
{
this.academicYear = DEFAULT_YEAR;
} // end of (academicYear >= MINIMUM_YEAR && academicYear <= MAXIMUM_YEAR)
serialNumber = ++counter;
} // end of constructor Course(String name, String code, char level, int academicYear)
/* Comparison methods*/
/**
* Indicates whether another object has a state identical to this object’s state.
*
* @param otherCourse - the object whose state is compared to this object’s
*
* @return true if the other object has an identical state; otherwise false
*/
public boolean equals(Object otherCourse)
{
if(otherCourse == null) return false;
if (this.getClass() != otherCourse.getClass()) return false;
if (this == otherCourse) return true;
// Typecasting Object otherCourse into a Course to compare objects' states
Course course1 = (Course) otherCourse;
if(serialNumber != course1.getSerialNumber()) return false;
if(academicYear != course1.getYear()) return false;
if(level != course1.getLevel()) return false;
if(code != course1.getCode()) return false;
if(name != course1.getName()) return false;
// needed to satisfy the compiler
return true;
} // end of method boolean equals(Object course)
/**
* Indicates whether another object has a state identical to this object’s state,
* ignoring each object's unique serial number.
*
* @param otherCourse - the object whose state is compared to this object’s
*
* @return true if the other object has an identical state; otherwise false
*/
public boolean equalsIgnoreSerial(Object otherObject)
{
if(otherObject == null) return false;
if (this.getClass() != otherObject.getClass()) return false;
boolean courseEquals;
Course anotherCourse = (Course) otherObject;
// Ignore unique serial number of each course
if(this.serialNumber == anotherCourse.getSerialNumber()) return false;
if(this.academicYear != anotherCourse.getYear()) return false;
else courseEquals = true;
if(this.level != anotherCourse.getLevel()) return false;
else courseEquals = true;
if(this.code != anotherCourse.getCode()) return false;
else courseEquals = true;
if(this.name != anotherCourse.getName()) return false;
else courseEquals = true;
return courseEquals;
} // end of method boolean equalsIgnoreSerial(Object course
/**
* Compares this Course to another.
*
* @return a negative value, if this course should come before the other course;
* 0, if this course and the other course have equal states;
* a positive value if this course should come after the other course
*/
public int compareTo(Course otherCourse)
{
int before = -1;
int after = 1;
int equals = 0;
int resultCode = code.compareTo(otherCourse.getCode());
int resultName = name.compareTo(otherCourse.getName());
if(otherCourse == null) return -1;
if(this.equals(otherCourse)) return equals;
if(serialNumber < otherCourse.getSerialNumber()) return before;
if(serialNumber > otherCourse.getSerialNumber()) return after;
if(academicYear < otherCourse.getYear()) return before;
if(academicYear > otherCourse.getYear()) return after;
if(!(sortLevel(level) == -1 || sortLevel(otherCourse.getLevel()) == -1))
{
if(sortLevel(level) < sortLevel(otherCourse.getLevel())) return before;
if(sortLevel(level) > sortLevel(otherCourse.getLevel())) return after;
} // end of if(!(sortLevel(level) == -1 || sortLevel(otherCourse.getLevel()) ==
-1))
if(code.compareTo(otherCourse.getCode()) != 0) return resultCode;
if(name.compareTo(otherCourse.getName()) != 0) return resultName;
// neccessary to satisfy the compiler
return 5;
} // end of public int compareTo(Course otherCourse)
/* utility methods*/
public static int sortLevel(char level)
{
/*************************************
final char[] LEVEL = {'7', '1', '9', '8'};
for (int index = 0; index < LEVEL.length; index++)
{
if(LEVEL[index] == level) return index;
if(LEVEL[index] != level) return ERROR_IO_EXCEPTION;
} // end of for (int index = 0; index < LEVEL.length; index++)
// error code for not found, should not be reached
return -1;
****************************************/ //code taken from in class discussion
final char[] LEVEL = {'7', '1', '9', '8'};
for (int index = 0; index < LEVEL.length; index++)
{
if(LEVEL[index] == level) return index;
if(LEVEL[index] != level) return -1;
} // end of for (int index = 0; index < LEVEL.length; index++)
// error code for not found, should not be reached
return ERROR_CODE;
} // end of public static int sortLevel(char level)
/* accessors*/
/**
* Returns the code of this course.
*
* @returns Returns the code of this course
*/
public String getCode()
{
return code;
} // end of method getCode()
/**
* Returns the level of this course.
*
* @return the level of this course
*/
public char getLevel()
{
return level;
} // end of method getLevel()
/**
* Returns the name of this course.
*
* @return the name of this course.
*/
public String getName()
{
return name;
} // end of method getName()
/**
* Returns the unique serial number of this course.
*
* @return the unique serial number of this course.
*/
public int getSerialNumber()
{
return serialNumber;
} // end of method getSerialNumber()
/**
* Returns the academic year of this course.
*
* @return the 4-digit academic year of this course
*/
public int getYear()
{
return academicYear;
} // end of method getYear()
/* mutators */
/**
* Sets the code of this course.
*
* @param newCode - the new code of this course.
*/
public void setCode(String newCode)
{
if (newCode == null) return;
this.code = newCode;
} // end of method setCode(String newCode)
/**
* Sets the level of this course.
*
* @param newLevel - one of the enumerated levels
*/
public void setLevel(char newLevel)
{
if(newLevel == IB_LEVEL || newLevel == ACADEMIC_LEVEL || newLevel ==
ENGLISH_LANGUAGE_LEARNERS_LEVEL || newLevel == SPECIAL_EDUCATION_LEVEL)
{
level = newLevel;
}
else
{
level = DEFAULT_LEVEL;
} // end of if(newLevel == IB_LEVEL || newLevel == ACADEMIC_LEVEL || newLevel ==
ENGLISH_LANGUAGE_LEARNERS_LEVEL || newLevel == SPECIAL_EDUCATION_LEVEL)
} // end of method setLevel(char newLevel)
/**
* Sets the name of this course.
*
* @param newName - the new name of this course
*/
public void setName(String newName)
{
if (newName == null) return;
this.name = newName;
} // end of method setName(String newName)
/**
* Sets the academic year of this course.
*
* @param newYear - the new 4-digit academic year of this course
*/
public void setYear(int newYear)
{
if (newYear >= MINIMUM_YEAR && newYear <= MAXIMUM_YEAR)
{
this.academicYear = newYear;
}
else
{
academicYear = DEFAULT_YEAR;
} // end of if (newYear >= MINIMUM_YEAR && newYear <= MAXIMUM_YEAR)
} // end of method setYear(int newYear)
/**
* Returns a string representation of this course.
*
* @override toString in class Object
*
* @return a string representation of this course.
*/
public String toString()
{
return
this.getClass().getName()
+"["
+ "Serial Number: " + serialNumber
+ ", academic year: " + academicYear
+ ", level: " + level
+ ", code: " + code
+ ", name: " + name
+"]";
} // end of String toString()
而且,我需要幫助轉換字符串成水平和成學年一個int一個字符,任何想法
這裏是我CourseUtility類,這還沒有結束,
public class CourseUtility
{
// class constants
private static final String INPUT_FILE = "courses.text";
private static final String OUTPUT_FILE = "CoursesTokenized.text";
private static int counter = 0;
private static int courseNumber = 0;
private static int k = 0;
private static final String DELIMITER_SPACE = " ";
private static final String DELIMITER_TAB = "\t";
String delimiter = DELIMITER_TAB;
private static final String DEFAULT_LEVEL = "X";
String name = "";
String code = "";
String year = "";
String level = "";
String lineOfText;
private static String[] courseField = new String[5];
/**
* Constructor for objects of class CourseUtility
*/
public CourseUtility() throws IOException
{
}
public void readFromFile() throws IOException
{
int index = 0;
int j = -1;
int spaceCount = 0;
courseNumber++;
setCourseFields();
BufferedReader inputFile = new BufferedReader(new FileReader(INPUT_FILE));
PrintWriter outputFile = new PrintWriter(new FileWriter(OUTPUT_FILE));
String lineOfText = inputFile.readLine();
while (lineOfText != null)
{
for (char c : lineOfText.toCharArray())
{
if (c == ' ')
{
spaceCount++;
}
}
if(spaceCount == 1)
{
delimiter = DELIMITER_SPACE;
}
else if(spaceCount == 2)
{
delimiter = DELIMITER_TAB;
}
System.out.println("Course" + courseNumber);
// for each token in the string
while ((j = lineOfText.indexOf(delimiter, (index = ++j))) != -1)
{
System.out.println(courseField[k] + ": " + lineOfText.substring(index, j));
System.out.println("");
outputFile.println(lineOfText.substring(index, j));
counter++;
k++;
}
// extract the last token
if (index > 0)
{
System.out.println("Year: " + lineOfText.substring(index));
outputFile.println(lineOfText.substring(index));
++courseNumber;
}
// for each token in the string
// Course c = new Course(hm.get("Name"), hm.get("Code"),
hm.get("Level"), Integer.parseInt(hm.get("Year")));
// System.out.println(c);
if(k == 3)
{
k = k - k;
index = 0;
}
delayDisplay();
lineOfText = inputFile.readLine();
} // while(lineOfText != null)
inputFile.close();
outputFile.close();
}
public CourseUtility(String name, String code, String level, String year)
{
if(name == null)
{
this.name = Course.DEFAULT_NAME;
}
else
{
this.name = name;
} // end of if(name == null)
if(code == null)
{
this.code = Course.DEFAULT_CODE;
}
else
{
this.code = code;
} // end of if(code == null)
if(level == null)
{
this.level = DEFAULT_LEVEL;
}
else
{
this.level = level;
} // end of if(level == null)
if(year == null)
{
this.year = null;;
}
else
{
this.year = year;
} // end of if(year == null)
}
private void delayDisplay()
{
try
{
Thread.sleep(1000);
} catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
} // end of try
} // end of method void delayDisplay()
private void convertLevel(String courseLevel)
{
level = DEFAULT_LEVEL;
if(level == "IB") level = "7";
if(level == "Academic")level = "1";
if(level == "Applied") level = "1";
if(level == "ELL") level = "9";
if(level == "Special Education") level = "8";
} // end of method convertLevel(String courseLevel)
public void deleteCourse()
{
private void setCourseFields()
{
courseField[0] = "Name";
courseField[1] = "Level";
courseField[2] = "Code";
courseField[3] = "Year";
} // end of method setCourseFields()
}
這些不是參數。這些是你的類的屬性(或實例變量或屬性)。另外,不要int大寫。你的課程課有一個構造函數嗎? –
@EdwinTorres是的它可以 – user3709657
你可以發佈你的課程嗎?這將有助於查看構造函數,變量等。您還需要提供唯一的序列號。從哪裏來? –