-1

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()
  • 2
    You declare characters using single quotes. For instance '7'. Are you wanting to take courseLevel and convert that to the associated character value? – IrishGeek82 Jun 06 '14 at 20:56
  • you data member level is declared as a string. Which is, indeed, incompatible with char. – IrishGeek82 Jun 06 '14 at 23:34
  • If you are wishing to simply convert your string level into a char, modify the convertLevel method to return a value instead of setting the value of the level data member. – IrishGeek82 Jun 06 '14 at 23:38
  • @IrishGeek82 that is becuase I am reading from a text file and want to assign text to variables that re other types like char – user3709657 Jun 06 '14 at 23:39
  • @IrishGeek82 how would I modify it, sorry for bothering you about it, could you help me in the other thread – user3709657 Jun 06 '14 at 23:40

4 Answers4

0

Changing a 1-character to String to a char type can be done with:

myString.charAt(0);
Andrew
  • 617
  • 2
  • 6
  • 19
0

to convert a String to a char you should use

"7".charAt(0); //first example;

you may have a look at this

In Java how does one turn a String into a char or a char into a String?

last but not least if you are comparing strings to each other you may not use the equality operator as i suppose you are checking the strings having the same values.

For that you should use the equals method. I recommend having a look at :

How do I compare strings in Java?

For further information try the official documentation of java.lang.String

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Hope this helps :)

Community
  • 1
  • 1
herby
  • 389
  • 3
  • 8
0

Just pass level.charAt(0) while calling the function.

Mayank Agarwal
  • 376
  • 2
  • 9
0

If your course level as indicated in the text file is SPH4C then you cant put it an a single char just change the char level in the paramater as String.

When you comparing two string then can use the equals method of the string

example:

if(level.equals("IB")) level = "7";
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63