Forums Register Login

advice needed for converting String array to ArrayList

+Pie Number of slices to send: Send
Hi,

I am eager to find out if the snippet of code is able to produce any values at all.



Also, in model class is there a need to declare the instance variable for courseName ?

Basically, I have only declared



My purpose is to convert String[] to an arrayList in my prepared Statement
+Pie Number of slices to send: Send
I think you might need to go into more detail about what it is you are trying to achieve?

I will say that I'm not sure a Course should have an array of names, but possibly should have a single courseName (as you suggest).  But other than that it's almost impossible to suggest anything else without knowing the bigger picture.
+Pie Number of slices to send: Send
For most of those questions: don't know because you haven't shown us enough of the classes involved.
Since arrays are used commonly, there is an accompanying utility class Arrays which can do all sorts of things with an array; if you go through its methods you can find one which creates a List from an array. If you want to turn that List into an instance of a specific type of List, you will find many have constructors or similar which allow that.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:For most of those questions: don't know because you haven't shown us enough of the classes involved.
Since arrays are used commonly, there is an accompanying utility class Arrays which can do all sorts of things with an array; if you go through its methods you can find one which creates a List from an array. If you want to turn that List into an instance of a specific type of List, you will find many have constructors or similar which allow that.



Hi Dave, Campbell,

Sorry I guess I have missed out the details.

Basically, I have the following code in my controller:

String [] CourseName = Httprequest.getParameterValues("CourseName");

So,

back to the method above,  I am simply trying to convert this Array into an ArrayList

I'd like to know if the method is correct, since the debugger is not working in my Eclipse now and there is no clue of where is the error.

+Pie Number of slices to send: Send
As Campbell says, the Arrays utility class has a method for turning an array into a List<T>.
If you only need to read the List then that should be enough.
+Pie Number of slices to send: Send
 

Dave Tolls wrote:As Campbell says, the Arrays utility class has a method for turning an array into a List<T>.
If you only need to read the List then that should be enough.



Unfortunately, I do not know the method that is written in the Oracle page...

If I know, I would not have so much trouble ...

So, is my method correct ?
+Pie Number of slices to send: Send
Did you follow the links I gave you?
If Eclipse isn't working properly, maybe you shou‍dl reinstall it.
+Pie Number of slices to send: Send
 

tangara goh wrote:

Dave Tolls wrote:As Campbell says, the Arrays utility class has a method for turning an array into a List<T>.
If you only need to read the List then that should be enough.



Unfortunately, I do not know the method that is written in the Oracle page...

If I know, I would not have so much trouble ...

So, is my method correct ?


the first skill as developer one have to learn is to formulate the problem in the right way.
Have to be understood by the others and in this way will be understood by you
so Please say again.
what is the challenge?
Please use a small snippet of code or quote so that can be easy for yourself to understand where the problem is


+Pie Number of slices to send: Send
 

tangara goh wrote:

Dave Tolls wrote:As Campbell says, the Arrays utility class has a method for turning an array into a List<T>.
If you only need to read the List then that should be enough.



Unfortunately, I do not know the method that is written in the Oracle page...



It's the very *first* method listed in the JavaDoc for the Arrays utility class.

Henry
+Pie Number of slices to send: Send
ok the link you received from Campbell says
modifier and type:


static <T> List<T>


method and desciption

asList(T... a)  Returns a fixed-size list backed by the specified array.



Now this looks a bit strange with all these letters as

<T>

 that means a whatever generic type, or the

(T... a)


this is a varargs that means you can put a lot of stuff inside this place, but in this case what you want to put.... is.....?
+Pie Number of slices to send: Send
 

Giovanni Montano wrote:

tangara goh wrote:

Dave Tolls wrote:As Campbell says, the Arrays utility class has a method for turning an array into a List<T>.
If you only need to read the List then that should be enough.



Unfortunately, I do not know the method that is written in the Oracle page...

If I know, I would not have so much trouble ...

So, is my method correct ?


the first skill as developer one have to learn is to formulate the problem in the right way.
Have to be understood by the others and in this way will be understood by you
so Please say again.
what is the challenge?
Please use a small snippet of code or quote so that can be easy for yourself to understand where the problem is




Hi Giovanni,

My challenge now is that I am using String [] courseName in my controller because HttpRequest.getParameterValues only allow array.

But, my courseName is not fixed in size.

So, I am trying to convert it to ArrayList

So, in my insertCourseName class I need to set the value ? to the prepared Statement etc.

I think my problem could be putting List<String> CourseName as a parameter inside this method since it is not available until it is performed by the controller.  Please let me know if my guess is correct.



I have been working on this problem for so long I think really I am not cut out to do coding....
2
+Pie Number of slices to send: Send
 

tangara goh wrote:. . .  Please let me know if my guess is correct. . . .

No, I am afraid it isn't. You can write programs correctly by guessing; if you guess 1,000,000× there is a good chance that one of the guesses will be correct. You now simply have to work out which one.
Or you can think about the problem, which you won't sort out by coding. As we have spent the last day trying to tell you, turning an array into a List is quite simple; you can do it in one line using a method found via the link I gave you. That probably isn't your real problem. Let's see what else you have been told:-

Giovanni Montano wrote:. . . the first skill as developer one have to learn is to formulate the problem in the right way. . . .

So, the ball is back in your court. Write down in words of one syllable what you are trying to do. It needs to be simple enough that a six‑year‑old can read and understand it. I couldn't do that because nobody can read my writing. Words like List and Array are banned; those are implementation details which you will only need to fill in later. Write down what the procedure is supposed to be and show us it. Once you have done that, you will have some chance of converting that into code. Without guessing.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:

tangara goh wrote:. . .  Please let me know if my guess is correct. . . .

No, I am afraid it isn't. You can write programs correctly by guessing; if you guess 1,000,000× there is a good chance that one of the guesses will be correct. You now simply have to work out which one.
Or you can think about the problem, which you won't sort out by coding. As we have spent the last day trying to tell you, turning an array into a List is quite simple; you can do it in one line using a method found via the link I gave you. That probably isn't your real problem. Let's see what else you have been told:-

Giovanni Montano wrote:. . . the first skill as developer one have to learn is to formulate the problem in the right way. . . .

So, the ball is back in your court. Write down in words of one syllable what you are trying to do. It needs to be simple enough that a six‑year‑old can read and understand it. I couldn't do that because nobody can read my writing. Words like List and Array are banned; those are implementation details which you will only need to fill in later. Write down what the procedure is supposed to be and show us it. Once you have done that, you will have some chance of converting that into code. Without guessing.



Hi Campbell,

Basically, my web app is separated into 3 layers.

The DAO, the DAOImpl which has the concrete method, the service class which hide the method and the controller.

The problem is that the controller is using String [] subj to get the parameterValues from my form.

And so in my method, I am setting the String subj but it is not working out.

Also, I am not sure how to initialize the String[] in my method ....

Hope that my explanation is clear and I can get some help.

Tks.

+Pie Number of slices to send: Send
 

tangara goh wrote:. . . Hope that my explanation is clear . . .

Afraid it isn't at all clear. You need to think again. You have added a single String to the equation, which makes things even more confusing. You are also using computing terms like DAO, String and parameterValues. You need to get all that sort of wo‍rd out of the explanation.
+Pie Number of slices to send: Send
 

The problem is that the controller is using String [] subj to get the parameterValues from my form.

And so in my method, I am setting the String subj but it is not working out.

Also, I am not sure how to initialize the String[] in my method ....


First, I'm assuming the String subj is actually String[] subj.  What does "it is not working out" mean?  Can you post the code of the method you talking about?

PS: This does not get you out of the request Campbell gave you.
(1 cow)
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:

tangara goh wrote:. . . Hope that my explanation is clear . . .

Afraid it isn't at all clear. You need to think again. You have added a single String to the equation, which makes things even more confusing. You are also using computing terms like DAO, String and parameterValues. You need to get all that sort of wo‍rd out of the explanation.


tangara embrace the challenge:), I think Campbell is teaching you one of the greatest assets a dev/professional can bring in his tool suitcase, one that can make the difference in the carreer, the art to be understood by everyone
he said explain as at a six years old.  Imagine a mafia guy from Godfather that say to you whatse de probleme?  find the simplicity
Knute told you what does "it is not working out" means

I give you a framework:)

what you want to obtain?
how you are obtaining?
whatse de probleme and the solutione?
+Pie Number of slices to send: Send
 

Giovanni Montano wrote:. . . whatse de probleme . . .?

That's good.
+Pie Number of slices to send: Send
 

Giovanni Montano wrote:

Campbell Ritchie wrote:

tangara goh wrote:. . . Hope that my explanation is clear . . .

Afraid it isn't at all clear. You need to think again. You have added a single String to the equation, which makes things even more confusing. You are also using computing terms like DAO, String and parameterValues. You need to get all that sort of wo‍rd out of the explanation.


tangara embrace the challenge:), I think Campbell is teaching you one of the greatest assets a dev/professional can bring in his tool suitcase, one that can make the difference in the carreer, the art to be understood by everyone
he said explain as at a six years old.  Imagine a mafia guy from Godfather that say to you whatse de probleme?  find the simplicity
Knute told you what does "it is not working out" means

I give you a framework:)

what you want to obtain?
how you are obtaining?
whatse de probleme and the solutione?



Hello Giovanni, Campbell,

First, thank you guys for being patient and helping a poor soul like me.  Really, I have no choice but to learn programming cos I really don't know what to do at this point of my life...old and then nobody to help me in this n I am stuck...(OK. So much for grumbling)

Basically, I am trying to get the tutor to register a form.   The information is to be entered into database so that I can then extend my program such as do a match, do a search, add in REST etc etc.

That is my purpose what I am trying to do.

1) So, the form will have tutor information - so, I must have a tutor including the subject column he or she will like to teach right?
2) And then I just know that I 'could' create a subject only table with subjectName as unique name so that I can then create a third table which will then have the tutor_Id and the subjectNames
3) But, the information is already in the first table  as in point 1?

+Pie Number of slices to send: Send
 

tangara goh wrote:. . . register a form.

Right. You need to decide what is on the form.

The information is to be entered into database so that I can then extend my program such as do a match, do a search, add in REST etc etc.

You have changed from normal English to programmer‑speak. That won't help you; you need to decide what you are going to do before you work out how to do it.

. . . tutor information - so, I must have a tutor including the subject column he or she will like to teach right?

You will have to decide that. What is a subject column? That sounds again like trying to decide how to do it before you have decided what to do.

. . . subject only table . . . table . . . tutor_Id and the subjectNames . . .

That is not at all what you shou‍ld be thinking at this stage. There shou‍ld be no mention of tables or tutor_id or anything.

You should be telling me things like this:-

I want to record information about a car. I need to know its make, registration number, colour and owner's name. I need to know how far the car is driven each week and how much petrol/diesel it consumes per week. I want to average its fuel consumption by week and year.
I expect to have that information from 1,000 different cars.

 
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:

tangara goh wrote:. . . register a form.

Right. You need to decide what is on the form.

The information is to be entered into database so that I can then extend my program such as do a match, do a search, add in REST etc etc.

You have changed from normal English to programmer‑speak. That won't help you; you need to decide what you are going to do before you work out how to do it.

. . . tutor information - so, I must have a tutor including the subject column he or she will like to teach right?

You will have to decide that. What is a subject column? That sounds again like trying to decide how to do it before you have decided what to do.

. . . subject only table . . . table . . . tutor_Id and the subjectNames . . .

That is not at all what you shou‍ld be thinking at this stage. There shou‍ld be no mention of tables or tutor_id or anything.

You should be telling me things like this:-

I want to record information about a car. I need to know its make, registration number, colour and owner's name. I need to know how far the car is driven each week and how much petrol/diesel it consumes per week. I want to average its fuel consumption by week and year.
I expect to have that information from 1,000 different cars.



OK.  Let me try it again.

I want to have tutor details like Name, age, contact number, address, subjects they can teach, qualification rates per hour,  remarks for special details like if they can only do during weekends.
I want to be able to do a search to find which tutor in which area based on their address so that I can list out the names, qualification and parents can then pick the one they like.
Parents can also chat to the tutor they pick but they cannot see each other details.  Only when parents confirmed, then details can be sent to the parents by email or sms.
I also want to give ratings to tutor with good record.

Think that's all the things I want to do for now.  As for the rest, I guess better don't mention now cos I will be happy if I can do all the above for now.

Hope I am speaking like a 6 years old finally.
+Pie Number of slices to send: Send
That's better
Now you have said name and address, please consider how you would write a Name class and an Address class. Once you have those classes working, then consider the rest of the assignment. Some things, like the search, shou‍ld be left till later.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:That's better
Now you have said name and address, please consider how you would write a Name class and an Address class. Once you have those classes working, then consider the rest of the assignment. Some things, like the search, shou‍ld be left till later.



Now, you are making me confused!

Why would l need to write a separate classes for Name and Address since it  belongs to Tutor class ?
1
+Pie Number of slices to send: Send
Because Name and Address are not specific to Tutors.
Because other classes would have name and address too.
Because creating a class with as many fields as you would need to specify name and address completely would make it unwieldy and too large.
Because the number of compiler parameters required would be so many as to be error‑prone.
Because the paradigm of object‑orientation decomposes things into their constituent parts and assembles larger object from those parts.
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 699 times.
Similar Threads
How can I set the parameter in this case?
I need to replace an array with an arraylist
alternative to I/O
(homework) What does creating an arraylist object in a constructor mean?
Deserialize JSON to Java class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:01:03.