Scala: given multiple strings, use an if statement to find if given string is in set 1,2,or none












1















Given Strings: "A", "BDS", "DE", "BO", "IID"
Here is what I have:



def givenStrings(code: List[String]) = {
//val code = List("A", "BDS", "DE", "BO", "IID")
if (code.contains("A", "BDS"))
"It is in the first set";
else if(security_code.contains("DE", "BO", "IID"))
"It is in the second set";
else
"Not given";
}

givenStrings(List("A")) -> Should result in "It is in the first set"
givenStrings(List("DE")) -> Should result in "It is in the second set"
givenStrings(List("OOOOOO")) -> Should result in "Not given"


The error I get is
type mismatch;
found :String("A")
required: List[String]










share|improve this question

























  • Hi @YouSun, the error is clear you're passing an String to your function, but it requires a List[String], because that is how you declared it here: def givenStrings(code: List[String]).

    – Luis Miguel Mejía Suárez
    Nov 21 '18 at 19:27











  • also code and securitycode are misleading

    – stack0114106
    Nov 21 '18 at 19:27
















1















Given Strings: "A", "BDS", "DE", "BO", "IID"
Here is what I have:



def givenStrings(code: List[String]) = {
//val code = List("A", "BDS", "DE", "BO", "IID")
if (code.contains("A", "BDS"))
"It is in the first set";
else if(security_code.contains("DE", "BO", "IID"))
"It is in the second set";
else
"Not given";
}

givenStrings(List("A")) -> Should result in "It is in the first set"
givenStrings(List("DE")) -> Should result in "It is in the second set"
givenStrings(List("OOOOOO")) -> Should result in "Not given"


The error I get is
type mismatch;
found :String("A")
required: List[String]










share|improve this question

























  • Hi @YouSun, the error is clear you're passing an String to your function, but it requires a List[String], because that is how you declared it here: def givenStrings(code: List[String]).

    – Luis Miguel Mejía Suárez
    Nov 21 '18 at 19:27











  • also code and securitycode are misleading

    – stack0114106
    Nov 21 '18 at 19:27














1












1








1








Given Strings: "A", "BDS", "DE", "BO", "IID"
Here is what I have:



def givenStrings(code: List[String]) = {
//val code = List("A", "BDS", "DE", "BO", "IID")
if (code.contains("A", "BDS"))
"It is in the first set";
else if(security_code.contains("DE", "BO", "IID"))
"It is in the second set";
else
"Not given";
}

givenStrings(List("A")) -> Should result in "It is in the first set"
givenStrings(List("DE")) -> Should result in "It is in the second set"
givenStrings(List("OOOOOO")) -> Should result in "Not given"


The error I get is
type mismatch;
found :String("A")
required: List[String]










share|improve this question
















Given Strings: "A", "BDS", "DE", "BO", "IID"
Here is what I have:



def givenStrings(code: List[String]) = {
//val code = List("A", "BDS", "DE", "BO", "IID")
if (code.contains("A", "BDS"))
"It is in the first set";
else if(security_code.contains("DE", "BO", "IID"))
"It is in the second set";
else
"Not given";
}

givenStrings(List("A")) -> Should result in "It is in the first set"
givenStrings(List("DE")) -> Should result in "It is in the second set"
givenStrings(List("OOOOOO")) -> Should result in "Not given"


The error I get is
type mismatch;
found :String("A")
required: List[String]







scala






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:40









SCouto

4,00731428




4,00731428










asked Nov 21 '18 at 19:21









YouSunYouSun

133




133













  • Hi @YouSun, the error is clear you're passing an String to your function, but it requires a List[String], because that is how you declared it here: def givenStrings(code: List[String]).

    – Luis Miguel Mejía Suárez
    Nov 21 '18 at 19:27











  • also code and securitycode are misleading

    – stack0114106
    Nov 21 '18 at 19:27



















  • Hi @YouSun, the error is clear you're passing an String to your function, but it requires a List[String], because that is how you declared it here: def givenStrings(code: List[String]).

    – Luis Miguel Mejía Suárez
    Nov 21 '18 at 19:27











  • also code and securitycode are misleading

    – stack0114106
    Nov 21 '18 at 19:27

















Hi @YouSun, the error is clear you're passing an String to your function, but it requires a List[String], because that is how you declared it here: def givenStrings(code: List[String]).

– Luis Miguel Mejía Suárez
Nov 21 '18 at 19:27





Hi @YouSun, the error is clear you're passing an String to your function, but it requires a List[String], because that is how you declared it here: def givenStrings(code: List[String]).

– Luis Miguel Mejía Suárez
Nov 21 '18 at 19:27













also code and securitycode are misleading

– stack0114106
Nov 21 '18 at 19:27





also code and securitycode are misleading

– stack0114106
Nov 21 '18 at 19:27












2 Answers
2






active

oldest

votes


















0














The problem is that you're checking if your string contains the list, while what you want is the opposite.



I believe this is more close to what you need.



object Codes {
private val codes: Set[String] = Set("A", "BDS")
private val securityCodes: Set[String] = Set("DE", "BO", "IID")
def givenString(code: String): Unit = {
if (codes.contains(code)) {
println("It is in the first set")
} else if (securityCodes.contains(code)) {
println("It is in the second set")
} else {
println("Not given")
}
}
}





share|improve this answer


























  • Thanks, however the keyword contain is not a member of Set[String]

    – YouSun
    Nov 21 '18 at 20:02











  • @YouSun my bad, it is contains, I edited the answer - my apologies for the confusion. PS: in this case contains is not a keyword, just a method name.

    – Luis Miguel Mejía Suárez
    Nov 21 '18 at 20:13



















1














The method you wrote is expecting a list, but you are passing in a string. So change your method signature to:



def givenStrings(code: String) = {


and now you'll have a method that expects a string, and won't complain to you when you give it one.



Now, you also don't want to over-write the value of code that is passed in to your function. You should remove that line.



Finally, you can't just start calling the variable security_code. So change that back to simply code.






share|improve this answer
























  • Thanks, I have changed it to def givenStrings(code: String) = { if (code.contains("A", "BDS")) "It is in the first set"; else if(code.contains("DE", "BO", "IID")) "It is in the second set"; else "Not given"; } givenStrings("A") however I get "Not given" when it should be "It is in the first set"

    – YouSun
    Nov 21 '18 at 19:36













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419178%2fscala-given-multiple-strings-use-an-if-statement-to-find-if-given-string-is-in%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The problem is that you're checking if your string contains the list, while what you want is the opposite.



I believe this is more close to what you need.



object Codes {
private val codes: Set[String] = Set("A", "BDS")
private val securityCodes: Set[String] = Set("DE", "BO", "IID")
def givenString(code: String): Unit = {
if (codes.contains(code)) {
println("It is in the first set")
} else if (securityCodes.contains(code)) {
println("It is in the second set")
} else {
println("Not given")
}
}
}





share|improve this answer


























  • Thanks, however the keyword contain is not a member of Set[String]

    – YouSun
    Nov 21 '18 at 20:02











  • @YouSun my bad, it is contains, I edited the answer - my apologies for the confusion. PS: in this case contains is not a keyword, just a method name.

    – Luis Miguel Mejía Suárez
    Nov 21 '18 at 20:13
















0














The problem is that you're checking if your string contains the list, while what you want is the opposite.



I believe this is more close to what you need.



object Codes {
private val codes: Set[String] = Set("A", "BDS")
private val securityCodes: Set[String] = Set("DE", "BO", "IID")
def givenString(code: String): Unit = {
if (codes.contains(code)) {
println("It is in the first set")
} else if (securityCodes.contains(code)) {
println("It is in the second set")
} else {
println("Not given")
}
}
}





share|improve this answer


























  • Thanks, however the keyword contain is not a member of Set[String]

    – YouSun
    Nov 21 '18 at 20:02











  • @YouSun my bad, it is contains, I edited the answer - my apologies for the confusion. PS: in this case contains is not a keyword, just a method name.

    – Luis Miguel Mejía Suárez
    Nov 21 '18 at 20:13














0












0








0







The problem is that you're checking if your string contains the list, while what you want is the opposite.



I believe this is more close to what you need.



object Codes {
private val codes: Set[String] = Set("A", "BDS")
private val securityCodes: Set[String] = Set("DE", "BO", "IID")
def givenString(code: String): Unit = {
if (codes.contains(code)) {
println("It is in the first set")
} else if (securityCodes.contains(code)) {
println("It is in the second set")
} else {
println("Not given")
}
}
}





share|improve this answer















The problem is that you're checking if your string contains the list, while what you want is the opposite.



I believe this is more close to what you need.



object Codes {
private val codes: Set[String] = Set("A", "BDS")
private val securityCodes: Set[String] = Set("DE", "BO", "IID")
def givenString(code: String): Unit = {
if (codes.contains(code)) {
println("It is in the first set")
} else if (securityCodes.contains(code)) {
println("It is in the second set")
} else {
println("Not given")
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 20:12

























answered Nov 21 '18 at 19:47









Luis Miguel Mejía SuárezLuis Miguel Mejía Suárez

2,7712923




2,7712923













  • Thanks, however the keyword contain is not a member of Set[String]

    – YouSun
    Nov 21 '18 at 20:02











  • @YouSun my bad, it is contains, I edited the answer - my apologies for the confusion. PS: in this case contains is not a keyword, just a method name.

    – Luis Miguel Mejía Suárez
    Nov 21 '18 at 20:13



















  • Thanks, however the keyword contain is not a member of Set[String]

    – YouSun
    Nov 21 '18 at 20:02











  • @YouSun my bad, it is contains, I edited the answer - my apologies for the confusion. PS: in this case contains is not a keyword, just a method name.

    – Luis Miguel Mejía Suárez
    Nov 21 '18 at 20:13

















Thanks, however the keyword contain is not a member of Set[String]

– YouSun
Nov 21 '18 at 20:02





Thanks, however the keyword contain is not a member of Set[String]

– YouSun
Nov 21 '18 at 20:02













@YouSun my bad, it is contains, I edited the answer - my apologies for the confusion. PS: in this case contains is not a keyword, just a method name.

– Luis Miguel Mejía Suárez
Nov 21 '18 at 20:13





@YouSun my bad, it is contains, I edited the answer - my apologies for the confusion. PS: in this case contains is not a keyword, just a method name.

– Luis Miguel Mejía Suárez
Nov 21 '18 at 20:13













1














The method you wrote is expecting a list, but you are passing in a string. So change your method signature to:



def givenStrings(code: String) = {


and now you'll have a method that expects a string, and won't complain to you when you give it one.



Now, you also don't want to over-write the value of code that is passed in to your function. You should remove that line.



Finally, you can't just start calling the variable security_code. So change that back to simply code.






share|improve this answer
























  • Thanks, I have changed it to def givenStrings(code: String) = { if (code.contains("A", "BDS")) "It is in the first set"; else if(code.contains("DE", "BO", "IID")) "It is in the second set"; else "Not given"; } givenStrings("A") however I get "Not given" when it should be "It is in the first set"

    – YouSun
    Nov 21 '18 at 19:36


















1














The method you wrote is expecting a list, but you are passing in a string. So change your method signature to:



def givenStrings(code: String) = {


and now you'll have a method that expects a string, and won't complain to you when you give it one.



Now, you also don't want to over-write the value of code that is passed in to your function. You should remove that line.



Finally, you can't just start calling the variable security_code. So change that back to simply code.






share|improve this answer
























  • Thanks, I have changed it to def givenStrings(code: String) = { if (code.contains("A", "BDS")) "It is in the first set"; else if(code.contains("DE", "BO", "IID")) "It is in the second set"; else "Not given"; } givenStrings("A") however I get "Not given" when it should be "It is in the first set"

    – YouSun
    Nov 21 '18 at 19:36
















1












1








1







The method you wrote is expecting a list, but you are passing in a string. So change your method signature to:



def givenStrings(code: String) = {


and now you'll have a method that expects a string, and won't complain to you when you give it one.



Now, you also don't want to over-write the value of code that is passed in to your function. You should remove that line.



Finally, you can't just start calling the variable security_code. So change that back to simply code.






share|improve this answer













The method you wrote is expecting a list, but you are passing in a string. So change your method signature to:



def givenStrings(code: String) = {


and now you'll have a method that expects a string, and won't complain to you when you give it one.



Now, you also don't want to over-write the value of code that is passed in to your function. You should remove that line.



Finally, you can't just start calling the variable security_code. So change that back to simply code.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 19:26









MetropolisMetropolis

1,240927




1,240927













  • Thanks, I have changed it to def givenStrings(code: String) = { if (code.contains("A", "BDS")) "It is in the first set"; else if(code.contains("DE", "BO", "IID")) "It is in the second set"; else "Not given"; } givenStrings("A") however I get "Not given" when it should be "It is in the first set"

    – YouSun
    Nov 21 '18 at 19:36





















  • Thanks, I have changed it to def givenStrings(code: String) = { if (code.contains("A", "BDS")) "It is in the first set"; else if(code.contains("DE", "BO", "IID")) "It is in the second set"; else "Not given"; } givenStrings("A") however I get "Not given" when it should be "It is in the first set"

    – YouSun
    Nov 21 '18 at 19:36



















Thanks, I have changed it to def givenStrings(code: String) = { if (code.contains("A", "BDS")) "It is in the first set"; else if(code.contains("DE", "BO", "IID")) "It is in the second set"; else "Not given"; } givenStrings("A") however I get "Not given" when it should be "It is in the first set"

– YouSun
Nov 21 '18 at 19:36







Thanks, I have changed it to def givenStrings(code: String) = { if (code.contains("A", "BDS")) "It is in the first set"; else if(code.contains("DE", "BO", "IID")) "It is in the second set"; else "Not given"; } givenStrings("A") however I get "Not given" when it should be "It is in the first set"

– YouSun
Nov 21 '18 at 19:36




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419178%2fscala-given-multiple-strings-use-an-if-statement-to-find-if-given-string-is-in%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?