好了,所以我有一個文本文件,它本質上是一個過程的描述。如何將文本轉換爲對象的字段
Physics Applied SPH4C 2014
說明: 物理=課程名稱 應用=課程級別 SPH4C =課程代碼 2014 =學年
所以,我已經符號化這個信息使每一個現場是在一個單獨的行像:
Physics
Applied
SPH4C
2014
現在,我想將每一行轉換爲不同的類型,因爲我的課程類接受不同類型。
像這樣:
public Course(String name, String code, char level, int academicYear)
如何轉換的串級,我從文本文件中獲取到一個char
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)
這是我有什麼,但它基本上只是改變了字符串,它不允許我將它作爲關卡輸入,因爲課程只接受字符作爲關卡,或者我錯了。
而且,我需要學術水平有所幫助 另外,我怎麼會每一行分配給一個變量
這裏是Hashtable
//This allows you to define your list with string keys instead of
//using a bunch of ifs.
Hashtable<String, char> CourseLevels = new Hashtable<String, char>()
{
put("IB", '7'),
put("Academic", '1'),
put("Applied", '1'),
put("ELL", '9'),
put("Special Education", '8')
};
但是編譯器告訴我一個返回類型是需要在第一次放進聲明,任何方式喲解決這個
下面是編輯後的版本
private char convertLevel(String courseLevel)
{
//This allows you to define your list with string keys instead of
//using a bunch of ifs.
Hashtable<String, Object> courseLevels = new Hashtable<String, Object>();
{
courseLevels.put("IB", '7');
courseLevels.put("Academic", '1');
courseLevels.put("Applied", '1');
courseLevels.put("ELL", '9');
courseLevels.put("Special Education", '8');
};
//Determine if the courseLevel exists in our list.
if (courseLevels.containsKey(courseLevel))
{
//Assuming level is defined as a char and not a string
//Yes it does, use it.
level = (char)courseLevels.get(courseLevel); // gives me an error
}
else
{
//if not use the default.
level = DEFAULT_LEVEL;
}
return level;
}
該錯誤消息是不兼容的類型
這裏是整體類
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++;
setCourseFieldDescription();
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)
private char convertLevel(String courseLevel)
{
//This allows you to define your list with string keys instead of
//using a bunch of ifs.
Hashtable<String, Object> courseLevels = new Hashtable<String, Object>();
{
courseLevels.put("IB", '7');
courseLevels.put("Academic", '1');
courseLevels.put("Applied", '1');
courseLevels.put("ELL", '9');
courseLevels.put("Special Education", '8');
};
//Determine if the courseLevel exists in our list.
if (courseLevels.containsKey(courseLevel))
{
//Assuming level is defined as a char and not a string
//Yes it does, use it.
level = courseLevels.get(courseLevel);
}
else
{
//if not use the default.
level = DEFAULT_LEVEL;
}
}
//
// //Assuming level is defined as a char and not a string
// if (CourseLevels.contains(courseLevel))
// {
// return CourseLevels.get(courseLevel);
// }
// else
// {
// return DEFAULT_LEVEL;
// }
public void setCourseFieldDescription()
{
courseField[0] = "Name";
courseField[1] = "Level";
courseField[2] = "Code";
courseField[3] = "Year";
} // end of method setCourseFields()
的聲明使用單引號字符。比如'7'。你想獲得courseLevel並將其轉換爲關聯的字符值? – IrishGeek82
您將數據成員級別聲明爲字符串。這實際上與char不兼容。 – IrishGeek82
如果您希望簡單地將字符串級別轉換爲char,請修改convertLevel方法以返回值,而不是設置級別數據成員的值。 – IrishGeek82