• 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

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: 22781
131
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: 28174
95
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: 2911
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 !!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic