8 Replies - 465 Views - Last Post: 03 August 2013 - 12:10 PM Rate Topic: -----

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 576
  • Joined: 10-March 13

Word searching/matching

Posted 31 July 2013 - 01:27 PM

How would I make a code that can search a word that the user enters and sees if it matches with a string list?
For example, lets say I want to make a code that asks the user, one by one, to enter the list of ingredients for homemade chocolate milk. Milk, sugar, cocoa powder.

System.out.println("Enter the 3 ingredients one at a time and press enter after each ingredient: "):
String a1 = input.next();
String a2 = input.next();
String a3 = input.next();

String chocolateMilk = "milk, sugar, coaco powder");

//check to see if they match one by one. if statement?



I know I COULD just make the ingredients into 3 separate strings, but this is just a simple example for one that would contain much more ingredients(trying to find an easier way). Would I have to make the ingredients into 3 separate strings and just match it up in the end or is there another way? Lastly, how would I make it so that if the user gets all 3 or 2 or 1 of the ingredients correct, I can bring up a score for them?

Is This A Good Question/Topic? 0
  • +

Replies To: Word searching/matching

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: Word searching/matching

Posted 31 July 2013 - 01:44 PM

The string class has a method called "contains" which you could use.

if (chocolateMilk.toLowerCase().contains(a1.toLowerCase())) {
     // Yes chocolate milk contains that ingredient
}



Here we are searching the string to see if whatever they typed in as "a1" is in the string. Then of course you could always split the line and test the pieces, but that is up to whatever you are doing.

:)

This post has been edited by Martyr2: 31 July 2013 - 01:45 PM

Was This Post Helpful? 1
  • +
  • -

#3 hax95  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 8
  • Joined: 31-July 13

Re: Word searching/matching

Posted 31 July 2013 - 02:42 PM

Martyr2 give you a fantastic solution,but, apart the contains method, if you want to generalize the program, to check how many string you want, instead of separate string, you must use an array to collect these.then, after typing string, you loop in another for loop to check if chockolateMilk contains the strings.
[code]
import java.util.*;

public class Searching {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int numberIngredients;
you can add how many elements you want
System.out.println("how many elements you want to add?");
numberIngredients=in.nextInt();
String[] ingredients = new String[numberIngredients];
for(int i=0; i<numberIngredients;i++){
//insert ingredients
}

//yu can change the number of elements
String chocolateMilk = "milk,sugar,coaco powder";

for(int i=0; i<numberIngredients;i++){
//check if the ingredients[i] is contained in chocolateMilk variable
//and keep trak with a counter

}

//print out how many strings matched
}

}
[code]
Was This Post Helpful? 2
  • +
  • -

#4 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 576
  • Joined: 10-March 13

Re: Word searching/matching

Posted 31 July 2013 - 06:14 PM

View PostMartyr2, on 31 July 2013 - 01:44 PM, said:

The string class has a method called "contains" which you could use.

if (chocolateMilk.toLowerCase().contains(a1.toLowerCase())) {
     // Yes chocolate milk contains that ingredient
}



Here we are searching the string to see if whatever they typed in as "a1" is in the string. Then of course you could always split the line and test the pieces, but that is up to whatever you are doing.

:)/>/>/>


Thank you very much!! But how would I make it into a ignoresCase style? I tried

if (chocolateMilk.ignoreCase().contains(a1.ingoreCase())) {
     
}



But that didnt work.

This post has been edited by streek405: 31 July 2013 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Word searching/matching

Posted 31 July 2013 - 06:18 PM

Putting both to lowercase like his example does effectively ignore case.
Was This Post Helpful? 0
  • +
  • -

#6 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 576
  • Joined: 10-March 13

Re: Word searching/matching

Posted 31 July 2013 - 06:20 PM

View Posthax95, on 31 July 2013 - 02:42 PM, said:

Martyr2 give you a fantastic solution,but, apart the contains method, if you want to generalize the program, to check how many string you want, instead of separate string, you must use an array to collect these.then, after typing string, you loop in another for loop to check if chockolateMilk contains the strings.
import java.util.*;

public class Searching {
public static void main(String args[]){
	Scanner in = new  Scanner(System.in);
	int numberIngredients;
        you can add how many elements you want
	System.out.println("how many elements you want to add?");
	numberIngredients=in.nextInt();
    String[] ingredients = new String[numberIngredients];
	for(int i=0; i<numberIngredients;i++){
		//insert ingredients
	}
	
        //yu can change the number of elements
	String chocolateMilk = "milk,sugar,coaco powder";
	
	for(int i=0; i<numberIngredients;i++){
		//check if the ingredients[i] is contained in chocolateMilk variable
                //and keep trak with a counter
		
	}
	
	//print out how many strings matched
}

}



for line 12 would i create multiple strings for the ingredients?
Was This Post Helpful? 0
  • +
  • -

#7 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 576
  • Joined: 10-March 13

Re: Word searching/matching

Posted 31 July 2013 - 07:03 PM

Thank you guys for answering my question, it really helped! But, I have I last question for this topic, is there a cleaner a way to check all of the answers like in the following code:

String a1 = input.nextLine();
		String a2 = input.nextLine();
		String a3 = input.nextLine();
		String a4 = input.nextLine();
		String a5 = input.nextLine();
	
		//match the ingredients
		if (answer.equalsIgnoreCase(cv)){
			frap.yesCaffeVanilla();
			if (caffeVanilla2.toLowerCase().contains(a1.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a2.toLowerCase()) &&  
				caffeVanilla2.toLowerCase().contains(a3.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a4.toLowerCase()) && 
				caffeVanilla2.toLowerCase().contains(a5.toLowerCase())){
			
				System.out.println("Congrats you got ALL of the ingredients right!");
			}
		}



The IF statement seems to crowded and I just want to know if there a cleaner way for it to do the same thing but with out all those lines. THANKS!

This post has been edited by streek405: 31 July 2013 - 07:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 hax95  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 8
  • Joined: 31-July 13

Re: Word searching/matching

Posted 02 August 2013 - 02:55 AM

View Poststreek405, on 31 July 2013 - 07:03 PM, said:

Thank you guys for answering my question, it really helped! But, I have I last question for this topic, is there a cleaner a way to check all of the answers like in the following code:

String a1 = input.nextLine();
		String a2 = input.nextLine();
		String a3 = input.nextLine();
		String a4 = input.nextLine();
		String a5 = input.nextLine();
	
		//match the ingredients
		if (answer.equalsIgnoreCase(cv)){
			frap.yesCaffeVanilla();
			if (caffeVanilla2.toLowerCase().contains(a1.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a2.toLowerCase()) &&  
				caffeVanilla2.toLowerCase().contains(a3.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a4.toLowerCase()) && 
				caffeVanilla2.toLowerCase().contains(a5.toLowerCase())){
			
				System.out.println("Congrats you got ALL of the ingredients right!");
			}
		}



The IF statement seems to crowded and I just want to know if there a cleaner way for it to do the same thing but with out all those lines. THANKS!


if you want to automatize the process with less code, you will use an array when you are inserting items, you use this statment

ingredients[i] = input.next();



when you check if the ingredients[i] is contained in chocolateMilk variable, you use the previous array:

 if {(chocolateMilk.toLowerCase().contains(ingredients[i].toLowerCase())
//here, you use a counter, that, when if is true,boost

//at the end of the matching, if the counter matches with numberIngredients it means that you got all ingredients right, //and you print out that to the user



Was This Post Helpful? 2
  • +
  • -

#9 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 576
  • Joined: 10-March 13

Re: Word searching/matching

Posted 03 August 2013 - 12:10 PM

View Posthax95, on 02 August 2013 - 02:55 AM, said:

View Poststreek405, on 31 July 2013 - 07:03 PM, said:

Thank you guys for answering my question, it really helped! But, I have I last question for this topic, is there a cleaner a way to check all of the answers like in the following code:

String a1 = input.nextLine();
		String a2 = input.nextLine();
		String a3 = input.nextLine();
		String a4 = input.nextLine();
		String a5 = input.nextLine();
	
		//match the ingredients
		if (answer.equalsIgnoreCase(cv)){
			frap.yesCaffeVanilla();
			if (caffeVanilla2.toLowerCase().contains(a1.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a2.toLowerCase()) &&  
				caffeVanilla2.toLowerCase().contains(a3.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a4.toLowerCase()) && 
				caffeVanilla2.toLowerCase().contains(a5.toLowerCase())){
			
				System.out.println("Congrats you got ALL of the ingredients right!");
			}
		}



The IF statement seems to crowded and I just want to know if there a cleaner way for it to do the same thing but with out all those lines. THANKS!


if you want to automatize the process with less code, you will use an array when you are inserting items, you use this statment

ingredients[i] = input.next();



when you check if the ingredients[i] is contained in chocolateMilk variable, you use the previous array:

 if {(chocolateMilk.toLowerCase().contains(ingredients[i].toLowerCase())
//here, you use a counter, that, when if is true,boost

//at the end of the matching, if the counter matches with numberIngredients it means that you got all ingredients right, //and you print out that to the user



Ok thanks! Ill try that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1