Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old September 24th, 2002, 09:26 PM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
check a string to see if it contains a specific word

how do i check a string to see if it contains a specific word.

Eg1: Shawn bin ahmad. i need to find the word "bin" and prompt user that bin exist.
Eg2: Shawn binte ahmad. i need to find the word "binte" and prompt user that bin exist.
Eg3: Shawn B. in this case, i need not check if "B" exist of not.

Reply With Quote
  #2  
Old September 25th, 2002, 01:53 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Code:
if ( inputString.indexOf( "bin" ) > -1 ) {
  out.print( "found it" );
} else {
  out.print( "not found" );
}

Reply With Quote
  #3  
Old September 25th, 2002, 07:33 PM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i was already using the method, however, i encountered problems with the bin and binte check.

Codes
---------------------------------------------------------------------------
boolean checkspecialword1 = fullname.indexOf("binte") > 0;
if (checkspecialword1)
{
out.print("BINTE found in name.");
}

if(chinese.equalsIgnoreCase("Y"))
{
//check to see if there is "bin"
//if yes, do the following
if(checkbin == true)
{
out.println("Ok.");
}
//check to see if there is "bin"
//if no, do the following
else if(checkbin == false)
{
out.println("Ok.");
}
//if no, do the following
else
{
out.println("Non chinese name should not contain 'bin'.");
}
}
//if it is not a chinese name
else
{ //check to see if there is "bin" //if yes, do the following
if(checkbin == true)
{
out.println("Non chinese name should not contain 'bin'.");
}
//if no, do the following
else
{
out.println("Ok.");
}
}
---------------------------------------------------------------------------------
The problem is when i enter bin, the error msg "Binte found" is prompt and if i entered binte, the same error msg is prompt too. why is this so??

i am trying to allow user to enter bin if it is a chinese name, however if it is a non-chinese name, bin is not allowed. And both chinese and non-chinese names, binte is not allowed.

Reply With Quote
  #4  
Old September 25th, 2002, 11:15 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Well, 'bin' is in 'binte', so it will be found both ways. You can try looking for ' bin '. That should be unique. Also, make sure you check that indexOf > -1, since it can return 0 as a valid result.

Reply With Quote
  #5  
Old September 25th, 2002, 11:25 PM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
but i need to check for both bin and binte.. is there a way whereby u can check the whole word?

Reply With Quote
  #6  
Old September 25th, 2002, 11:29 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Psuedo code:
Code:
if (containsBin) {
  if (containsBinte) {
     ..has binte...
  } else {
     ..has bin but not binte...
}

Reply With Quote
  #7  
Old September 26th, 2002, 02:03 AM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
it still doesn't work.
Can u help me check if there is anything wrong with this code..

Codes:
---------------------------------------------------------------------------------

//if it is a chinese name
if(chinese.equalsIgnoreCase("Y"))
{
//is bin
if(checkbin)
{
//if it is binte
if(checkspecialword1)
{
out.print("BINTE found in name.");
}
// but not binte
else
{
out.println("Ok.");
}
}
//is not bin
else
{
// and it is not binte
if(!checkspecialword1)
{
out.print("Ok.");
}
//it is binte
else
{
out.print("BINTE found in name.");
}
}
}
//if it is not a chinese name
else
{
//if it is binte
if(checkspecialword1)
{
out.print("BINTE found in name.");
}
//if it is bin
else if(checkbin)
{
out.println("Non chinese name should not contain 'bin'.");
}
//if not bin or binte
else
{
out.print("Ok.");
}
}

-----------------------------------------------------------------------------------

thanks.. get back to me asap...

Reply With Quote
  #8  
Old September 26th, 2002, 02:11 AM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
or maybe this??

-----------------------------------------------------------------------------------
if(chinese.equalsIgnoreCase("Y"))
{
//is bin
if(checkbin)
{
out.println("Ok.");
}
//if it is binte
else if(checkspecialword1)
{
out.print("BINTE found in name.");
}
//is not bin or binte
else
{
out.println("Ok.");
}
}
//if it is not a chinese name
else
{
//if it is bin
if(checkbin)
{
out.println("Non chinese name should not contain 'bin'.");
}
//if it is binte
else if(checkspecialword1)
{
out.print("BINTE found in name.");
}
//if not bin or binte
else
{
out.print("Ok.");
}
}
----------------------------------------------------------------------------------

Reply With Quote
  #9  
Old September 26th, 2002, 03:06 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Code:
if(chinese.equalsIgnoreCase("Y"))
{
	//is bin
	if(checkbin)
	{
		//if it is binte
		if(checkspecialword1)
		{
			out.print("BINTE found in name.");
		}
		//not binte
		else
		{
			out.println("Ok.");
		}
	//is not bin or binte
	else
	{
		out.println("Ok.");
	}
}
//if it is not a chinese name
else
{
	//if it is bin
	if(checkbin)
	{
		//if it is binte
		if(checkspecialword1)
		{
			out.print("BINTE found in non chinese name.");
		}
		else
		{
			out.println("Non chinese name should not contain 'bin'.");
		}
	//if not bin or binte
	else
	{
		out.print("Ok.");
	}
}


Btw, use the code (#), or php (PHP) button to paste when pasting in code. Makes it easier to read.

Reply With Quote
  #10  
Old September 26th, 2002, 03:23 AM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
it still doesn't work.

what i actually want is to check if the name that the user entered contains "BINTE" or "BIN". if the user entered a name with binte, no matter whether it is a chinese name or other name, i will reject it. however, if the user enter a name with bin, it will check whether it is a chinese name or non-chinese name using a name indicator.

if it is a chinese name, i allow it, if i is not a chinese name, i reject it.

Reply With Quote
  #11  
Old September 26th, 2002, 01:17 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Code:
if ( inputString.toLowerCase().indexOf( "bin" ) > -1 ) {
  if ( inputString.toLowerCase().indexOf( "binte" ) > -1 ) {
    out.print( "name not allowed" );
  } else {
    if ( isChinese ) {
      out.print( "welcome bin" );
    } else {
      out.print( "name not allowed" );
    }
  }
}

That work?

Reply With Quote
  #12  
Old September 26th, 2002, 01:54 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Here ya go:
Code:
<%@page contentType="text/html"%>
<%!
public String checkString( String inputString, boolean isChinese ) {
	if ( inputString.toLowerCase().indexOf( "bin" ) > -1 ) {
		if ( inputString.toLowerCase().indexOf( "binte" ) > -1 ) {
			return( "binte" );
		} else {
			if ( isChinese ) {
				return( "Chinese bin" );
			} else {
				return( "non-Chinese bin" );
			}
		}
	} else {
		return( "No bin or binte" );
	}
}
%>
<html>
<head><title>Bin/Binte Check</title></head>
<body>
<p>Checking "Shawn bin ahmad" as Chinese<br>
<b><%= checkString( "Shawn bin ahmad", true) %></b>
</p>
<p>Checking "Shawn bin ahmad" as not Chinese<br>
<b><%= checkString( "Shawn bin ahmad", false) %></b>
</p>
<p>Checking "Shawn binte ahmad" as Chinese<br>
<b><%= checkString( "Shawn binte ahmad", true) %></b>
</p>
<p>Checking "Shawn binte ahmad" as not Chinese<br>
<b><%= checkString( "Shawn binte ahmad", false) %></b>
</p>
<p>Checking "Shawn ahmad" as Chinese<br>
<b><%= checkString( "Shawn ahmad", true) %></b>
</p>
<p>Checking "Shawn ahmad" as not Chinese<br>
<b><%= checkString( "Shawn ahmad", false) %></b>
</p>
</body>
</html>

Reply With Quote
  #13  
Old September 26th, 2002, 07:58 PM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
both way does not work. it is because, .indexOf method checks for the first occurances of a string. and part of binte is bin thus, no matter if i enter bin or binte, the error prompt is still wrong. that is why i need a method to check for the exact length and word.

Reply With Quote
  #14  
Old September 27th, 2002, 01:34 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Ok, I think I'm missing what you're trying to do here. The code I posted last checks a string.
If the string contains "binte" it prints "binte".
If the string contains "bin" and is Chinese it prints "Chinese bin".
If the string contains "bin" and is not Chinese, it prints "non-Chinese bin".
And if the string doesn't contain "bin" or "binte" it prints "No bin or binte".

If that's not what it's supposed to do, then what am I missing?

Reply With Quote
  #15  
Old September 27th, 2002, 02:13 AM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i don't really understand your codes. what i have is a interface whereby user can enter their names to subscribe for a service. therefore, when the user enters bin or binte in name, it checks (as previously mentioned.)

the checks is done in another program and the fields from the interface are passed by posting the form page.

is it possible that i send u the files then u help me take a look???

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > check a string to see if it contains a specific word


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread: