• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing out string out of index  RSS feed

 
Matt Wright
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am new to Java so I am writing a Java permutation program, it takes in N and k and gives k amount of combinations based on N length.
That portion works, but the portion I have that does not work is when k is greater then 1, the array is then then printing strings out of index.

The perm algorithm ends on index - 1 for moving characters but then I push all those characters into an Array List, I would think I could print them off however I want from there but that is not the case. I'll include the minimum amount of code possible.

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt, welcome to the Ranch!

I looked at your post, but I'm stuck because I don't understand what "out of index" means. Could you clarify that?
 
Matt Wright
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean that the String index is out of range
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, okay. And that's at line 45 in your original post, right?

Well, looking at the loop which contains that line, there's something which doesn't look right to me. Your code is going through an iterator (the "itr" variable), but it isn't doing anything with that iterator. Shouldn't it be working with the current element of the iterator, or something like that?
 
Matt Wright
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is line 45, Ah I see, I'm not actually printing out the substrings pushed into the ArrayList.

How can I print out the strings from the arrayList truncated a user defined amount.

I'm pushing in "1234" "1243"

But I want to print from the ArrayList

"12" "14"

if the user enters a 2
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Wright wrote:How can I print out the strings from the arrayList truncated a user defined amount.


There's two steps: (1) Get the string from the ArrayList, i.e. from the Iterator; (2) Print the truncated version of that string.
 
Matt Wright
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you man, I really appreciate your help.




Worked beautifully, I didn't realize I wasn't actually printing from the ArrayList
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!