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 9 years ago.
Improve this question
I am seeing different results in checking Integers against another and i cant figure out why i am reciving these results?
For example:
Integer j = new Integer(2);
Integer i = new Integer(2);
boolean test1 = false;
if(i >= j){
test1 = true;
}else{
test1 = false;
}
boolean test2 = false;
if(i <= j){
test1 = true;
}else{
test2 = false;
}
System.out.println("i >= j: " + test1); //gives True
System.out.println("i <= j: " + test2); //gives False
System.out.println(i >= j); //gives True
System.out.println(i <= j); //gives True
I was curius as to why i am seeing that i is greater then j so i also tested i > j and still seeing a True as result.
I wonder if someone can explain this to me, or tell me where i can read up on this. I have tried to figure it out and have been reading into wrapper classes but not sure that is the culprit. Stack placement maybe?
You had a typo: in the second block, you wrote test1 = true, but it should be test2 = true.
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 5 years ago.
Improve this question
public class Sum_of_Numbers {
public static void main( String [] args) {
int sumOfEven = 0;
int sumOfOdd = 1;
int even_Times = 0;
int odd_Times = 0;
while ((even_Times < 12) || (odd_Times < 13)); {
sumOfEven = sumOfEven+2;
even_Times = even_Times+1;
sumOfOdd = sumOfOdd + 2;
odd_Times = odd_Times + 1;
System.out.println("The sum of even integers is " + sumOfEven);
System.out.println("The sum of odd integers is " + sumOfOdd);
}
System.out.println("The sum of even integers is " + sumOfEven);
System.out.println("The sum of odd integers is " + sumOfOdd);
}
}
When I run this code, the loop fails to start and I don't know why.
You've used the wrong syntax with the while statement and it's in an infinite loop
while ((even_Times < 12) || (odd_Times < 13)); {
The semi-colon is closing the statement, so only the conditions within the while loop are executed. even_Times and odd_Times don't increment, so it loops forever.
When the semi-colon is removed, the following { } block will execute within the while loop.
while ((even_Times < 12) || (odd_Times < 13)) {
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 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.
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.