Storing a string in a created obj type

  • Thread starter Thread starter kolleamm
  • Start date Start date
  • Tags Tags
    String Type
Click For Summary

Discussion Overview

The discussion revolves around the problem of storing a string in an array of a custom object type in Java. Participants explore the methods and challenges associated with this task, including type compatibility and casting issues.

Discussion Character

  • Homework-related, Technical explanation, Debate/contested

Main Points Raised

  • One participant asks how to store a string in an array of a custom object type, noting that their approach did not work.
  • Another participant suggests that if the custom type X is an Object, storing a string should work, providing an example where a string is successfully stored in an Object array.
  • The same participant notes that casting a string to a Float type fails, illustrating type compatibility issues in Java.
  • There is a question about how the programming language was identified, indicating some uncertainty about the context of the original question.
  • A later reply confirms that the language is Java, acknowledging the oversight in not mentioning it initially.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the original question, as the initial poster's method is not clarified, and there are varying perspectives on type handling in Java.

Contextual Notes

There are limitations regarding the assumptions about the type of X and the specific requirements of the task, which are not fully detailed in the discussion.

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]);
}
 
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...
 
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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
43
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K