My intention is to let the user decide which method to use by cheking its input.
I have the following code:
try {
String test = scan.next();
if(test == "y") {
//do stuff
}
else if (test == "n") {
//do stuff
}
} catch (Exception e) {
System.out.println("false");
}
I tried to analyze with the debugger. It is not jumping in the if-statement.
can you help me out here?
You need to use equals to compare strings
if(test == "y")
becomes
if (test.equals("y"))
Same for "n" obviously.
== test for reference equality, but you're looking for value equality, that's why you should use equals, and not ==.
Related
I am writing some tests for a Java method which contains a switch statements but it seems that the "default statement" does not work. I only accept: Yes, No, Maybe. Everything else should return Maybe. My test always return the user input, does not matter what the user typed, so I guess my switch statement is not correct.
I have tried to move the default statement on the top
#Override
public String choice(String ans) {
getChoice = ans;
switch (ans) {
case "Yes":
break;
case "No":
break;
default:
getChoice = "Maybe";
}
return getChoice;
}
Thank you!
Your switch should work... As Kyle tells you "what are you overriding ?"
But why a switch when you can perform an "if then else" ?
Public String choice (String choice) {
If (choice.equals("yes") || choice.equals("no") {
return choice;
else {
return "maybe";
}
}
Switch is fine for multiple choices
Is it possible to have a switch-case statement with more than a variable in groovy? I tried with tuples but the case part doesn't accept more than one argument.
I am trying to avoid several nested if statements so instead of
if (a==1) {
if (b==2) {
if (c==3) {
// do something
}
}
}
else {
if (a==4) {
if (b==5) {
if (c==6) {
//do something else
}
}
}
}
Can I do:
switch(a,b,c) {
case : (1,2,3) // if a==1, b==2 and c==3
// do something
...
case : (4,5,6)
// do something else
...
}
}
Groovy is just dirty java, you don't need any class definition. everything you write in a java method you can write it directly in groovy.
switch (num) {
case 1:
case 2:
case 3:
System.out.println("1 through 3");
break;
case 6:
case 7:
case 8:
System.out.println("6 through 8");
break;
}
To answer your question, inside the switch we need an expression, not function parameters.
Based on your edit, I believe that this should work:
if (a == 1 && b == 2 && c == 3) {
// do something
} else if (a == 4 && b == 5 && c == 6) {
// do something else
}
If you want a switch statement instead, that's possible:
def val = [a, b, c]
switch (val) {
case {it == [1, 2, 3]}:
// something
break;
case {it == [4, 5, 6]}:
// something else
break;
class Solution{
static void main (String...args){
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in))
def val=br.readLine()
switch(val){
case('E0'):
println "Basic"
break;
default:
break;
case('E1'):
println "Inter"
break;
case('E2'):
println "Advance"
break;
default:
println "not defined"
}
}
}
My problem is that my switch (case) statement keeps evaluating an extra switch case statement. And I don't understand why it's doing this.
My problem is in case 10:
It's always being evaluated, doesnt matter what case # i choose. It will always run to case 10: and evaluate if it's true or not.
I'v been going over it and I don't understand what's going on. Why would it read case 10.
public boolean checkIfPossible(double x, double a, double y) {
boolean pass;
int value = spinnerA.getSelectedItemPosition();
switch (value) {
case 1:
if (x > a && a == 0) {
etX.setError("Error1 ");
pass = false;
break;
} else if (x == a) {
etX.setError("Error2);
pass = false;
break;
}
case 10:
if (x == y) {
etX.setError("Error3");
pass = false;
break;
}
default:
pass = true;
}
return pass;
}
The solution to your problem is to put a break statement after your if and else statements in your case 1 like this:
case 1:
if (x > a && a == 0) {
etX.setError("Error1 ");
pass = false;
}
else if (x == a) {
etX.setError("Error2);
pass = false;
}
break;
The most likely reason you are having this problem is because the conditions for your if and else-if are both not satisfied. There is no break statement to stop the flow (since the if and else-if blocks are not executed). That's why your code eventually spills over to the next case. Try logging the value of x and a to verify.
I hope this helps.. Merry coding!
How do I check if a String includes a specific Character?
For example:
if !emailString.hasCharacter("#") {
println("Email must contain at sign.")
}
You can use the free-standing find function, like this:
let s = "hello"
if (find(s, "x") != nil) {
println("Found X")
}
if (find(s, "l") != nil) {
println("Found L")
}
Here you go:
if emailString.rangeOfString("#") != nil{
println("# exists")
}
You can use this
if emailString.rangeOfString("#") == nil {
println("Email must contain at sign.")
}
I have a tree structure and I want to find all nodes matching a given criteria. Each time I call the find function, it returns next matching node. Children are searched by recursive function call.
For some reason a key comparison of pointers fails for this implementation. Please see the code below, I have pointed out the failing comparison.
HtmlTag* HtmlContent::FindTag(string tagName, string tagParameterContent)
{
if (tagName.empty() && tagParameterContent.empty())
return NULL;
if (this->startTag == NULL)
return NULL;
this->findContinue = this->FindChildren(this->startTag, &tagName, &tagParameterContent);
return this->findContinue;
}
HtmlTag* HtmlContent::FindChildren(HtmlTag* firstTag, string* tagName, string* tagParameterContent)
{
HtmlTag* currentTag = firstTag;
HtmlTag* childrenFound = NULL;
while (currentTag != NULL)
{
if (!tagName->empty() && *tagName == currentTag->tagName)
{
if (tagParameterContent->empty() || currentTag->tagParameters.find(*tagParameterContent, 0) != -1)
{
if (this->findContinue == NULL)
break; // break now when found
else if (this->findContinue == currentTag) // TODO why this fails?
this->findContinue == NULL; // break on next find
}
}
if (currentTag->pFirstChild != NULL)
{
childrenFound = this->FindChildren(currentTag->pFirstChild, tagName, tagParameterContent);
if (childrenFound != NULL)
{
currentTag = childrenFound;
break;
}
}
currentTag = currentTag->pNextSibling;
}
return currentTag;
}
VC++ compiler accepts this code but for some reason I can't put a breakpoint on this comparison. I guess this is optimized out, but why? Why this comparison fails?
I think that you shoud replace == with = in assignment after comparison. Compiler optimalized this whole section because it doesnt do anything useful.