• Post Reply Bookmark Topic Watch Topic
  • New Topic

printing an integer array into a GUI text field  RSS feed

 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings fellow bar flys,

I am having trouble printing an arraylist of integers into a text field. Can someone buy this fella a drink?

Thanks,
Jim

Here is the code

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Arrays.toString() method, which returns all the elements of the array, comma separated and enclosed in [].
If that's not the format you want then you need to use a loop to add the values to a StringBuilder in the format you want and then just call the StringBuilder's toString method.
 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:Have a look at the Arrays.toString() method, which returns all the elements of the array, comma separated and enclosed in [].
If that's not the format you want then you need to use a loop to add the values to a StringBuilder in the format you want and then just call the StringBuilder's toString method.


Thank you Stuart,

May I ask you a few questions about the Arrays.toString() method. I have just did a little research on the topic and it does not appear to be offered for arrayLists. Is this correct?
Also, once I switch over to Arrays instead of ArrayLists and use the Arrays.toString() method to build my array of strings will I be able to do something like inField.setText(copiedArray); where copiedArray is the new string array? I will play around with this.


Best To You,
Jim
 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:Have a look at the Arrays.toString() method, which returns all the elements of the array, comma separated and enclosed in [].
If that's not the format you want then you need to use a loop to add the values to a StringBuilder in the format you want and then just call the StringBuilder's toString method.


Hello,
I have tried Arrays.toString() method and it does print the array to the text field like you said it would. I am having trouble with my loop though, the one in the inorder() method is printing alot of zeros. I had solved this problem earlier, just need to refigure out how to create the array without all of the zeros in this recursive method. Probably need some outside counting variable. Here is the code if you're interested.
Thank you,
Jim

 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I greatly reduced the number of zeros from 99 to 4 for each inTestField.setText(); method call by reducing the size of the array from [100] to [5].
This is a big help, however I need to get rid of the remaining 4 - zeros.
Still plugging away at it though.

Thanks
Jim
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Branley wrote:.............it does not appear to be offered for arrayLists.


ArrayList also has the toString() method.


 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote:
Jim Branley wrote:.............it does not appear to be offered for arrayLists.


ArrayList also has the toString() method.




Thanks Rameshwar,

I was getting a little distressed.

I will convert my code and let you know how it is doing.

Jim
 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again, I tried it and it works. I will keep plugging away at actually filling up the array. Right now it is not printing a string per se but one element at a time because of the recursive nature of the method inorder();

I will have to print 5 numbers if btree has 5 nodes, etc.. etc..

Jim
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Branley wrote: Right now it is not printing a string per se but one element at a time because of the recursive nature of the method inorder();

I will have to print 5 numbers if btree has 5 nodes, etc.. etc..

Jim


I haven't really understood what you are trying to say here. But if I guess I think you want the output to be displayed in String format, if yes then Stuart A. Burkett has already answered in that case

Have a look at the Arrays.toString() method, which returns all the elements of the array, comma separated and enclosed in [].
If that's not the format you want then you need to use a loop to add the values to a StringBuilder in the format you want and then just call the StringBuilder's toString method.

 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote:
Jim Branley wrote: Right now it is not printing a string per se but one element at a time because of the recursive nature of the method inorder();

I will have to print 5 numbers if btree has 5 nodes, etc.. etc..

Jim


I haven't really understood what you are trying to say here. But if I guess I think you want the output to be displayed in String format, if yes then Stuart A. Burkett has already answered in that case

Have a look at the Arrays.toString() method, which returns all the elements of the array, comma separated and enclosed in [].
If that's not the format you want then you need to use a loop to add the values to a StringBuilder in the format you want and then just call the StringBuilder's toString method.



Instead of printing an array it is printing each element of the array. when infieldtext.setText() is called it erases the previous element called and prints the next element called. Therefore at the end I only have the last element of the array printed.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Branley wrote:when infieldtext.setText() is called it erases the previous element called and prints the next element called.


That's right. You've identified the problem correctly. So the next step is to decide what to do instead. You don't want to set the component's text to just the element you have in your hand at that moment, so what do you want to set it to instead?
 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Branley wrote:Thanks again, I tried it and it works. I will keep plugging away at actually filling up the array. Right now it is not printing a string per se but one element at a time because of the recursive nature of the method inorder();

I will have to print 5 numbers if btree has 5 nodes, etc.. etc..

Jim


I solved the problem by moving the creation of the arraylist orderArray out of this method and at the beginning of the class. This is working fine now. Thanks to all.

 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Jim Branley wrote:when infieldtext.setText() is called it erases the previous element called and prints the next element called.


That's right. You've identified the problem correctly. So the next step is to decide what to do instead. You don't want to set the component's text to just the element you have in your hand at that moment, so what do you want to set it to instead?


Thanks. I hope my previous post explains how I solved the problem. I am not thrilled about the braces [ ] displayed on the GUI but for now I will let it be unless I decide to use StringBuilder as suggested by our friend in a previous post.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Branley wrote:May I ask you a few questions about the Arrays.toString() method. I have just did a little research on the topic and it does not appear to be offered for arrayLists.

Sorry about that. I was just reading the comments in your code where you refer to arrays. I didn't notice you were actually using ArrayLists.
Glad you got it working.
 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
Jim Branley wrote:May I ask you a few questions about the Arrays.toString() method. I have just did a little research on the topic and it does not appear to be offered for arrayLists.

Sorry about that. I was just reading the comments in your code where you refer to arrays. I didn't notice you were actually using ArrayLists.
Glad you got it working.


Your advice was perfect, thanks. I wish that I could get rid or the brackets [] that surround the list but other than that I couldn't be happier.

Now I am trying to figure out how to count the leaves in a binary search tree. I am starting to narrow it down to using a getHeight(); method

Thanks a bunch,

I am enjoying the relaxed atmosphere of this forum.

 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Branley wrote:I wish that I could get rid or the brackets [] that surround the list but other than that I couldn't be happier.


In that case a replace() method might be useful where you are replacing the left "[" and right "]" brackets with "" i.e.

 
Jim Branley
Greenhorn
Posts: 27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote:
Jim Branley wrote:I wish that I could get rid or the brackets [] that surround the list but other than that I couldn't be happier.


In that case a replace() method might be useful where you are replacing the left "[" and right "]" brackets with "" i.e.



That is nice Rameshwar. I thought I saw a replace method somewhere in the chapter on arraylists but never thought of using it here.
Thanks,
Jim
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're Welcome !!!
 
Jim Branley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

Thanks for all of the help this past weekend on my binary tree program. I am becoming pleased with it but I plan to spruce the GUI up a bit to make it more presentable. I sure wish that I could get my head wrapped around Recursion, (PreOrder, InOrder and PostOrder Traversals), I would like to have that knowledge firmly implanted in my head. Explaining those topics should be like second nature to me. I am going to keep plugging away at these topics to fully understand them so that I can answer questions like.... Why would anyone want to Traverse a Binary Tree in either of those three orders? What advantage does one order have over another? If my community college offered a next level of Java as a course that would delve deeper into these questions I would take it. Maybe someone like Neal Stephenson will write a novel and devote a few chapters explaining these topics. Until then, I will keep writing code until I see the light.

Best To All,

I am not seeking advice here. Just contemplating....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!