Storing a string in a created obj type

  • Thread starter Thread starter kolleamm
  • Start date Start date
  • Tags Tags
    String Type
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 1K views
kolleamm
Messages
476
Reaction score
44

Homework Statement


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
 
Physics news on Phys.org
If X is Object then it should work. I tried it in a processing sketch and it worked okay:

Java:
void setup() {
   Object[] x = new Object[100];
   String s = "hello";
   x[0]=s;      // Object is the parent class to all java classes
   println("x[0]="+x[0]);
}

whereas attempting to cast the string to a FLoat class doesn't work:

Java:
void setup() {
   Float[] x = new Float[100];
   String s = "hello";
   x[0]=(Float)s;       // fails with can't cast String to Float
   println("x[0]="+x[0]);
}
 
BvU said:
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...
The language is Java, apologies I completely forgot to mention it