• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Card example from HFJ: adding blank objects unintentionally

 
Greenhorn
Posts: 3
Android Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:)
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same code that was in this thread https://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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic