Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6836197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:19:38+00:00 2026-05-26T23:19:38+00:00

Disclaimer: I am not going to say that I am the most experienced Java

  • 0

Disclaimer: I am not going to say that I am the most experienced Java person. There could be easier ways to do what I have in my examples. But this is a question about turning a string entry into an Enum class, not an enum variable. I tried to explain as best as I could.

This is a simple version of what I want to do. I will have two different enums and I want to choose one or the other based off what the user wants. And example of this, in code, would be

public enum Letters {
    A, B, C, D, E, ... , X, Y, Z
}

public enum Numbers {
    ONE, TWO, THREE, ..., EIGHT, NINE, TEN
}

public static void main(String [] args) {

    System.out.println("Enter in you choice, letters (Letters) or numbers(Numbers)");
    String entry = "";
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(input);
    try {
        entry = reader.readLine();
    }
    catch(Exception e){};
    ...
}

I want to make it so the user, if he enters in “Letters” it will be able to take the string entry and turn it into a variable reference to the Letters Enum. The ellipses are place holders for code. I want to eventually use this so I can use

for (answer n : variable.values()) {
    System.out.println(n);
}

instead of

if (answer.equals("Numbers")) {
    Numbers n;
    for (n : Numbers.values()) {
        System.out.println(n);
    }
} else {
    Letters l;
    for (l : Letters.values()) {
        System.out.println(l);
    }
}

I have tried doing

Class<?> c = Class.forName("Letters");

but it doesn’t work.

In my real world example, I have 3 .java files which contains separate classes, and an enum in each of them. There is Sandwich, Beer, and Chip class and their enums are Sandwiches, Beers, Chips. Those classes implement an interface VendingMachineItem. And finally, that interface is part of a vendingMachine package. I don’t know if those matter, but that’s all the info that I can give.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:19:39+00:00Added an answer on May 26, 2026 at 11:19 pm

    The following works (tested with Java6 under Eclipse):

    package com.bob;
    public class Test
    {
        public enum Letters {
            A, B, C, D, E
        }
    
        public enum Numbers {
            ONE, TWO, THREE, FOUR, FIVE
        }
    
        public static void main(String [] args) throws Exception 
        {
    
            System.out.println("Enter in you choice, letters (Letters) or numbers(Numbers)");
            String entry = "";
            InputStreamReader input = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(input);
            entry = reader.readLine();
    
            Class<Enum<?>> clazz = (Class<Enum<?>>)Class.forName("com.bob.Test$" + entry);
    
            for (Enum<?> x : clazz.getEnumConstants())
            {
                System.out.println(x);
            }
    
        }
    }
    

    Notice that the fully qualified classname of the enums are com.bob.Test$Letters and com.bob.Test$Numbers due to being inner classes. Error handling is left as an exercise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer This is not strictly a programming question, but most programmers soon or later
Disclaimer: I have not used RoR, and I have not generated tests. But, I
Disclaimer: JS novice I have a JS widget that depends on JQuery. The widget's
Disclaimer: I'm not very good at Java and just comparing read/writer locks between C#
Disclaimer: I'm working in a project where exist an huge webapp that have an
Disclaimer: I am not a C programmer. I have recently seen a friend's project.
Disclaimer: I'm very new to Django. I must say that so far I really
Disclaimer: I'm not a programmer, never was, never learned algorithms, CS, etc. Just have
( Disclaimer: This question is not specific to ASP.NET) I have a control which
Disclaimer: I'm not a Java guy, but ended up having to maintain some Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.