Storing a string in a created obj type

In summary, the conversation discusses how to store String objects in an array in Java. The user tried assigning the String to the first index of the array, but it did not work. They also mention trying to cast the String to a Float class, but this did not work either. The conversation ends with the language being revealed as Java.
  • #1
kolleamm
477
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
  • #2
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]);
}
 
  • #3
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...
 
  • #4
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
 

1. How do I store a string in a created object type?

To store a string in a created object type, you can create a property within the object and assign the string as its value. For example, you can use the dot notation or bracket notation to create a property named "string" and assign the string value to it.

2. Can I store multiple strings in an object?

Yes, you can store multiple strings in an object by creating multiple properties and assigning string values to each of them.

3. How can I access the stored string in an object?

To access the stored string in an object, you can use the dot notation or bracket notation and specify the property name that contains the string value. This will return the string value stored in that property.

4. Can I change the stored string in an object?

Yes, you can change the stored string in an object by reassigning a new string value to the corresponding property. If the property already exists, the new string value will replace the previous one. If the property doesn't exist, a new property will be created with the assigned string value.

5. How do I delete a stored string in an object?

To delete a stored string in an object, you can use the delete keyword followed by the object name and property name. This will remove the specified property and its corresponding string value from the object.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
946
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
944
  • Engineering and Comp Sci Homework Help
Replies
8
Views
909
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Introductory Physics Homework Help
Replies
4
Views
822
Back
Top