8 Replies - 1397 Views - Last Post: 23 February 2015 - 06:08 AM Rate Topic: -----

#1 lillie13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 15

Java String Method 'contains'

Posted 22 February 2015 - 07:20 PM

You have been hired by a friendly vampire to help him figure out his diet. He can only drink certain blood types depending on his blood group compatibility. To determine this, write a program that takes as input his blood type, and the blood type of his prey and outputs whether they are safe to consume. You can assume all input to be uppercase characters.

Here is a blood type compatibility chart that will help you determine safe targets. The vampire is the recipient and the prey is the donor.
https://www.bankofmo...mpatibility.gif

Hint: Writing your cases will be much shorter and easier if you use the Java String method 'contains'.
It works like this:
	//if we have 2 strings str and str2
	String str = "abc"; 
	String str2 = "bc";

	//we can use contains on one of the strings (str) to see if it contains the other (str2)
	boolean b = str.contains(str2);


In this case, b will contain true. Since contains gives us a boolean, you can directly use it in an if statement:
if (str.contains("a")){
		//do something
	}


Think about the patterns that occur in the compatibility chart, and how you can quickly check them using contains.

I don't know how to use contains and was wondering if someone could help me understand this problem a little more and how to write it using contains.

This post has been edited by jon.kiparsky: 22 February 2015 - 07:45 PM
Reason for edit:: please use the [code] tags!


Is This A Good Question/Topic? 0
  • +

Replies To: Java String Method 'contains'

#2 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6545
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Java String Method 'contains'

Posted 22 February 2015 - 07:44 PM

Java.lang.String.contains() Method :tutorials

If you can't understand your book's documentation about contains then look for other examples and resources.

Bookmark the docs as well.

This post has been edited by andrewsw: 22 February 2015 - 07:45 PM

Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Java String Method 'contains'

Posted 22 February 2015 - 07:44 PM

What have you tried? For example, have you read the documentation that describes the method? Have you tried to write a little code using the method? (have you read the description of the method in the problem statement?)
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6545
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Java String Method 'contains'

Posted 22 February 2015 - 07:47 PM

Using code tags:

Attached Image
Was This Post Helpful? 1
  • +
  • -

#5 lillie13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 15

Re: Java String Method 'contains'

Posted 22 February 2015 - 08:26 PM

import java.util.Scanner
public class Hw2pr2
{	
	public static void main(String[] args)
	{
	double Donor;
	double Recipient;
	String str = "O-";
	String str = "O+";
	String str = "AB-";
	String str = "AB+";
	String str = "B-";
	String str = "B+";
	String str = "A-";
	String str = "A+";
	
	if (recipient = A-)
		blood types = O- and A-;
		
	
	System.out.print("Enter a blood type ");
	Scanner keyboard = new Scanner(System.in);
	
	


This is what i have so far but I don't think I'm doing it right and I'm trying to understand the contains method but even the examples aren't helping.

This post has been edited by jon.kiparsky: 22 February 2015 - 08:33 PM
Reason for edit:: [code] tags - please use them...

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Java String Method 'contains'

Posted 22 February 2015 - 08:55 PM

View Postlillie13, on 22 February 2015 - 10:26 PM, said:

This is what i have so far but I don't think I'm doing it right and I'm trying to understand the contains method but even the examples aren't helping.


What are you having trouble understanding about it? Try asking a question, so we can know what sort of answer you need.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6545
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Java String Method 'contains'

Posted 23 February 2015 - 05:18 AM

Looks like my animation was a waste of time :whistling:
Was This Post Helpful? 0
  • +
  • -

#8 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Java String Method 'contains'

Posted 23 February 2015 - 05:43 AM

To start off:

08	    String str = "O-";
09	    String str = "O+";
10	    String str = "AB-";
11	    String str = "AB+";
12	    String str = "B-";
13	    String str = "B+";
14	    String str = "A-";
15	    String str = "A+";


This won't compile because these variables are using the same name str. Can't have variables with the same name, how will Java know which one you mean? Give each one a meaningful name, eg: OPositive, ABNegative, etc

This post has been edited by mike73: 23 February 2015 - 05:43 AM

Was This Post Helpful? 0
  • +
  • -

#9 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 66
  • Joined: 28-November 13

Re: Java String Method 'contains'

Posted 23 February 2015 - 06:08 AM

this is a total mess before worrying about the contains method there's lots of things to fix in ur code
first why ur Donor and Recipient variables are of the type double ?
second as mike73you cant use the same name str for all the blood types give them different names as told by mike
third this make no sense
if (recipient = A-)
blood types = O- and A-;

and finally u needs two input the blood type of the vempire and the blood type of the victim
as for the contains method well it is explained in ur exercise i don't know if it could be explained better but i'll try
take a look at this
if (recipient = A-)

suppose that the A- is in a variable called aminus
now using it with a contains method would look like this
if(recipient.contains(aminus)

which means if recipient contain A- it will return true else it will return false
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1