How get Java class to determine if string is empty?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2630 Views - Last Post: 26 March 2009 - 01:56 PM Rate Topic: -----

#1 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

How get Java class to determine if string is empty?

Post icon  Posted 24 March 2009 - 04:54 PM

I'm messing around making a madlib game for extra credit in my Java class and i have everything done accept i cant figure out how to get the class to determine if the string is left empty, like if someone decided to not enter something in the input box, a message would pop up saying they had to go back and enter it again. i know it should use a loop like a while or something, but i don't know how to get it to recognize weather or not the string has stuff in it. I'm using the JOptionPane for my input and messages and i have a lot of getters and setters, so i just need to know the best way to get have a message pop up if the string is left empty, then have them enter something in that string. Thanks

File 1 MathLib1.java

import javax.swing.JOptionPane;
public class MathLib1
{
		private String adjective;
		private String first_name;
		private String illness;
		private String plural_noun;
		private String number;
		private String adjective2;
		private String place;
		private String number2;
		private String noun;
		private String job_occupation;
		private String adjective3;



	public void setadjective (String adj)
	{
			   
		adjective = adj;

		adjective = JOptionPane.showInputDialog("Welcome to the Sic Day MadLib Game!\nPlease enter a adjective.");

	}
	public String getadjective(){

		return adjective;
	}
	public void setfirst_name(String name)
	{
		first_name = name;
		first_name = JOptionPane.showInputDialog("Please enter a first name.");
	}
	public String getfirst_name(){

		return first_name;
	}
	public void setillness(String ill)
	{
		illness = ill;
		illness = JOptionPane.showInputDialog("Please enter a illness.");
	}
	public String getillness(){

		return illness;
	}
	public void setplural_noun(String pnoun)
	{
		plural_noun = pnoun;
		plural_noun = JOptionPane.showInputDialog("Please enter a plural noun.");
	}
	 public String getplural_noun(){

		return plural_noun;
	}
	 public void setnumber(String num)
	{
		number = num;
		number = JOptionPane.showInputDialog("Please enter a number.");
	 }
	 public String getnumber(){

		return number;
	}
	 public void setadjective2(String adj2)
	{
		adjective2 = adj2;
		adjective2 = JOptionPane.showInputDialog("Please enter a adjective.");
	 }
	 public String getadjective2(){

		return adjective2;
	}
	 public void setnoun(String nun)
	{
		noun = nun;
		noun = JOptionPane.showInputDialog("Please enter a noun.");
	 }
	 public String getnoun(){

		return noun;
	}
	 public void setplace(String plc)
	{
		place = plc;
		place = JOptionPane.showInputDialog("Please enter a place.");
	 }
	 public String getplace(){

		return place;
	}
	 public void setnumber2(String num2)
	{
		number2 = num2;
		number2 = JOptionPane.showInputDialog("Please enter a number.");
	 }
	 public String getnumber2(){

		return number2;
	}
	 public void setjob_occupation(String job)
	{
		job_occupation = job;
		job_occupation = JOptionPane.showInputDialog("Please enter a job occupation.");
	 }
	 public String getjob_occupation(){

		return job_occupation;
	}
	 public void setadjective3(String adj3)
	{
		adjective3 = adj3;
		adjective3  = JOptionPane.showInputDialog("Please enter a adjective.");
	 }
	 public String getadjective3(){

		return adjective3;
	}
}

This post has been edited by cmoney12051: 24 March 2009 - 04:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How get Java class to determine if string is empty?

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 04:59 PM

All you have to do is...

if(textBox.getText() == null)

And then do whatever you want in the if statement
Was This Post Helpful? 1
  • +
  • -

#3 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 05:07 PM

where does textbox come in? i know that in my JOptionPane.showInputDialog there is a text box, but when i try to enter what you gave it does not compile, could you please elaborate more. thanks

I Apologize, i figured it out, sorry, i wasn't reading you post right, but i got it thanks
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 05:08 PM

all you have to do is refer to your inputs to check for null values
if(first_name.equals(null))
{
JOptionPane.showMessageDialog(null, "Please enter a name u lazy person!");
} 

Was This Post Helpful? 1
  • +
  • -

#5 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 05:13 PM

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 05:24 PM

I Noticed that when clciking cancel this works but if i click ok and leave it blank it moves onto the next JOptionPane, so it still is possible to leave some blank, is there any way to get around this? i would like to have it leave no blanks weather i click cancel or ok, it should always have something entered into it, or ask the user to re enter it. thanks

P.S. Sorry i should have just editied my last post instead of makeing a new one, i was not thinking.

This post has been edited by cmoney12051: 24 March 2009 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 05:35 PM

are you calling up the input again because remember, you will need to do this otherwise it will just jump to the next one e.g.
if(first_name.equals(null))
{
JOptionPane.showMessageDialog(null, "Please enter a name u lazy person!");
first_name = JOptionPane.showInputDialog("Please enter a first name.");
} 


You could also just create your own custom dialog.
Was This Post Helpful? 0
  • +
  • -

#8 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 05:44 PM

This is exactly what i am putting in

 while(getillness() == null)
		{
			JOptionPane.showMessageDialog(null,"No Empties");
			illness = JOptionPane.showInputDialog("Please enter a illness.");
		}

and if i click cancel it works but if i put ok, it just goes to the next one

its not just this one, its everyone.

This post has been edited by cmoney12051: 24 March 2009 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 06:08 PM

Actually there is a little history behind this little concept. Generally the way I do this is something like:

if (str != null && str.length > 0) { }

or sometimes

if (str != null && !"".equals(str)) { }

something like first_name.equals(null) will fail if first_name is null -- since if it is null is not an object and does not have the .equals method.

I have seen people do:

"null".equals(str) but this excludes the string "null" which may be what the user wants to enter.
Was This Post Helpful? 1
  • +
  • -

#10 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 06:38 PM

i think i lost you in that last post, could you explain it a bit more, thanks
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 06:40 PM

View PostNickDMax, on 24 Mar, 2009 - 05:08 PM, said:

I have seen people do:

"null".equals(str) but this excludes the string "null" which may be what the user wants to enter.

That's horrible !!!
This is like outputting a command like DIR or ls to a file and reading back the file and parsing the output generated
Was This Post Helpful? 0
  • +
  • -

#12 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 06:42 PM

ok i'll try it

This post has been edited by cmoney12051: 24 March 2009 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 06:55 PM

View Postpbl, on 24 Mar, 2009 - 08:40 PM, said:

That's horrible !!!
This is like outputting a command like DIR or ls to a file and reading back the file and parsing the output generated


....um... I don't know what rumors you have heard... but um... I have never done that. I swear!

...

...ok maybe I did, but that was because QBasic didn't have a good way to get a directory listing (the FILES command output the list of files to the screen! useless).
Was This Post Helpful? 0
  • +
  • -

#14 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 07:20 PM

ok i got this much figured out with all your help

  while (adjective != null && !"".equals(adjective)){}
		{
			JOptionPane.showMessageDialog(null,"No Empties");
			
			adjective = JOptionPane.showInputDialog("Welcome to the Sic Day MadLib Game!\nPlease enter a adjective.");
			
		}


but i need it to loop, it stops one time if i click ok the first time it asks again, but if i click it again, it just moves to the next, same for cancel, i tried it with wile, nd if statements,how can i make this repeat. thanks
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How get Java class to determine if string is empty?

Posted 24 March 2009 - 07:36 PM

try removing the { } at the end of the while line...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2