Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

advice needed for converting String array to ArrayList  RSS feed

 
tangara goh
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Tolls
Ranch Foreman
Posts: 2996
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tangara goh
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Dave Tolls
Ranch Foreman
Posts: 2996
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tangara goh
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you follow the links I gave you?
If Eclipse isn't working properly, maybe you shou‍dl reinstall it.
 
Giovanni Montano
Ranch Hand
Posts: 426
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Giovanni Montano
Ranch Hand
Posts: 426
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....?
 
tangara goh
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tangara goh
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4189
122
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Giovanni Montano
Ranch Hand
Posts: 426
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:. . . whatse de probleme . . .?
That's good.
 
tangara goh
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tangara goh
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tangara goh
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!