Scala: given multiple strings, use an if statement to find if given string is in set 1,2,or none
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
add a comment |
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
Hi @YouSun, the error is clear you're passing anString
to your function, but it requires aList[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
add a comment |
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
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
scala
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 anString
to your function, but it requires aList[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
add a comment |
Hi @YouSun, the error is clear you're passing anString
to your function, but it requires aList[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
add a comment |
2 Answers
2
active
oldest
votes
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")
}
}
}
Thanks, however the keyword contain is not a member of Set[String]
– YouSun
Nov 21 '18 at 20:02
@YouSun my bad, it iscontains
, I edited the answer - my apologies for the confusion. PS: in this casecontains
is not a keyword, just a method name.
– Luis Miguel Mejía Suárez
Nov 21 '18 at 20:13
add a comment |
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
.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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")
}
}
}
Thanks, however the keyword contain is not a member of Set[String]
– YouSun
Nov 21 '18 at 20:02
@YouSun my bad, it iscontains
, I edited the answer - my apologies for the confusion. PS: in this casecontains
is not a keyword, just a method name.
– Luis Miguel Mejía Suárez
Nov 21 '18 at 20:13
add a comment |
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")
}
}
}
Thanks, however the keyword contain is not a member of Set[String]
– YouSun
Nov 21 '18 at 20:02
@YouSun my bad, it iscontains
, I edited the answer - my apologies for the confusion. PS: in this casecontains
is not a keyword, just a method name.
– Luis Miguel Mejía Suárez
Nov 21 '18 at 20:13
add a comment |
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")
}
}
}
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")
}
}
}
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 iscontains
, I edited the answer - my apologies for the confusion. PS: in this casecontains
is not a keyword, just a method name.
– Luis Miguel Mejía Suárez
Nov 21 '18 at 20:13
add a comment |
Thanks, however the keyword contain is not a member of Set[String]
– YouSun
Nov 21 '18 at 20:02
@YouSun my bad, it iscontains
, I edited the answer - my apologies for the confusion. PS: in this casecontains
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
add a comment |
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
.
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
add a comment |
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
.
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
add a comment |
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
.
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
.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Hi @YouSun, the error is clear you're passing an
String
to your function, but it requires aList[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