• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert String[] to arraylist  RSS feed

 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I am super new at this and I am having so much trouble trying to convert the following:



Am I anywhere close to getting this converted?

This part has to remain exactly like this:



And it has to convert to an ArrayList, so that it is expandable, can someone please assist in ways to get this accomplished?

Thanks,

Jenni
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37249
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jenni,
Welcome to CodeRanch!

In a nutshell, you need to:
1) Instantiate an empty array list
2) Loop through your array and add one element on the array list each time.

Give it a shot and post what you are having trouble with. (and what code you had when you got stuck)
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried this:


But it gives an error on .split
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37249
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, so you want each comma separated value to have a separate entry in the ArrayList. Then you need to call split() within the loop.

split() is failing because it is called on a single String rather than an array.
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's my problem I have no idea how to do that I guess. I am confused as to how to iterate thru the list when it's just a huge string like this.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37249
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Let's try a simpler problem. Can you write a loop that goes through the String[] and just prints each element? The output would be:
1,John,Smith,John1989@gmail.com,20,88,79,59
2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85
3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87
4,Erin,Black,Erin.black@comcast.net,22,91,98,82

Once you get that try to add two more lines to your program. One to create an empty ArrayList and the other something like list.add(line). This will create a list with each line. If you get that working, it will be time to go on to the next step. And if not, you'll have a more specific to post in this thread.
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The extra comma between the strings I want to separate is what is screwing me up:



I am a complete noob, and my directions for this project are very vague, I am not finding anything in my reading material that is the least bit helpful, so any help you could provide for a for loop would be great too. I just don't get it, I think I am going to fail this class.
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again.

I think you are getting confused between commas between successive elements in the array, and commas inside the individual Strings. TryThat should give you 4 followed by a printout rather similar to what Jeanne showed you (minus the line numbers).
Also: do you know how to iterate an array and print its individual elements?
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this:


But I'm not sure if I'm using it correctly.
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The commas separating successive array elements are part of the syntax for writing an array initialiser, and they do not form part of the array itself elements themselves.
String[] students = {"1,John,Smith,John1989@gmail.com,20,88,79,59",
"2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
"3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87",
"4,Erin,Black,Erin.black@comcast.net,22,91,98,82",
"5,Jennifer,Pasternik,jpasternik@myschool.edu,40,83,75,93"};
That is your code with the commas between elements shown in red. The nice formatting has gone away because text colours don't work in code tags. You may have to look carefully for the colour. You will also have to look carefully to find out that I have lost the ability to count beyond 4
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jenni Pasternik wrote:I know this:


But I'm not sure if I'm using it correctly.
If you use i < 0 your loop will run exactly 0 times, but you can change that. Change the i < 0 bit to i < students.length. And change i after println to students[i] and see what happens
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK so I have:


Which now outputs:

1,John,Smith,John1989@gmail.com,20,88,79,59
2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85
3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87
4,Erin,Black,Erin.black@comcast.net,22,91,98,82
5,Jennifer,Pasternik,jpasternik@myschool.edu,40,83,75,93

Which is further than I have gotten, so THANK YOU very much!!

Now I need to convert it to an arraylist, so that I can set some of my variables.
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I do something like this?

 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope... I just answered my own question I guess...
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this is working right either:


It gives me an output of:

[1,John,Smith,John1989@gmail.com,20,88,79,59, 2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85, 3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87, 4,Erin,Black,Erin.black@comcast.net,22,91,98,82, 5,Jennifer,Pasternik,jpasternik@myschool.edu,40,83,75,93]

Is this now an arraylist?
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well actually I think that is working now... It seems to have printed out right.

I'm going to see if it works with some of my other stuff now...
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I quite forgot you can do that. Only just remembered now.
 
Eric Matthew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I noticed you got an answer for an array but if you still need an answer for ArrayList, maybe this code can help.

 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much everyone! I really appreciate it!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!