• Post Reply Bookmark Topic Watch Topic
  • New Topic

String to Object array  RSS feed

 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone knows how can I add the String to hte Object Array in Java?
eg.
String name="hey";
Name[] objArr= new Name[10];

How can I add the name to the array?
It looks imposssible, I tryed many ways dosnt work.
Gives me error : types Incompatible
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Do you mean something like this?



Also why use an array, could just use a List?

John
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes John i mean that but it gives me error.

I cant use anything else because It s my project that im studying SCJP.
But there is no way i can do it. I tryed everthing
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by umut uzumcu:
... but it gives me error...

This isn't very helpful.
What error messages are you seeing?

Originally posted by umut uzumcu:
.. I tryed everthing


What did you try?
Post your code and the error messages that you're getting.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, your array is a "Name" array. A string is of type "String", which means that it may be stored in a String array or Object array.

A Name array is for Name objects (or objects that inherit from Name).

Henry
[ January 10, 2007: Message edited by: Henry Wong ]
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok here is my code
and i get the error msg
incompatible types (String and Student)

public class Storage
{
String name;
int i=0;
Student[] objStd=new Student[10];
public void AddToArray(Student obj)
{

objStd[i]=obj;
name=objStd[i].getName(); //uses the method from Student class
System.out.println(name);
//till here everthing is ok

objStd[i]=name; //gives me the compilation error
//incompatible types Student and String
System.out.println(objStd[i]);
++i;
}
[ January 10, 2007: Message edited by: umut uzumcu ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you are trying to put a String into an array of Student objects.
But, having said that, you've already added the Student object to the array, so why are you trying to overwrite it with the student's name ?
 
Suchen Chodankar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------------------------------------------
String name="hey";
Name[] objArr= new Name[10];
---------------------------------------------------------

you question is not clear to me.

But if you are trying to store names in an array you have to do it as shown below:

String[] names = new String[10];
String name="hey";
names[0] = name;
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error msg: incompatible types (String and Student)



Ok, so you have a Student Array, objStd. and try to insert a String object
into this array. Since Student and String are not compatible (e.g. if
String were a subclass of Student it wouldn't be a problem) you will get
an error.

The question is: why do you want to insert the name of the student ?
 
Suchen Chodankar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code this should solve your problem.

public class Storage
{
[removed by fred]
}

[Fred wrote: Please remember we want to help people figure stuff out on their own, not hand them the answer]
[ January 10, 2007: Message edited by: Fred Rosenberger ]
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason i m trying to add the string in to the Student assay object is because I need to save it there.

Briefly the program should get the student name from the Student class(which i can do that) and then i need to add it to the Student array obj.

In my code i try to print it to see if its saved in to the object, its not the thing im trying to do. I need it saved to the Student array obj.

Help please i m new in Java and trying to da that last few day, there is no way i can. Its my project.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String name="hey";
Name[] objArr = new Name[10];

This is a great example of a poorly named variable.
If someone sees "objArr" they would naturally assume that it points to an object of type Object[]; not an object of type Name[].



If you think you need to store a String in an array of Student objects, then you need to re-think the structure of you application.

Suppose your Student class has a studentId property and suppose you needed to loop through your array of students and print the studentId for each of them.
What would happen if the code in that loop suddenly tried to call getStudentId() on a String object?
[ January 10, 2007: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help please i m new in Java and trying to da that last few day, there is no way i can. Its my project.


In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:
[EaseUp

We are helping you.
We already know that this is your project.
That you've been trying to do this for the last few days, doesn't make us more or less likely to help you.
Well written questions are more likely to get well written answers.
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Souther
Sory about my english may be thats why i cant explain my problem or may be because i m new in Java. Thank you for your and everybody's help and time.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by umut uzumcu:
The reason i m trying to add the string in to the Student assay object is because I need to save it there.

Briefly the program should get the student name from the Student class(which i can do that) and then i need to add it to the Student array obj.


What you are saying, is that you want the program to

1) get the student name from the student object at position "i" in the array.

2) set the student object at position "i" in the array, to "name". This is where you get the compilation error.

You get the compilation error because "name" is not a Student object.

Using myself as an example: My name is �dne, but "�dne" is not a Student. "�dne" is a String, which contains the name of a Student (representing me). This is called a "has-a(n)" relationship, as in "the Student has a String, called name". The "opposite" relationship is called "is-a(n)", as in "the Student is an Object" (all classes are subclasses of the Object class).

Since you have a Student-array (Student[]), only Student objects can be inserted into it. Since "name" is not a Student (the statement "String is a Student" is false), the compiler will give you an error.

Now, in order to avoid the compiler error, you need to ensure that the parameter you pass to the Student-array "is a" Student-object - which means is a Student-object or a sub-class of Student.

Hope this was helpful.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have in your code this:

What you have said here is that you will ONLY put Student type objects (or Student sub-classes) into your array.

a String is NOT a Student.

Therefore, you CANNOT put a string into this array.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your AddToArray method.


The first thing you do in here is add a Student object to the objStd array. This makes perfect sense.
However, the next thing you do is try to put the name of the student into the same element of the array. As has been explained, you can't put a String into a Student array, but even if you could you wouldn't want to because it would overwrite the Student object that you have just put there.

What do you want to store in the array - the names of the students or the Student objects ? If the latter, then you are already doing that - just delete the code that tries to store the student name. If the former then you need to change the type of your array to String and store the result of obj.getName() in your array.
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by umut uzumcu:
The reason i m trying to add the string in to the Student assay object is because I need to save it there.

Briefly the program should get the student name from the Student class(which i can do that) and then i need to add it to the Student array obj.


Let try to solve the problem in "baby steps"...

The following is how I understand your problem statement:
We need to have a class representing Students and a class which
stores the Student data.


You correctly made two classes: Student and Storage.

The Student has data about itself and the Storage has to have some
methods to insert/retrieve information in the datastructures it has.

Consider the following:

How would you think the following method should be implemented?
---------------------------------------------------------------
insertStudentInStudentArray(Student s)

insertStudentName(Student s)

Would it be possible to print the names of the student from an array of Students? if so, how?
----------------------------------------------------------------------------

Does the above help you?
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be possible to print the names of the student from an array of Students? if so, how?


The roblem is not to print the names of the students, I need to store the names of the students into the Student array object, which i get from the Student class using the method in the Student.


And yes one of you right that i can store it in an array that works, instead of Student object array but the problems is the question says "add Student to the Student object array".
[ January 10, 2007: Message edited by: umut uzumcu ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by umut uzumcu:
but the problems is the question says "add Student to the Student object array".

[ January 10, 2007: Message edited by: umut uzumcu ]


Note that it doesn't say 'add student name to the Student object array'
You are already adding the Student to the array. Thats what the line
does.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You CANNOT put a String into your Student array. Your variable name is a String. You CANNOT put name into your Student array.

If i said "I have a container that can ONLY hold eggs - NOTHING else is allowed", would you insist on putting a bowling ball in it?

no. it's the same thing. name is a String. your array holds Students. they are DIFFERENT. you CAN'T do it.

WHY are you insisting upon storing a String there? what are you trying to accomplish?

The reason i m trying to add the string in to the Student assay object is because I need to save it there.
Basically, here you are saying "i need to do it because i need to do it".

I need to store the names of the students into the Student array object
Do you need to store the name in the object IN the student array? in other words, the array is an object, that contains other objects. your array seems to contain a Student already. do you want to put the name String into that Student object inside the array?
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal

Note that it doesn't say 'add student name to the Student object array'
You are already adding the Student to the array. Thats what the line

code:
--------------------------------------------------------------------------------

objStd[i]=obj;




Hey I think that is the problem, you found it.
Just 1 last thing I wanna be sure that I shouldn't try to save the string to the Student object.

What do you think about this sentence
The Storage class contains an array of Student objects. The class should define a method which accepts an instance of the Student class to be stored into the array of Student objects.
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred

If i said "I have a container that can ONLY hold eggs - NOTHING else is allowed", would you insist on putting a bowling ball in it?

no. it's the same thing. name is a String. your array holds Students. they are DIFFERENT. you CAN'T do it.

WHY are you insisting upon storing a String there? what are you trying to accomplish?


Yes, exactly I was trying to do something impossoble(its may be because of my english). But i thought the question is saying that, I thing Joanne Neal found my mistake.

Now it looks much easier without trying to put bowling ball in an egg container.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by umut uzumcu:
What do you think about this sentence
The Storage class contains an array of Student objects. The class should define a method which accepts an instance of the Student class to be stored into the array of Student objects.


That's exactly what the first line of your addToArray method does. The only thing you might want to add is a check that the value of i is not greater than the length of your array.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!