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 8190497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:40:07+00:00 2026-06-07T03:40:07+00:00

New to Java, and can’t figure out what I hope to be a simple

  • 0

New to Java, and can’t figure out what I hope to be a simple thing.

I keep “sections” in an array:

//Section.java
public static final String[] TOP = {
    "Top News",
    "http://www.mysite.com/RSS/myfeed.csp",
    "top"
};

I’d like to do something like this:

Article a1 = new Article();
a1.["s_" + section[2]] = 1;  //should resolve to a1.s_top = 1;

But it won’t let me, as it doesn’t know what “section” is. (I’m sure seasoned Java people will cringe at this attempt… but my searches have come up empty on how to do this)

Clarification:

My article mysqlite table has fields for the “section” of the article:

s_top
s_sports
...etc

When doing my import from an XML file, I’d like to set that field to a 1 if it’s in that category. I could have switch statement:

//whatever the Java version of this is
switch(section[2]) {
    case "top": a1.s_top = 1; break;
    case "sports": a1.s_sports = 1; break;
    //...
}

But I thought it’d be a lot easier to just write it as a single line:

a1["s_"+section[2]] = 1;
  • 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-07T03:40:09+00:00Added an answer on June 7, 2026 at 3:40 am

    In Java, it’s a pain to do what you want to do in the way that you’re trying to do it.

    If you don’t want to use the switch/case statement, you could use reflection to pull up the member attribute you’re trying to set:

    Class articleClass = a1.getClass();
    Field field = articleClass.getField("s_top");
    field.set(a1, 1); 
    

    It’ll work, but it may be slow and it’s an atypical approach to this problem.

    Alternately, you could store either a Map<String> or a Map<String,Boolean> inside of your Article class, and have a public function within Article called putSection(String section), and as you iterate, you would put the various section strings (or string/value mappings) into the map for each Article. So, instead of statically defining which sections may exist and giving each Article a yes or no, you’d allow the list of possible sections to be dynamic and based on your xml import.

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

Sidebar

Related Questions

I'm new to Java and I can't figure out what the difference between String[]
I'm new to Java, and while reading documentation so far I can't find any
As far as I know, in Java I can Object o = new String(abc)
How can I authenticate in Java to use the new bing search api from
Can I add more permit to a semaphore in Java? Semaphore s = new
in java can you have an array of variables? if so what is the
i'm new for java how can i add a button or others like Microsoft
I'm new to java and trying to write a simple program using JDBC. It
I'm a new Java developer and would like to find out if it's possible
In Java I can do something like the following: TreeMap<Double, String> myMap = new

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.