1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Storing a string in a created obj type

  1. Feb 14, 2017 #1
    1. The problem statement, all variables and given/known data
    How can I store String s in X x_obj [] = new X [100] ;

    2. What I tried
    I tried

    x_obj [0] = s;
    But it didn't work. I know there are easier ways but this is how I have to do it.

    Thanks in advance
  2. jcsd
  3. Feb 15, 2017 #2


    Staff: Mentor

    If X is Object then it should work. I tried it in a processing sketch and it worked okay:

    Code (Java):

    void setup() {
       Object[] x = new Object[100];
       String s = "hello";
       x[0]=s;      // Object is the parent class to all java classes
    whereas attempting to cast the string to a FLoat class doesn't work:

    Code (Java):

    void setup() {
       Float[] x = new Float[100];
       String s = "hello";
       x[0]=(Float)s;       // fails with can't cast String to Float
  4. Feb 16, 2017 #3


    User Avatar
    Science Advisor
    Homework Helper
    2017 Award

    How did you guess the language ? I thought better of asking OP for it since that would have thrown the thread off the unanswered list...
  5. Feb 16, 2017 #4
    The language is Java, apologies I completely forgot to mention it
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted