【发布时间】:2014-07-28 05:29:57
【问题描述】:
好的,所以我有一个本质上是课程描述的文本文件。
Physics Applied SPH4C 2014
说明: 物理 = 课程名称 应用 = 课程水平 SPH4C = 课程代码 2014 = 学年
所以,我已经标记了这个信息,以便每个字段都在单独的行上,例如:
Physics
Applied
SPH4C
2014
现在,我想将每一行转换为不同的类型,因为我的 Course 类接受不同的类型。
像这样:
public Course(String name, String code, char level, int academicYear)
如何将我从文本文件中获得的字符串级别转换为字符
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)
这就是我所拥有的,但它基本上只是改变了字符串,这不允许我将它作为一个级别输入,因为课程只接受一个字符作为一个级别,或者我错了。
另外,我还需要学术水平方面的帮助 另外,我如何将每一行分配给一个变量
这是哈希表
//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')
};
但是编译器告诉我在第一个 put 语句中需要返回类型,你可以通过任何方式解决这个问题
这是修改后的版本
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 并将其转换为相关的字符值吗?
-
你的数据成员级别被声明为一个字符串。这确实与 char 不兼容。
-
如果您希望简单地将字符串级别转换为字符,请修改 convertLevel 方法以返回一个值,而不是设置级别数据成员的值。
-
@IrishGeek82 这是因为我正在从文本文件中读取并希望将文本分配给其他类型的变量,例如 char
-
@IrishGeek82 我将如何修改它,很抱歉打扰你,你能在另一个线程中帮助我