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

  • SEARCH
  • Home
  • 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 8809597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:54:57+00:00 2026-06-14T02:54:57+00:00

Example code: int width = 5; int area = 8; int potato = 2;

  • 0

Example code:

int width = 5;
int area = 8;
int potato = 2;
int stackOverflow = -4;

Now, say I want to have the user input a string:

String input = new Scanner(System.in).nextLine();

Then, say the user inputs potato. How would I retrieve the variable named potato and do stuff with it? Something like this:

System.getVariable(input); //which will be 2
System.getVariable("stackOverflow"); //should be -4

I looked up some things and did not find much; I did find a reference to something called “the Reflection API,” but that seems too complicated for this one simple task.

Is there a way to do this, and if so, what is it? If “Reflection” does indeed work and if it is the only way, then how would I use it to do this? The tutorial page for it has all sorts of internal stuff that I can’t make any sense of.

EDIT: I need to keep the Strings in the variables for what I am doing. (I can’t use a Map)

  • 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-06-14T02:54:59+00:00Added an answer on June 14, 2026 at 2:54 am

    Using reflection doesn’t seem like a good design for what you’re doing here. It would be better to use a Map<String, Integer> for example:

    static final Map<String, Integer> VALUES_BY_NAME;
    static {
        final Map<String, Integer> valuesByName = new HashMap<>();
        valuesByName.put("width", 5);
        valuesByName.put("potato", 2);
        VALUES_BY_NAME = Collections.unmodifiableMap(valuesByName);
    }
    

    Or with Guava:

    static final ImmutableMap<String, Integer> VALUES_BY_NAME = ImmutableMap.of(
        "width", 5,
        "potato", 2
    );
    

    Or with an enum:

    enum NameValuePair {
    
        WIDTH("width", 5),
        POTATO("potato", 2);
    
        private final String name;
        private final int value;
    
        private NameValuePair(final String name, final int value) {
            this.name = name;
            this.value = value;
        }
    
        public String getName() {
            return name;
        }
    
        public String getValue() {
            return value;
        }
    
        static NameValuePair getByName(final String name) {
            for (final NameValuePair nvp : values()) {
                if (nvp.getName().equals(name)) {
                    return nvp;
                }
            }
            throw new IllegalArgumentException("Invalid name: " + name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7,
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
Here is code example class A{ int i; public: A(int i) : i(i) {}
Take the following C code as an example: char buffer1[5]; int* ret; printf(Buffer1 is:
I'm trying to get the working area of IE browser What i have now
I want to have a for statement that repeats until a given int reaches
I want to calculate area and perimeter of rects using the following code: rect
Example code: <html> <head> <script src=jquery-1.3.2.min.js type=text/javascript></script> <script src=jquery-ui-1.7.1.custom.min.js type=text/javascript></script> </head> <body> <table border=1

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.