Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
What I am doing: Replacing odd numbers(values) in array with zeros.
Problem: when executing following code it replaces only positive numbers, ignoring negative.
Code:
public static int[] nullOddValues(int[] array) {
int[] resultArray = new int[array.length];
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 != 0) {
resultArray[i] = 0;
} else {
resultArray[i] = array[i];
}
}
return resultArray;
}
You test the loop variable i for oddity when you (probably) want to test array[i]
if (array[i] % 2 != 0) {
Using modulus on X returns [0-X)
positive X (X > 0) -> it will return 0+
negative X (X < 0) -> it will return 0-
For your problem, I recommend using a bit operator
if((i & 1) == 0)
This will return true for even numbers (positive and negative), since it is checking the last bit of the number.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
the code compiles and also works for any string that is passed but it doesn't work for checking the vowels. It throws a String out of bounds error and i have no idea why. the check for consonants is working.
Here is the code:
public String catchword(String word){
int x = 0;
for(x=0; x<word.length()+1; x++){
boolean v = Vowel(word.charAt(x));
boolean c = Consonant(word.charAt(x));
if (x<word.length()-1){
v = Vowel(word.charAt(x+1));
} else{
v = true;
}
if (c == true && v == true){
word = word.substring(0,x+1) + "op" + word.substring(x+1,word.length());
x = x+3;
}
}
System.out.print(word);
return word;
}
In for(x=0; x<word.length()+1; x++)
x<word.length()+1
Should be
x<word.length()-1
The max index of word is word.length()-1
You will also have problem in word.charAt(x+1). word.length() - 1 is the max index, when x will be equal to it you will get again IndexOutOfBoundsException.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
my while loop :
while (j =>0&& (courseArray[j].compareByCourse(value)) >=0 ){
}
is giving me this error in netbeans: int cannot be converted to boolean
method:
public static void insertionSort(Course[] courseArray){
Course value ; // the next value from the unsorted list to be inserted into the sorted list
int i; // i is a pointer to an item in the unsorted list
int j; // j is a pointer to an item in the sorted list; originally the sorted list is just a[0]
int compare;
for (i=1; i<courseArray.length; i++){
value = courseArray[i];
j = i -1;
compare = courseArray[j].compareByCourse(value);
while (j =>0&& (courseArray[j].compareByCourse(value)) >=0 ){
}
}
}
compareByCourse method:
//method to compare Courses by course name
int compareByCourse(Course other){
return this.course.compareTo(other.getCourse());
}
j is an int, the return value is an int, 0 is an int, so where is the boolean?
It seems like you have confused the operator >= with => (which is not a valid operator in Java). Try changing the while condition to:
while (j >= 0 && (courseArray[j].compareByCourse(value)) >= 0 )
Also if you are assigning j to:
compare = courseArray[j].compareByCourse(value);
use it in the while loop (you can also use it directly).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Here is some basic code:
public static void main(String[] args) {
String string = "Hello!";
System.out.println("First loop.");
for (int i = 0; i < string.length(); i++) {
System.out.println("g");
}
System.out.println("Second loop.");
for (int i = (string.length() - 1); i <= 0; i--) {
System.out.println("g");
}
}
For some reason, the program won't go through the second loop at all. This is somewhat strange. Can you explain this, and how to fix it?
Your second loop should be looping backwards, while the index is still greater than or equal to zero, not less than or equal to zero. With <= 0, i is greater than zero on the first evaluation and the loop never runs.
Try:
for (int i = (string.length() - 1); i >= 0; i--) {
Change the for condition,the i is initial with value greater than 0 (length-1) and there is condition i <= 0 which is true in case length is equal to 1.But the length of string is 6 so change the condition as below :
for (int i = (string.length() - 1); i >= 0; i--) {
System.out.println("g");
}
Your problem is the condition in the second for loop
i <= 0
never happens. I don't understand why you would want to check that.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
For some reason the true and false are considered "incompatible types". Am I only suppose to run this through a boolean method? What's wrong with it.
for(int i = 0; i < array.length ; i++)
{
int val = (array[i] % 2);
if(val == 0)
array[i] = true;
else
array[i] = false;
}
Well array is probably an int[], given that you're using array[i] % 2 and assigning the result to an int.
There's no conversion from boolean to int, so you can't store your result back in the int[] array. It's not clear what you're trying to do, but that's why it's not compiling.
Aside
If you had a separate boolean[] of the same size, that would work - although it would be more simply written as:
boolean[] even = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
even[i] = (array[i] % 2) == 0;
}
Any time you find yourself with:
if (someCondition) {
doSomething(true);
} else {
doSomething(false);
}
you should consider refactoring it to:
doSomething(someCondition);
Your array contains wrong types:
int[] a = {1, 2, 4};
boolean[] b = {true, false};
b[0] = 1; //error
a[0] = 1; //ok
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to write a section of code that outputs the binary value up to a user defined number (0-7).
We can't use .toBinaryString it has to be using loops (for loops preferably).
The output should be three columns with filler zeros.
Ex) User enters 7
001
010
100
101
110
111
It seems like it should be so simple but I cant seem to get it right.
for (int i = 1; i <= input; i++) {
String line = "";
for (int k = 2; k >= 0; k--) {
line += ((i >> k) & 1) == 1 ? "1" : "0";
}
System.out.println(line);
}
That uses two for loops.
I would create your own toBinary() function:
int toBinary(int x){
StringBuilder sb = new StringBuilder("");
while(x >= 1){
sb.append(x%2);
x /= 2;
}
return Integer.parseInt(sb.reverse().toString());
}
Then just use that function to print:
for(int i=1; i<=7; i++)
System.out.println( String.format("%03d", toBinary(i)) );