Results 1 to 15 of 15
  1. #1
    elkalibre is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Post convert into its equivalent character??

    ok so im working on this homework, but i cant seem to understand one of the requirements.

    so the program requires me to input a student id number and the student course. Simple to tally how much students exist in each given course.
    it requires 3 methods, this is one of them.

    convertCourse – converts the course, taken as parameter, into its equivalent character value as follows:
    BLIS -> L
    BSCS -> C
    BSIS -> S
    BSIT -> T

    NOTE: BLIS, BSCS,BSIS, and BSIT are the courses. Can anyone explain this to me?

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: convert into its equivalent character??

    Hi elkalibre, welcome.

    It sounds like a simple switch statement is all that is required. As of JDK 7 String is accepted as an input to a switch statement.
    If you are using an older version then a simple if else statement would do the job.

    Regards.

  3. #3
    elkalibre is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: convert into its equivalent character??

    Quote Originally Posted by Ronin View Post
    Hi elkalibre, welcome.

    It sounds like a simple switch statement is all that is required. As of JDK 7 String is accepted as an input to a switch statement.
    If you are using an older version then a simple if else statement would do the job.

    Regards.



    yeah earlier i tried using switch statement, but im not quite sure if im doing it right.

    public static boolean convertCourse() {

    char courseChar;

    switch (courseChar){
    case "BLIS":
    courseChar = 'L';
    break;
    case "BSCS":
    courseChar = 'C';
    break
    }
    pls dont laugh, as im new to this. lol. My teacher said something about it so that when i need to input multiple info, i dont need to type like BLIS BSCS multiple times, instead, i input the characters equivalent to the course. hope its clear. i admit, im an idiot for not asking my teacher to explain further.

    another thing. does it mean... i should input "L" instead of "BLIS"?? or the other way around?
    Last edited by elkalibre; 04-15-2013 at 03:04 PM.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: convert into its equivalent character??

    From the looks of the requirement the method accepts the course name as a parameter and should return a char. Where you have previously defined 'courseChar' as the parameter for the switch statement this should instead be the string which has been passed in. The value returned is 'courseChar' which is type char. This type should also appear in the method signature.

    For more information:
    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Regards.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: convert into its equivalent character??

    Nobody here is going to laugh, to allow others to ask for help and assistance is the purpose of forums such as this.
    In answer to your earlier question, it looks like you are required to convert a string input to a char but I can't see the harm in providing two methods, one to convert from string to char and one to convert char to string.

    Regards.

  6. #6
    elkalibre is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: convert into its equivalent character??

    updated the code. should it be like this instead??
    public static boolean convertCourse(char courseChar) {

    String course;

    switch (courseChar){
    case "L":
    course = "BLIS";
    break;
    case "C":
    course = "BSCS";
    break;
    }

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: convert into its equivalent character??

    Your cases are defined as strings as opposed to chars.

    "L" = string
    'L' = char

    Regards.

  8. #8
    elkalibre is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: convert into its equivalent character??

    Quote Originally Posted by Ronin View Post
    Your cases are defined as strings as opposed to chars.

    "L" = string
    'L' = char

    Regards.


    Thanks. I got rid of the errors in that method. :)

    another thing. here is another one of the requirements:

    inputStudInfo – asks the user for the ID number and course. It shall repeatedly ask for a valid ID number (non-negative integer value) if an invalid input is entered. Input for a valid course will also be asked repeatedly by this method until a valid value (see above for the list of accepted courses) has been entered by the user. This method shall return the valid course entered by the user or shall return the word END if the user entered 0 as input for the ID number.
    and here is my code for that.
    public static boolean inputStudInfo(int IDnumber) {
    Scanner kbd = new Scanner(System.in);
    String course;
    int BLIS, BSCS, BSIS, BSIT;
    System.out.print("Please enter the student ID number: ");
    IDnumber = kbd.nextInt();
    if (IDnumber < 0){
    System.out.println("Invalid student ID number;");
    return false;
    }

    else if (IDnumber == 0){
    System.out.println("END");
    }


    System.out.print("Please enter the student course: ");
    course = kbd.nextLine();
    if (course.equalsIgnoreCase("BLIS")) {
    BLIS = +1;
    }

    else if (course.equalsIgnoreCase("BSCS")) {
    BSCS = +1;
    }

    else if (course.equalsIgnoreCase("BSIS")) {
    BSIS = +1;
    }

    else if (course.equalsIgnoreCase("BSIT")) {
    BSIT = +1;
    }

    else {
    System.out.println("Invalid course");
    return false;
    }

    return true;
    }
    did I do it the right way?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: convert into its equivalent character??

    The switch on Strings construct just saw the light in Java because people familiar with BASIC-type of languages wanted it and thought it needed to be there. The code needed to implement this 'feature' (mind the quotes) is horrible. Why not use a Map<String, Character> and populate it when the Map is constructed instead?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    elkalibre is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: convert into its equivalent character??

    I dont know how to use the "map<String, Character>" yet as i am still kind of a beginner in programming. and i dont know what a map(java) is. lol

  11. #11
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: convert into its equivalent character??

    You would need two loops to meet the requirements, do-while loops would be sufficient for your needs.
    The ints are pointless as they would lose scope as soon as the method finishes. The next time this method is called it will reset these values.
    The brief also suggests the method should return a string value.

    Regards.

  12. #12
    elkalibre is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: convert into its equivalent character??

    Quote Originally Posted by Ronin View Post
    You would need two loops to meet the requirements, do-while loops would be sufficient for your needs.
    The ints are pointless as they would lose scope as soon as the method finishes. The next time this method is called it will reset these values.
    The brief also suggests the method should return a string value.

    Regards.

    i dont understand.. i need TWO do-while loops in that single method?

  13. #13
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: convert into its equivalent character??

    Thats correct.

    inputStudInfo – asks the user for the ID number and course. It shall repeatedly ask for a valid ID number (non-negative integer value) if an invalid input is entered. Input for a valid course will also be asked repeatedly by this method until a valid value (see above for the list of accepted courses) has been entered by the user. This method shall return the valid course entered by the user or shall return the word END if the user entered 0 as input for the ID number.
    According to the above the method asks the user to input a valid ID number which loops until a valid input is entered.
    It will then ask for a valid course until a valid input is entered.

    I could be misreading the requirements but that is how I read it. On the other hand it asks for an ID number but doesn't say what this is used for. Can you shed some light on the requirements?

    Regards.

  14. #14
    elkalibre is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: convert into its equivalent character??

    Quote Originally Posted by Ronin View Post
    Thats correct.



    According to the above the method asks the user to input a valid ID number which loops until a valid input is entered.
    It will then ask for a valid course until a valid input is entered.

    I could be misreading the requirements but that is how I read it. On the other hand it asks for an ID number but doesn't say what this is used for. Can you shed some light on the requirements?

    Regards.
    here is the problem statement for you to understand it clearly

    Problem Statement:
    Write a program that reads the ID number and course of some CICS students. Data input is terminated by an ID number of 0 (sentinel value). Tally (format shown below) and output as follows:
    Total Students: xx

    Course _______Number of Students
    BLIS__________xx
    BSCS_________xx
    BSIS_________ xx
    BSIT__________xx

    update on code:

    do{
    System.out.print("Please enter the student ID number: ");
    IDnumber = kbd.nextInt();
    if (IDnumber < 0){
    System.out.println("Invalid student ID number;");
    return false;
    }

    else if (IDnumber == 0){
    System.out.println("END");
    }
    }while(showSummary) {
    System.out.println();
    }
    good?
    Last edited by elkalibre; 04-15-2013 at 04:48 PM.

  15. #15
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: convert into its equivalent character??

    I'm still not entirely clear on the requirements.
    I'm assuming ID is the student ID, in which case is this being recorded? Each time an ID is put into the system it adds one to the number of students for that course but is the ID being validated to check if it has already been assigned to a course?

    With this project you can go as far as you want in that is this a simple application demonstrating coding fundamentals or is this something including additional objects / classes etc?

    Regards

Similar Threads

  1. How to convert array list into character
    By suniva in forum New To Java
    Replies: 1
    Last Post: 07-28-2012, 03:49 PM
  2. Replies: 1
    Last Post: 11-01-2011, 05:35 PM
  3. Convert Character to String
    By andy9507 in forum New To Java
    Replies: 3
    Last Post: 05-21-2011, 03:29 PM
  4. Convert String to Binary equivalent
    By vinayak.top in forum Advanced Java
    Replies: 1
    Last Post: 01-21-2011, 08:07 AM
  5. Replies: 1
    Last Post: 09-17-2010, 12:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •