Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying Objects from ArrayList  RSS feed

 
M Sparks
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm working on a project and I am suppose to read in the data from a text file called "stars.txt", create objects for all the sales reps in the file, load the objects onto an ArrayList and display them into my JTextField using an enhanced for loop and formatted with the toString method. I have been at this for days and cannot get the stars.txt data to display in the JTextField!!! It compiles in NetBeans and displays the format for which I want the data in the stars.txt file to be, but not the actual data (which is salesID, firstName, lastName, osField, bsField, psField, buttonGroup1, buttonGroup2). This is all under class (JButton) displayActionButtonPerformed. I created another class called SalesReps to store the variables, sets/gets and constructor (I made this under the main class, does it matter in NetBeans I did not create a new class, i.e. a new TAB outside the main?) I would love any help I can get. Thanks. Heres my code:



 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please UseCodeTags (←click). I've added them for you this time - see how much better it looks?

Not being a Swing expert, I don't feel competent to reply right now; but if anything leaps out at me, I'll let you know.

Winston
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Not being a Swing expert, I don't feel competent to reply right now; but if anything leaps out at me, I'll let you know.

And something has.

You are controlling your loop with an empty List. Have a look at lines 5, 21 and 33: they just don't make sense - you're trying to add to a List inside a loop that uses that List, and that's never going to work. And because it's empty, the loop never gets executed anyway.

Why don't you back up and tell us what you're trying to do?

Winston

PS: 'aList1' is a really bad name for a variable - especially in the context you've used it. It's just plain lazy.
 
M Sparks
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Winston Gutkowski wrote:Not being a Swing expert, I don't feel competent to reply right now; but if anything leaps out at me, I'll let you know.

And something has.

You are controlling your loop with an empty List. Have a look at lines 5, 21 and 33: they just don't make sense - you're trying to add to a List inside a loop that uses that List, and that's never going to work. And because it's empty, the loop never gets executed anyway.

Why don't you back up and tell us what you're trying to do?

Winston

PS: 'aList1' is a really bad name for a variable - especially in the context you've used it. It's just plain lazy.


Sorry, I'm new to coding and new to this site and new to it all. I want to read in the data from a text file called "stars.txt", create objects for all the sales reps in the file, load the objects onto an ArrayList and display them into my JTextField using an enhanced for loop and formatted with the toString method. As for the lazy aList1 I don't know whats lazy and whats not as of yet so I guess thanks for pointing it out, I wouldn't know what non lazy coding is yet.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Sparks wrote:Sorry, I'm new to coding and new to this site and new to it all. I want to read in the data from a text file called "stars.txt", create objects for all the sales reps in the file, load the objects onto an ArrayList and display them into my JTextField using an enhanced for loop and formatted with the toString method. As for the lazy aList1 I don't know whats lazy and whats not as of yet so I guess thanks for pointing it out, I wouldn't know what non lazy coding is yet.

Let's deal with the last bit first: Maybe I was a bit harsh by saying it's lazy, but it looks to me like you called it 'aList1' because you couldn't be bothered to think of a more descriptive name.

Programs - at the least the source code for them - are meant to be read by human beings; and the more you can do to help other people understand what you're doing - or what you're trying to do - the better you're likely to be. So make your names as descriptive as you can.

Now to the first bit: You say you want to read in data from a file and create objects which you add to a List, but your loop is controlled by that List. Doesn't it make more sense to control it by the file you're reading from?

So the next question is: How do you that? Hint: have a look at the Scanner.hasNext() method.

And if that's not enough to get you going, come back and show us what you've tried. We try not to hand out solutions here, because - believe me - you'll be much happier when YOU work it out for yourself. And you'll learn more too.

It might also be useful if you showed us what your file looks like - just a few lines or records will do - because we might be able to suggest some other ideas.

Hope it helps.

Winston
 
M Sparks
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
M Sparks wrote:Sorry, I'm new to coding . . .

. . .

Hope it helps.

Winston


Gotcha, that makes a lot of sense, thanks!
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Sparks wrote:Gotcha, that makes a lot of sense, thanks!

No probs.

Just for future reference: when you "quote" a response, just include enough of it to make it obvious what you're responding to. It helps save old farts like me from getting CTS scrolling down threads.

Just make sure you include the '[/quote]' at the end

Winston
 
Campbell Ritchie
Sheriff
Posts: 53747
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . when you "quote" a response, just include enough of it to make it obvious . . . save . . . me from getting CTS scrolling down threads. . . .
Agree so much that I shall remove most of that quote.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!