Okay, so I have a text file that is essentially a course description.
Physics Applied SPH4C 2014
Description: Physics = course name Applied = course level SPH4C = course code 2014 = academic year
So, I have already tokenized this info so that each field is on a separate line like:
Physics
Applied
SPH4C
2014
Now, I want to convert each line into a different type since my Course class accepts different types.
Like this:
public Course(String name, String code, char level, int academicYear)
How do I convert the string level that I get from the text file into a 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)
this is what I have, but it basically just changes the string, which wont allow me to enter it as a level since the course only accepts a char as a level, or am I wrong.
Also, I need help with the academic level as well Also, how would I assign each line to a variable
Here is the 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')
};
but the compiler is telling me that a return type is required at the first put statement, any ways yo solve this
Here is the edited version
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;
}
the error message is incompatible types
Here is the whole class
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()