I have been working on a program to count vowels in inputted text. It uses this method to recursively add to a vowel count every time a vowel is found. However, I get an out of bounds error every time lastPos reaches negative 1. How can I get this to stop once lastPos reaches -1?
static int R_countVowels(String s, int lastPos)
{
switch (s.charAt(lastPos))
{ case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U': return (1 + R_countVowels(s, --lastPos));
default: return R_countVowels(s, --lastPos);
}
}
I'm assuming this is homework so no code.
Recursive functions require a base case. You need to define your base case to return 0 (no vowels) for an empty input and check for the base case before your inductive step (recursive call).
Insert an if before the switch():
if (lastpos < 0) {
// stop the recursion
}
Related
In a switch(string) can i make a case to start with a specific char f.e case('N') and then followed by other characters that i don't care about them? f.e NOC
case("N**"):
case("1**"):
case("0**"):
BalancedTrenary term = new BalancedTrenary(parts[i]);
You can use charAt and use char in the cases
String str = "NOC";
switch (str.charAt(0)) {
case 'N':
// do something here
break;
}
Be aware that the case is case sensitive. To make it ignored case use another case with n
switch (str.charAt(0)) {
case 'N':
case 'n':
// do something here
break;
}
I just run over something strange in my java code:
switch (result) {
case 0:
result_amount = 500;
case 1:
result_amount = 600;
case -1:
result_amount = 700;
}
result is from primitive type int.
For value 1 case 1 and case -1 are executed.
Is this a normal switch case behaviour? If yes: why?
You need to use the break keyword after a case block:
switch (result) {
case 0:
result_amount = all_amounts[i];
break;
case 1:
result_amount = all_amounts[i];
break;
case -1:
result_amount = all_amounts[i+1];
}
The switch statement will make a jump to the correct case tag, then execute all the code that follow, ignoring potential other case tags. You can consider the switch statement just like a goto one.
Fall Through.
quoting from docs
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
You have to add the break after each case.
case 1:
result_amount = 600;
break;
Remember:
This is a mistake that almost every beginner will make. That's why I like C# more. It doesn't allow "fall-through".
What you did wrong is you fell-through the switch statement. Try using 0 as the value of result. It will go through all the cases. When a switch case finishes execution, the next case gets executed. That's why we need to add a break; statement for each case of the switch statement.
switch (result) {
case 0:
result_amount = 500;
break;
case 1:
result_amount = 600;
break;
case -1:
result_amount = 700;
break;
}
But sometimes we want fall-through. For example when we want to calculate number of days in a month:
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
days = 28;
case 4:
case 6:
//Lots of code...
}
Without break statement next cases will be executed.
Generally Case Label Should be Integer / Constant
Negative numbers having no decimal point are also considered as Integer
Thus it is not necessary to have Case Labels greater than Zero
This is fall through condition.
switch (result) {
case 0:
result_amount = all_amounts[i];
case 1:
result_amount = all_amounts[i];
case -1:
result_amount = all_amounts[i+1];
}
To avoid it you must put break statement
switch (result) {
case 0:
result_amount = all_amounts[i];
break;
case 1:
result_amount = all_amounts[i];
break;
case -1:
result_amount = all_amounts[i+1];
break;
}
Now only matching case will be executed.
You should include break and default to run program in a proper way. Let me give you a example of it.
switch (result) {
case 0:
result_amount = 500;
break;
case 1:
result_amount = 600;
break;
case -1:
result_amount = 700;
break;
}
This question already has answers here:
In a switch statement, why are all the cases being executed?
(8 answers)
Closed 1 year ago.
I am making an app that has a grid of images with text and each one opens a different activity. It works fine but just for design purposes I want to replace my if-else statements with switch statements (which I assume I can do) however it doesn't work. Right now my working code to set the label on each image is:
if(position == 0)
textView.setText(R.string.zero);
else if(position == 1)
textView.setText(R.string.one);
else if(position == 2)
textView.setText(R.string.two);
else if(position == 3)
textView.setText(R.string.three);
else if(position == 4)
textView.setText(R.string.four);
else if(position == 5)
textView.setText(R.string.five);
ect....
I want to use:
switch(position)
case 0:
textView.setText(R.string.zero);
case 1:
textView.setText(R.string.one);
case 2:
textView.setText(R.string.two);
case 3:
textView.setText(R.string.three);
case 4:
textView.setText(R.string.four);
but when I did that ever label was the last one that I defined (in my example it would be "four"). I also have a similar code for each object to start a different intent with the position variable however that does the opposite and makes every intent equal to the first one. Is my syntax wrong or will this not work for my situation?
You need to break; after each statement in a case, otherwise execution flows down (all cases below the one you want will also get called), so you'll always get the last case.
switch(position) {
case 0:
textView.setText(R.string.zero);
break;
case 1:
textView.setText(R.string.one);
break;
case 2:
textView.setText(R.string.two);
break;
case 3:
textView.setText(R.string.three);
break;
case 4:
textView.setText(R.string.four);
break;
}
Here's the official tutorial explaining when to and when not to use break;.
You need to break; after each branch:
switch (position) {
case 0:
textView.setText(R.string.zero);
break; // <-- here
// etc
}
Legitimate uses of switch when you don't break exist, those are called fall throughs; or because you return or throw.:
switch (someNumber) {
case 0:
return 0;
// no need for break here
case 1:
throw new IllegalArgumentException();
// no need to break here
case 2:
System.out.println("Oh, I got two!");
// fall through
case 3:
return 3;
default:
System.out.println("Meh")
// No need to break: last possible branch
}
return -1;
will return 3 even if you enter 2.
But otherwise you need to break.
Using a break statement after each case should fix the problem. I would also use a default statement as well after the last case.
This is the solution. You need to use break to avoid going through each case:
switch(position)
case 0:
textView.setText(R.string.zero);
break;
case 1:
textView.setText(R.string.one);
break;
case 2:
textView.setText(R.string.two);
break;
case 3:
textView.setText(R.string.three);
break;
case 4:
textView.setText(R.string.four);
break;
I would recommend to read the oracle documentation about the switch statement.
You need to use break statement after eace case operations. In a switch-case statement if you dont use a break statement then all the cases after that specific one will be executed also
case 0:
textView.setText(R.string.zero);
break;
Don't forget to put break; after each case: like that:
switch(position){
case 0:
textView.setText(R.string.zero);
break;
case 1:
textView.setText(R.string.one);
break;
case 2:
textView.setText(R.string.two);
break;
case 3:
textView.setText(R.string.three);
break;
case 4:
textView.setText(R.string.four);
break;
}
In the Switch-case statements, you need to put break; after each case.
switch(position){
case 0:
textView.setText(R.string.zero);
break;
case 1:
textView.setText(R.string.one);
break;
case 2:
textView.setText(R.string.two);
break;
case 3:
textView.setText(R.string.three);
break;
case 4:
textView.setText(R.string.four);
break;
default:
System.out.println("not available");
}
Also you need to put default: at last, because when all case are wrong that time perform default: action.
In the switch-case statement not forgot about break; and default action.
Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
Switch is faster than if-else statement
Bottom line : Default is optional(works like else statement in switch), Break is mandatory.
Interesting fact: you won't see any compilation error, even if you forgot to place the break statement.
The switch needs a break with in each case. But in your case it could be done much simpler by defining an array as shown below.
String values = {R.string.zero, R.string.one, R.string.two, ... };
Use this to populate textView : textView.setText(values[position]);
I've seen similar questions about this issue, but what happens to me is a little different;
I am developing a remote control application and I'm sending keystrokes to my computer.
The Robot class in java only accepts VK_CODES for keystrokes, so I have to translate non ascii characters into keystrokes combinations, like this:
public void type(char character) {
switch (character) {
case 'a': doType(KeyEvent.VK_A); break;
case 'á': doType(KeyEvent.VK_A); break;
case 'à': doType(KeyEvent.VK_A); break;
case 'ä': doType(KeyEvent.VK_A); break;
case 'â': doType(KeyEvent.VK_A); break;
case 'b': doType(KeyEvent.VK_B); break;
case 'c': doType(KeyEvent.VK_C); break;
case 'd': doType(KeyEvent.VK_D); break;
case '{': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET); break;
case '}': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_CLOSE_BRACKET); break;
case '|': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH); break;
etc...
}
private void doType(int... keyCodes) {
doType(keyCodes, 0, keyCodes.length);
}
private void doType(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
This works well, but when I try to combine ALT+numbers characters (for example to type the '#' character) I do:
case '#': doType(KeyEvent.VK_ALT,KeyEvent.VK_6,KeyEvent.VK_4); break;
It won't type it. If I type it directly with my keyboard, it works.
Is there a reason for this? How can I make a Robot instance to accept all the unicode characters and not just ascii? Is there a better way to do what I am doing?
Thanks for reading and sorry for my English!
I found out that the problem was, I was entering the VK_number directly.
Now it works like a charm using the VK_NUMPAD keys:
case '#': doType(KeyEvent.VK_ALT,KeyEvent.VK_NUMPAD6,KeyEvent.VK_NUMPAD4); break;
case '#': doType(KeyEvent.VK_ALT,KeyEvent.VK_NUMPAD3,KeyEvent.VK_NUMPAD5); break;
etc..
I have looked at a lot of post here on stackoverflow concerning this problem, I have found partial solutions but so far I have yet to find a solution that works for me.
new BigDecimal("3324679375210329505").toString(2);
Seems to work best for me (from: Convert a large 2^63 decimal to binary) but I do need leading and trailing zeros. Is there anyway I can convert a large (bigger than a long) hex to a binary (String) representation?
Thanks in advance.
The BigInteger class handles arbitrarily large numbers.
Trailing zeroes are allready handled. To handle leading zeroes, simply add a "1" at the front, which ensures a leading "1" bit, then strip it back off again:
String bits = new BigInteger("1" + hex, 16).toStting(2).substring(1);
You need leading zeroes? I don't know of a built-in function, but you can easily implement it yourself:
public static String hex2binary(String hex) {
StringBuilder result = new StringBuilder(hex.length() * 4);
for (char c : hex.toUpperCase().toCharArray()) {
switch (c) {
case '0': result.append("0000"); break;
case '1': result.append("0001"); break;
case '2': result.append("0010"); break;
case '3': result.append("0011"); break;
case '4': result.append("0100"); break;
case '5': result.append("0101"); break;
case '6': result.append("0110"); break;
case '7': result.append("0111"); break;
case '8': result.append("1000"); break;
case '9': result.append("1001"); break;
case 'A': result.append("1010"); break;
case 'B': result.append("1011"); break;
case 'C': result.append("1100"); break;
case 'D': result.append("1101"); break;
case 'E': result.append("1110"); break;
case 'F': result.append("1111"); break;
default: throw new IllegalArgumentException("Invalid hex: '" + hex + "'");
}
}
return result.toString();
}