• Post Reply Bookmark Topic Watch Topic
  • New Topic

Card example from HFJ: adding blank objects unintentionally  RSS feed

 
Dugie Stone
Greenhorn
Posts: 3
Android Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, nice to be here learning and asking lots of dumb questions :P

Sorry to resurrect this post but I have a question that relates to it.
I discovered for myself the reason the saved file has an extra "/" in the middle and at the end is because the QuizCardBuilder will add a blank object into the array every time you click the "next card" button or "save" from the menu.

My questions: What is the best way to detect if the field is empty, so that it doesn't save an empty object in the array.

I looked through the JTextArea documentation and didn't see any kind of isEmpty or something simple like that...

Or, would it be better to go through the array before writing it to a file and remove any empty objects, and if so, again, how would you check.

Thanks for any info you guys can send my way! so, so much to learn
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a blank object? I've never heard that term before. Do you mean a null object? Or do you mean a String with length 0? For those of us that do not own the HFJ book, can you post your code?
 
Dugie Stone
Greenhorn
Posts: 3
Android Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same code that was in this thread http://www.coderanch.com/t/511880/java/java/Head-First-Java-QuizCardBuilder-not
Sorry, the sheriff moved my reply from that thread to this new one ...
here is a snippet of the code that matters, and my two questions.


1.It gets text from a JTextArea field, but before it adds the new 'card' object to the 'cardList' ArrayList, I want to make sure that question.getText() and answer.getText() actually have some data to save.



2.As an extra precaution I might like to filter the array before saving it to a file...

Say I have an ArrayList of 'card' objects, each object has two instance variables that are strings. I need to go through the array and remove any objects where the two instance variables do not contain any strings in the variables.

thanks for any help you can give
-Dugie
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can test whether there is text in a text field by using its getText() method, then things like length() or isEmpty().
If you need proper validation, try whether it matches a regular expression.
 
Dugie Stone
Greenhorn
Posts: 3
Android Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, that works quite well. And thank you for the link! I'll have to explore that API, looks like it could be very useful.

for anyone else who stumbles on this thread here is the new code I came up with.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always use the code button for code. Since you are new, I have edited your code, and you can see how much better it looks now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!