Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Convert Enum to String in JAVA

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Convert Enum to String in JAVA

    Hello,

    How can I fix this? Complier tells me we should convert the enum to String.



    package newpackage;
     
    import java.util.Scanner;
     
    enum Week
    {
        Shanbeh, Yekshanbeh, Doshanbeh, Seshanbeh, Chaharshanbeh, Panjshanbeh,Jomeh
    }
     
    public class NewMain {
     
        public static void main(String[] args) {
     
            Scanner s = new Scanner(System.in);
            String day = s.next();
            int inDay;
     
            switch(day)
            {
                case Week.Shanbeh:
                   inDay = 1;
                   break;
     
                   case Week.Yekshanbeh:
                   inDay = 2;
                   break;
            }
     
     
        }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert Enum to String in JAVA

    Please copy the full text of the error message and paste it here. The message should show the source line where the error happens.

    Note: An enum is not a String. The case statement wants String values for the case statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (October 7th, 2020)

  4. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Convert Enum to String in JAVA

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. The message should show the source line where the error happens.

    Note: An enum is not a String. The case statement wants String values for the case statements.
    incompatible types: We cannot be converted to String

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert Enum to String in JAVA

    There is some missing details in your post:
    What source line did the error happen on?
    What is "We" in the message: We cannot be converted to String

    The case statements expect a String constant. Change the values used in the case statements to be Strings.
    For example create a class named Week. Add final static String variables using the names from the enum.
    Get rid of the enum
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (October 7th, 2020)

  7. #5
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Convert Enum to String in JAVA

    Quote Originally Posted by Norm View Post
    There is some missing details in your post:
    What source line did the error happen on?
    What is "We" in the message: We cannot be converted to String
    Thank you for your answer.

    incompatible types: We cannot be converted to String ( case Week.Shanbeh

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert Enum to String in JAVA

    The case statements expect a String constant. Change the values used in the case statements to be Strings.
    For example create a class named Week. Add final static String variables using the names from the enum.
    Get rid of the enum
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (October 7th, 2020)

  10. #7
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Convert Enum to String in JAVA

    Quote Originally Posted by Norm View Post
    The case statements expect a String constant. Change the values used in the case statements to be Strings.
    For example create a class named Week. Add final static String variables using the names from the enum.
    Get rid of the enum
    Thanks, I should use an enum.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert Enum to String in JAVA

    If you must use an enum, then change day to be a Week object:
    Week day = ???  // no idea how to covert a String to a Week  object
    Then switch(day) will work if you Remove the Week. from the case statements.

    --- Update ---

    Look at the tutorial for enum: https://docs.oracle.com/javase/tutor...vaOO/enum.html
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (October 7th, 2020)

  13. #9
    Junior Member tonya's Avatar
    Join Date
    Feb 2018
    Posts
    16
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Convert Enum to String in JAVA

    This will allow you compare a string to a Week enum value, the string will need to be tested prior it's use, it will give an Illegal Argument Exception if the string is not exactly the same as one of the enum values.
    Week day = Week.valueOf(str);
    Last edited by tonya; October 14th, 2020 at 07:26 AM.

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert Enum to String in JAVA

    Your example looks like how to convert a String to an enum. Not how to convert an enum to a String.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Convert String of XML to Document object is null all the time
    By anupamk in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2014, 05:36 PM
  2. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  3. Convert List<Set<String>> to string array
    By shatlav in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2013, 08:16 AM
  4. String to Enum problems...
    By alex13809 in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 2nd, 2011, 10:59 AM
  5. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM