• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding integer values into a String arraylist  RSS feed

 
Kirstin Jackson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all again,

I'm having an issues with adding integer values to a string list. The question is asking me "the method should iterate over runners, and for each runner generate a random number between 90 and 180 (inclusive) which should be used to set the time (in minutes) for that runner."

I have been able to get the random number and iterating over the runner arraylist but I haven't been able to figure out how to add the values generated into the runners list. I am also using BlueJ.

Here's the whole code I have at the moment:


 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kirstin Jackson wrote:Hello all again,
I'm having an issues with adding integer values to a string list. The question is asking me "the method should iterate over runners, and for each runner generate a random number between 90 and 180 (inclusive) which should be used to set the time (in minutes) for that runner."


Let me ask you this: If you have a "Runner" class why are you using a List<String> rather than a List<Runner>?

Assuming I have misunderstood the original problem (keeping the List<String>), the question to ask yourself is: Is there a class in the Java API that will help do such conversion?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I don't see a list called "runners". I see a variable names "runners" but it refers to a single runner.

I see a list called "runner" that holds Strings.

I also don't see a Runner class, so it is hard to know what is going on with that class.

I am also not even sure how this would compile since you don't declare the variable randomNumber anywhere, but you use it in line 28.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kirstin Jackson wrote:I'm having an issues with adding integer values to a string list.

Well, my first question would be: why a String List?

What about a List<Runner> instead? Then all you'd need to do is add a setTime() method to it, and I suspect you'd find things a lot easier.

You might also be interested in reading the StringsAreBad page.

HIH

Winston
 
Kirstin Jackson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never thought of using Runner than String to be honest for the list.. and the code I have at the moment works with the list being <String>. I would have to change the for loop in the first method and that method is working as required so.

I was thinking along the lines of parseInt or String.valueOf().. but when I tried both, I either got errors or nothing was returned.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I am also not even sure how this would compile since you don't declare the variable randomNumber anywhere, but you use it in line 28.


Hi Fred the randomNumber variable is declared on line 19 as private instance variable.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kirstin Jackson wrote:I was thinking along the lines of parseInt or String.valueOf().. but when I tried both, I either got errors or nothing was returned.


And what errors or values did you get? I see your "minute" variable is local in the runMarathon() method, output to console. Where else is "minute" needed?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Hi Fred the randomNumber variable is declared on line 19 as private instance variable.

dang it...i need to stop replying to posts before coffee.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!