If statement doesn't execute - java

I am creating a program that allows you to create a pie chart easily. In the method for removing a slice the if statement inside of a for loop doesnt execute and I cant figure out why this happens. Here is the removeSlice method:
public void removeSlice(Color color, float size, String displayText){
int num = 0;
System.out.println("Thing: " + color + " " + size + " " + displayText);
for(int i = 0; i < slice.size(); i++){
System.out.println("I: " + slice.get(i).color + " " + slice.get(i).size + " " + slice.get(i).text + " Current: " + i);
if(slice.get(i).color == color && slice.get(i).size == size && slice.get(i).text.equals(displayText)){
num = i;
System.out.println("It Works");
}
}
System.out.println(num);
slice.remove(slice.get(num));
totalSize -= size;
--current;
}
When trying to remove a slice the console output shows this
Thing: java.awt.Color[r=255,g=255,b=255] 100.0 Hey
I: java.awt.Color[r=0,g=0,b=0] 500.0 Hi Current: 0
I: java.awt.Color[r=255,g=153,b=153] 70.0 Hello Current: 1
I: java.awt.Color[r=255,g=255,b=255] 100.0 Hey Current: 2
I: java.awt.Color[r=153,g=153,b=0] 120.0 Hola Current: 3
0
as you see all of the values equal position 2's values in the ArrayList but still the if statement doesn't execute.

You are comparing colors with ==. Use equals instead. == checks if the objects refer to the same place in memory. You create two colors, but with the same content - then, you must use equals to check if the content match.

You need to modify slice.get(i).color == color to slice.get(i).color.equals(color).
You should use .equals() method to compare color object.
if(slice.get(i).color.equals(color) && slice.get(i).size == size && slice.get(i).text.equals(displayText)){
num = i;
System.out.println("It Works");
}

In addition to the == issue for Color, exact equality comparison for float can give problems. It will work if the values being compared were produced by exactly the same calculation, or if all calculations involved are exact. If not, there may be different rounding error leading to a very small difference in values that would be equal in real number arithmetic.
Small integer-valued floats such as 100.0 do represent the integer exactly, so that is probably not your current problem, but it could give you problems with different numbers.

As a Java Programmer, you should know about equals. Quite often this is what you really want.

You need to use equals() instead of == for the Color objects.

It's hard to say because we can't see whole source code but I think that your problem is here "slice.get(i).color == color". With == you tests if both variables reference the same object.
You should consider to use slice.get(i).color.equals(color) and you need to also implement equals, hashCode methods on that object

try to compare the different values with "equals", no with "=="

Related

How should I format my return statement so I don't double the answer?

private String twoDigits(int value) {
String result = "";
{
if ((mMinute >= 0) && (mMinute <= 9) && (mSecond >= 0) && (mSecond <= 9)) {
tempmin = ("0" + mMinute );
tempsec = ("0" + mSecond );
} else
tempmin = (mMinute + "");
tempsec = (mSecond + " ");
return tempin+tempsec;
This just doubles the output that I'm looking for and I was wondering, whether or not the issue was with the return statement or the actual method.
I need to call back to this method, twoDigits(mMinute)+":"+twoDigits(mSecond) to get the code to display the time, but instead of being able to display 10:09:08 I keep displaying 10:0908:0908
I was wondering how I should fix my code.
Since there are a lot of tiny mistakes in your code, I'll suggest a slightly different approach. Not sure if this method works, in what I assume is Java, but give it a shot:
private String twoDigits(int value)
{
return value <= 9 ? "0" + value : value;
}
This is actually an if/else abbreviation. Return the following: If value <= 9 then add a zero before the value, else the value.
If there's a risk of negative values being received, you could add this:
return (value >= 0 && value <= 9) ? "0" + value : value;
First, there's Paul's comment about the {} after else to encompass both rows. Then, you are not actually using the value received by the function but rather some global variables (mMinute and mSecond). You create but never use result. Furthermore, your if statement says that if both mMinute AND mSecond are between 0 and 9 then both should be fixed. Since you should use value you only have to check that variable's range and edit it accordingly. On the row tempsec = (mSecond + " "); you add a space.. mistake? Finally, you misspelled tempmin on the return row.
Good luck.
Note that your method has a value parameter. You should use this rather than directly access the fields in your class. Perhaps it might help for you to think about the purpose of the twoDigits() method. It seems to me that it is supposed to take an int value and pad it with a leading zero if the input is only a single digit. Note that my description in the previous sentence does not refer to the member variables that represent minutes and seconds; it only refers to the input value.

Object values and local values in arithmetic

So I was trying to perform a simple arithmetic on values within and object 'currentUser' in my one 'pricingAction' class.
The code should add the two volume values(doubles) and set the value of the variable to the sum of the two. In this example the volume_2, and volume_4 variable should be set to the sum of the two.
method 1:
if(filled4 == true){
if(currentUser.getUtility_2().equalsIgnoreCase(currentUser.getUtility_4())){
currentUser.setVolume_2(currentUser.getVolume_2() + currentUser.getVolume_4());
currentUser.setVolume_4(currentUser.getVolume_2() + currentUser.getVolume_4());
}
}
method 2:
if(filled3 == true){
if(currentUser.getUtility_2().equalsIgnoreCase(currentUser.getUtility_3())){
holder = 0;
holder = currentUser.getVolume_2() + currentUser.getVolume_3();
currentUser.setVolume_2(holder);
currentUser.setVolume_3(holder);
}
}
Method 2 returns the value expected and Method 1 appears to be tossing in a duplicate of the value it is setting to.
My question is why does Method 1 do this? I can only assume it is just tacking on the extra sum to the current value but the setter method is a generic this.x = x;
Let's simplify the code a little so it's easier to read:
foo.setX(foo.getX() + foo.getY());
foo.setY(foo.getX() + foo.getY());
Now suppose we start with foo.X = 10, foo.Y = 20.
The first statement will initially compute foo.X + foo.Y - which is 10+20, or 30.
It then sets that (30) as a new value for foo.X.
The second statement will initially compute foo.X + foo.Y, which is now 30+20, or 50. Note that this is using the new value of foo.X. It then sets 50 as a new value for foo.Y.
If you want to set the same value for both properties, you should compute that value once, to avoid the change to the value of the first property from affecting the computation. However, it's clearer to declare the local variable for that value as locally as you can:
double result = foo.getX() + foo.getY();
foo.setX(result);
foo.setY(result);
That's not only correct, but it's also easier to understand and more efficient. Bonus!
Because you have set the value of volume2 before using its new value to set volume4.
currentUser.setVolume_2(currentUser.getVolume_2() + currentUser.getVolume_4());
// volume2 now set with new value
// which you are about to use below
currentUser.setVolume_4(currentUser.getVolume_2() + currentUser.getVolume_4());
Your code is performing two additions (and I suspect you wanted one) -
if(currentUser.getUtility_2().equalsIgnoreCase(currentUser.getUtility_4())){
// Changes volume 2
currentUser.setVolume_2(currentUser.getVolume_2() + currentUser.getVolume_4());
currentUser.setVolume_4(currentUser.getVolume_2() + currentUser.getVolume_4());
}
Should probably be
if(currentUser.getUtility_2().equalsIgnoreCase(currentUser.getUtility_4())){
int newVolume = currentUser.getVolume_2() + currentUser.getVolume_4();
currentUser.setVolume_2(newVolume);
currentUser.setVolume_4(newVolume);
}

Questions over BigInteger?

import java.math.BigInteger;
public class ProjectEuler {
public static void main(String[] args) {
BigInteger bi = new BigInteger("600851475143");
int div = 7;
while (bi.compareTo(new BigInteger("1")) != 0) {
while (bi.mod(new BigInteger(div + "")).compareTo(new BigInteger("0")) == 0) {
bi = bi.divide(new BigInteger(div + ""));
}
div += 2;
}
System.out.println("" + div);
}
}
I was just looking over one of the basic but famous problems of "What is the largest prime factor of the number 600851475143". I found this solution different, i have a couple of questions on how this works.
The first condition checks whether the number equals 1 or not. From there i am not able to understand the rest of the code.
new BigInteger(div +""). why do we concatenate + "" here?
How is the div = 7 decided?
The author decided to "hard-code" his knowledge of the number itself to decide that the first three primes are not among the divisors
The first condition checks whether the number equals 1 or not. From there i am not able to understand the rest of the code.
The rest of the code looks like this in "regular" integers:
while (bi % div == 0) {
bi /= div;
}
div += 2;
new BigInteger(div +"") why do we concatenate + "" here
That is a short way of making an object a String. BigInteger has a parameter that takes String, so the alternative to this approach would be calling Integer.toString(div).
Note that this is not the most efficient solution: one could speed this up by observing that you could stop trying to divide when you reach the square root of the original number, because you can be sure that the next divisor will be the number itself.
How is the div = 7 decided?
Probably the author noticed that the number isn't divisible by 2 nor 3 nor 5. To know how the author did this, he/she should have known this rules: Divisibility Rules and Tests
The first condition checks whether the number equals 1 or not. From there i am not able to understand the rest of the code.
The author is making sure that the number is not BigInteger("1") since it's dividing the number and storing the results in bi in the loop iterations. Note this:
bi = bi.divide(new BigInteger(div + ""));
new BigInteger(div +""). why do we concatenate + "" here?
It uses the BigInteger(String) constructor. The author n̶a̶i̶v̶e̶l̶y̶ makes a new String by adding the int with an empty String.

java if statement not breaking the "for loop" [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I am newbie in java but I think I have done well teaching myself in these few weeks. But now I am stuck at this loop.
Here is a method from one of my class. To help me debug, I have added "myString" string and "syntax" list inside this method to demonstrate what is happening and to keep it simple, at least for now.
public void getIndex(){
String myString = "2 2 + 3 5";
String[] syntax = myString.split(" ");
for (int index = 0; index < syntax.length; index++){
System.out.println("current index is: " + index);
System.out.println("It has: " + syntax[index]);
// these print statements are made to help me debug
if (syntax[index] == "+"){
indexNeeded = index;
break;
}
}
System.out.println("Index Needed: " + indexNeeded);
As you can see inside the loop, I want to break the "for loop" when the element of the list, "syntax" is "+".
(I am showing "+" here but it can be anything in the actual program.)
Here is the output, when run this method:
current index is: 0
It has: 2
current index is: 1
It has: 2
current index is: 2
It has: +
current index is: 3
It has: 3
current index is: 4
It has: 5
Index Needed: 0
The loop should have stopped when it found "+" but it seems that "if statement" is not working at all, and hence "indexNeeded" hasn't changed.
It's a simple method but what am I doing wrong here?
You're trying to compare strings with ==. That doesn't work, you need to use .equals():
change:
syntax[index] == "+"
to
syntax[index].equals("+")
== only returns true when both objects refer to the same instance. equals() will return true when the contents of the string are the same. This is what you want.
Replace
if (syntax[index] == "+"){
with
if (syntax[index].equals("+")){
When you are trying == it comparing the references and syntex[index] is not referring to same location where literal "+" is. So they are not equal.
// If syntax[index] get '+' value from somewhere but not literal
if(syntax[index] == "+" ) // is false
// right way is
if(syntax[index].equals("+")) // is true
// If syntax[index] get '+' value from literal
syntax[index] = "+";
if(syntax[index] == "+" ) // is true
// This approach is faster but has mentioned above has limitations.
When you do equals it actually compares the content.
You should write:
syntax[index].equals("+")
"+" is a reference to a String, and syntax[index] is another. But here you want to compare the objects themselves, not their references.
If you take two objects a and b of whatever class, a == b will test that the references are the same. Testing that they are "the same" is written a.equals(b).
You should read Java's .equals() documentation carefully, it is a fundamental part to understand.
for String, you need to do
syntax[index].equals("+")
If you want to compare the value of a String you need to use .equals() but if you want to compare references you use the operator ==. That a common mistake with newbies.
Take a minute and see the difference between:
syntax[index] == "+"
and
"+".equals(syntax[index])
it that order you don't allow possible null pointer in syntax[index]
Here's a fun, educational way to fix your problem. Add a call to String.intern() to your method and it will work fine. Amaze your friends! :)
public int getIndex()
{
String myString = "2 2 + 3 5";
String[] syntax = myString.split(" ");
int indexNeeded = -1;
for (int index = 0; index < syntax.length; index++)
{
System.out.println("current index is: " + index);
System.out.println("It has: " + syntax[index]);
// these print statements are made to help me debug
if (syntax[index].intern() == "+")
{
indexNeeded = index;
break;
}
}
return indexNeeded;
}
Note that it is better to return a value from a method than it is to use variables with class scope. Class-scoped variables should be reserved for data that can be considered a property of the object. indexNeeded doesn't meet that description, and it's a poor name for an int - it sounds like it should be a boolean.
Equality checks in Java come in two forms.
The equality operator "==" checks to see if two variables refer to the same object. In your case, this test fails because, though their content is the same, you're referring to two different string objects.
The .equals() method is available on every Java object and provides extensible equality checking. In the case of Strings, consider the following:
"+".equals("+") // evaluates to true
going back to the equality operator:
"+" == "+" // evaluates to false
See this page for more detail.
Use return; instead of break;
it works for me

cant sort the following tree map

for (a = 0; a < filename; a++) {
Map<Double,String> m = new HashMap<Double,String>();
String pre = "abc";
String post = ".txt";
for (int ii = 0; ii < 11; ii++) {
m.put(similarityScore[a],pre + a + post + '\n');
}
SortedSet<Double> set = new TreeSet<Double>(m.keySet());
for (Double d : set) {
System.out.println(d + " " + m.get(d));
}
}
Output :
0.5773502691896258 abc0.txt
0.5773502691896258 abc1.txt
0.5773502691896258 abc2.txt
NaN abc3.txt
0.5773502691896258 abc4.txt
NaN abc5.txt
NaN abc6.txt
NaN abc7.txt
NaN abc8.txt
0.5773502691896258 abc9.txt
NaN abc10.txt
This code should be able to sort the double values. But it displays the output on top. What happen ?
The problem is almost certainly NaN.
This is, as the name suggests, not a realy number, and behaves very strangely in terms of comparisons. Is NaN greater than, equal to, or less than 0.5773502691896258? It could be any of those results, and isn't even required to be consistent within a single execution of the program. NaN is not even equal to itself, which says something about how preconceptions of the laws of equality, and strong ordering, go out of the window when NaN is involved.
So the fix is not to use a non-numeric and expect Double.compareTo() to do what you want with it. Depending on what NaN means when returned from similarityScore(), there are several approaches you could take. If it means that it's not a match at all, you could have that method return a Double (rather than a double), return null in these cases, and then only add non-null results to the map. If these results should be displayed anyway, then perhaps you could use a result of 0.0 or -1.0, assuming that's less than any "real" similarity score. If you want something more finessed, then returning something as pure and straightforward as a primitive double is likely going to be the problem, and you may need to return your own (simple) domain class instead.
As an aside - why on earth do you create and populate a HashMap, then use a TreeSet to get the iteration order over the keys? If you simply create m as a TreeMap<Double, String> you get exactly the iteration order you want, so can just iterate overm.entrySet()`. It's clearer, more idiomatic (thus more understandable), and more efficient, so there's no reason not to do this.
for (int ii = 0; ii < 11; ii++) {
m.put(similarityScore[a],pre + a + post + '\n');
}
This puts the same value into the map 11 times - you're not referencing ii inside the loop.
for (Double d : set) {
System.out.println(d + " " + m.get(d));
}
This prints the single entry in the map.
You do the above for values 0..filename - Adding a value to the map several times, then printing it and restarting with a new map.
Map<Double,String> m = new HashMap<Double,String>();
for (a = 0; a < filename; a++) {
String pre = "abc";
String post = ".txt";
m.put(similarityScore[a],pre + a + post + '\n');
}
SortedSet<Double> set = new TreeSet<Double>(m.keySet());
for (Double d : set) {
System.out.println(d + " " + m.get(d));
}
This creates a map, populates it with values for 0..filename, then prints it sorted. You'll still have issues with NaN which isn't really sortable.
Map<Double,String> m = new TreeMap<Double,String>();
for (a = 0; a < filename; a++) {
String pre = "abc";
String post = ".txt";
m.put(similarityScore[a],pre + a + post + '\n');
}
for (Double d : m.keySet()) {
System.out.println(d + " " + m.get(d));
}
And this uses a TreeMap - No need for the intermediate Set
For any Collection to sort, the type of the value on which you are sorting should be same. And should implement comparable interface.
In your case you have NaN and Double values to sort.
Your loop means you're sorting for each filename separately. You'll need to pull the sorting out of the loop to get those values sorted. (Ooops, #Eric beat me to it.)

Categories

Resources