int count = charFreq.get(guessChar);
int matchedChars = updatedCharFreq.get('_');
if (updatedKeyVals.contains('_')) {
if (count == matchedChars) {
;
}
if (count < matchedChars) {
;
}
else {
count = count - matchedChars;
}
Method works if count < matchedChars, and also the else statement. It just skips past the if equality statement. I have been trying to figure it out, but just can't seem to.
As commented, you neglected to chain the first if with an else.
I suggest accounting for all cases explicitly plus an extra final case that should never be reached. The extra check is for defensive programming, to guard against editing errors.
if (count == matchedChars) {
// No code needed here.
} else if (count < matchedChars) {
// No code needed here.
} else if (count > matchedChars) {
count = count - matchedChars;
} else {
throw new IllegalStateException( … ) ; // Should never reach this point.
}
It seems you only care about the case where the first number is bigger than the second. So we could shorten this code.
if (count > matchedChars) {
count = count - matchedChars;
}
Alternatively, you can use the static method Integer.compare. To quote the Javadoc:
Returns: the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
if( Integer.compare( count , matchedChars ) > 0 ) {
count = count - matchedChars;
}
Related
I'm trying to print following pattern:
0
11
0
222
0
3333
0
222
0
11
0
I want to achieve this by using recursion and a loop inside said recursive method, which gets one integer value passed. The int value determines how far this pyramid pattern goes. In the example above the int value would be 3.
I managed to get the bottom half, but I have no idea to get the upper half.
if (arg != 0) {
System.out.println("0");
for (int i = 0; i <= arg; i++) {
System.out.print(arg);
}
System.out.println();
print(arg - 1);
}
How would I be able to somehow implement some increment, which turns to a decrement to this recursion? Since I'm thinking this would be how I could achieve the above pattern.
Thank you very much in advance!
You can pass two arguments. The Max value and start value which will be zero and
then increment start value until reaches max
then decrement Max until reaches zero
call--> printline(3);
private static void printline(int input, int... vars) {
if (input == 0) {
System.out.println(0);
return;
}
int start= vars.length > 0 ? vars[0] : 0;
start++;
System.out.println("0");
for (int i = 0; (i <= start && i <= input); i++) {
System.out.print(start >= input ? input : start);
}
System.out.println();
if (start >= input) {
input--;
}
printline(input, start);
}
}
I have a program that grabs a file containing boolean values, and I'm counting the number of false values until it reaches a true value. Then, when it encounters another false value it counts them again. I have a lower threshold and a higher threshold for the number of false values, and I need to increment a counter only once when within this range. This is not a homework assignment, by the way.
private static int readBooleanValues(File textFile, int higher, int lower) throws FileNotFoundException {
Scanner scanner = new Scanner (textFile);
int counter = 0;
int secondaryCounter = 0;
while ( scanner.hasNext() ) {
String value = scanner.next();
if ( value.equalsIgnoreCase("false") ) {
counter++;
if ( counter >= lower && counter <= higher ) {
secondaryCounter++;
}
} else if ( value.equalsIgnoreCase("true") ) {
counter = 0;
}
}
return secondaryCounter;
}
If I understand your question correctly, you want to do what #WonderWorld suggested, and also replace
if ( counter >= lower && counter <= higher ) {
secondaryCounter++;
}
by
if (counter == lower)
secondaryCounter++;
if (counter == higher+1)
secondaryCounter--;
This increments secondaryCounter only once, when it first enters the range. If it later turns out that the string of false values is too long (when counter reaches higher+1), the value of secondaryCounter is corrected.
Change this:
if ( counter >= lower && counter <= higher ) {
secondaryCounter++;
}
to
if ( counter >= lower) {
secondaryCounter++;counter=0;
}
Now your range will not be in reach, counter will stay zero while Scanner is reading true values, and you can start again. Although if having too many false values invalidates the count, you will need to take further action.
Or you could place the secondaryCounter in the true part:
if ( value.equalsIgnoreCase("false") ) {
counter++;
}
} else if ( value.equalsIgnoreCase("true") ) {
if ( counter >= lower && counter <= higher ) {
secondaryCounter++;
}
counter = 0;
public Action getMove(CritterInfo info) {
count++;
Direction d = info.getDirection();
if (count < 100) {
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else {
return Action.RIGHT;
}
}
if (count >= 100) {
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else if (count / 100.0 < 2.0 && count / 100.0 >= 1.0 && !(d == Direction.EAST)) {
return Action.LEFT;
} else if (count / 100.0 < 3.0 && count / 100.0 >= 2.0 && !(d == Direction.WEST)) {
return Action.RIGHT;
} else {
return Action.HOP;
}
}
return Action.INFECT;
}
Right now I have this code that is part of my critter and i'm having problems are the if (count >= 100) part of the code. I can't get my go east and go west code to repeat itself because when I divide count by 100.0, it only works up until 299 then it just stays going west running into the wall. I've tried to set an else if statement after my go west code stating
} else if (count == 299) {
count = 0;
}
but this didn't solve my problem either. Any ideas? I just want my critter to sweep east and west over and over again.
Just use variables instead of your numbers, and change them each time their numbers are reached.Then you can create a method that changes the direction each time the number reaches 100+(100*n).
So, if you reach 200, it will check that the condition I set above is true and will therefore change directions for the next 100 numbers.
Was this what you were looking for , or did I misunderstand what you wanted?
You can use some kind of "cyclic" function like modulo (%) instead of the absolute value of count. E.g.
public Action getMove(CritterInfo info) {
count++;
Direction d = info.getDirection();
if (count < 100) {
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else {
return Action.RIGHT;
}
}
else {
int choiceOfAction = (count - 100)%200;
if (0 <= choiceOfDir && choiceOfDir < 100 && !(d == Direction.EAST)) {
return Action.LEFT;
} else if (100 <= choiceOfDir && choiceOfDir < 200 && !(d == Direction.WEST)) {
return Action.RIGHT;
} else {
return Action.HOP;
}
}
}
The line int choiceOfAction = (count - 100)%200; will yield a value of choiceOfAction with:
if count is in [100, 200[ : choiceOfAction is in [0, 100[
if count is in [200, 300[ : choiceOfAction is in [100, 200[
if count is in [300, 400[ : choiceOfAction is in [0, 100[
etc.
Note that I removed the last return in your method that was never reached, and also, that in the case above you will HOP only when you reach the limit and change direction.
I am trying to increment the counter by whatever number the user inputs. I have been at this for almost an hour now and cannot figure it out. Any ideas?
Here's what I have:
if (starting < ending) {
while (i < ending) {
++i;
System.out.println(i);
}
}
else if (starting > ending) {
while (i > ending) {
--i;
System.out.println(i);
}
}
else {
System.out.println(i);
}
No matter what increment is entered, it starts at the starting number and counts up or down by 1.
It counts up or down by 1 because of ++i and --i. The ++ and -- operators are equal to i = i + 1 and i = i - 1, or i += 1 and i -= 1 respectively.
In order to increase or decrease by the amount a user enters, use i = i + userInput and i = i - userInput, or i += userInput and i -= userInput.
For example:
int userInput = 4;
if(starting < ending) {
while(i < ending) {
i = i + userInput;
System.out.println(i);
}
// ... etc
}
In your while loop, you could put:
while (i > ending){
i -= numberUserInput;
}
This will reassign i's value with itself plus the variable that holds the number the user input (named whatever you want). For addition, you could use += instead of -=.
I have a for loop that checks for every 5th position. And at every 5th position, I'm performing an action like so (which works):
for(int i = 0; i < foo().length; i++)
{
System.out.print(i);
if(i == 5 || i == 10 || i == 15)
System.out.println();
}
Is there a way to write if statement so no matter how long foo().length is, I don't have to keep coming back to adjust it?
Use modulus(%) operator: -
if (i % 5 == 0) {
}
5 % 5 == 0, 10 % 5 == 0, ...
Since you are using a for loop, you can simply change your increment from i++ to i += 5, and leave the if condition.
for (int i = 0; i < someNum; i += 5) {
// No need to check for `i` against `modulus 5`.
}
You can use next for witout if statement
for(int i = 0; i < foo().length; i+=5)
{
adding 5 to i step by step
Use the following:
if ( i % 5 == 0 )
if you want to put it in a single print statement this would work too.
for(int i = 0; i < foo().length; i++)
{
System.out.printf("%d%s", i, (i%5==0) ? "\n" : "");
}