• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How can i check for empty JTextfield and get one line at a time from a JTextArea?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JTextField filename = new JTextField(800,20);


......


 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String.trim() never returns null. Neither does JTextField.getText(). Therefore, the check against null can be removed.

Never ever use == for String comparison. Strings are objects, so you are comparing object references. Use equals.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would probably make a StringUtils class that has a isNullOrEmpty(String str) { } method. If you're going to be doing a lot of String validation you'll use it a lot. Then you could replace lines 5 through 9 with:

 
Ana Suvari
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob - Thanks for the tip on not using == and using equals instead.

Jarred - Thanks for the idea but i am not sure i need to import another class. It's good to know in case i might need it.

But can someone help me with the textarea problem? How would you separate one line at a time? Should i use indexOf but doesn't that use only characters and not code for '\n'.

I appreciate any tips to help me learn and i can help others if i can.
 
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JTextArea class does have a getLineCount() method. So in your original post, you declare "content" to be a JTextArea variable in the first code fragment, but in the second code fragment you must have declared "content" as a different variable of a different type.
 
Bartender
Posts: 2907
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This sounds like a job for String tokenizer !!
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic