Java Combo Box and MySql database - java

I am currently using netbeans GUI drag and drop form, i have a combobox, and i want the combobox value to change based on data received from a database. The other textboxes are receiving their data correcly, the main problem is with the combobox.
String x = tI.getStatus();
if(x == "Assigned"){
cboStatus.setSelectedIndex(0);
}
else if(x == "In progress"){
cboStatus.setSelectedIndex(1);
}
else if (x == "Pending"){
cboStatus.setSelectedIndex(2);
}
else if(x == "Completed"){
cboStatus.setSelectedIndex(3);
}
can anyone tell me how to change the index of the combo box based on data received from database. thanks.

Use String.equals to compare String content. The == operator compares Object references.
if (x.equals("Assigned")) {

You can't compare Strings with ==, use equals()

Use String.equals(). The == operator compares if two Strings reference the same String object; not if they have equal characters in the String.

Related

JOptionPane trying to retrieve which button is selected

I'm currently trying to get this part of my code to compare which button the user chooses to the button it self. At the moment it automatically displays the else which is Test2.
Object usersChoice;
Object[] options = { "Go on a journey!", "Exit, i'm tired." };
usersChoice = JOptionPane.showOptionDialog(null, "Hello Melissa :)", "Sunshine program",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
if(usersChoice == options )
{
JOptionPane.showMessageDialog(null, "Test1");
} else {
JOptionPane.showMessageDialog(null, "Test2");
}
The problem is with this code (I've moved the declaration of usersChoice for clarity):
Object[] options = [...]
Object usersChoice = JOptionPane.showOptionDialog([...])
The method JOptionPane.showOptionDialog() returns an int. Since that's a primitive, it gets autoboxed to an Integer.
Now you have this code:
if(usersChoice == options )
You are comparing an Object[] to an Object (more specifically, an Integer). That will always be false because they are different types.
Also remember that in Java, using == checks for equality on primitives, but Objects compared this way will be compared by their memory locations. Use .equals() instead to compare Objects.

Edit Text and CheckBox checking

I currently have 2 checkboxes with an edit text field beside them. There are 4 checkboxes total and I have the logic to check each one and uncheck others if they are checked (almost radio button style). These 2 however could both be checked if they have values in the edit text fields.
However, if the EditText field (which is set to numeric) has a value of 0 or is blank I want it to uncheck the check box and set the value to 0.
Here is the code I have to do this
if (etBase.getText().toString() == "0" || etBase.getText().toString() == ""){
etBase.setText("0");
cbBase.setChecked(false);
} else {
cbBase.setChecked(true);
}
if (etField.getText().toString() == "" || etField.getText().toString() == "0"){
etField.setText("0");
cbField.setChecked(false);
} else {
cbField.setChecked(true);
}
As it sits right now I default the two fields to be "0" when it starts. When this logic runs, it is setting both checkboxes to checked.
I must be missing something here.
To compare string, you must use .equals() function. ( Or .compareTo() but that's no the today's deal)
Ex :
if (etBase.getText().equals("0") || etBase.getText().equals("")){
...
}
var1 == var2 is used to get if these both variable have the same object

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

Conditional with number1 == number2 (of type Long) is never true?

this code is supposed to list recent calls with recent same nos skipped but they are being displayed, please help
//code
Long number0=(long) 0;
// loop through cursor
while(mCallCursor.moveToNext()){
Long number1 = mCallCursor.getLong(0);
if(number1==number0)
continue;
else
number0=number1;
if(mCallCursor.getString(2)!=null){
String name = mCallCursor.getString(2);
System.out.println(name);
}
else
System.out.println(number1);
}
Instead of
if(number1==number0)
use
if(number1.equals(number0))
Two Long values can satisfy equals without being ==.
The main reason why it is not working is that Long 's are objects and == operator works as it is testing equalities of two objects not the long values stored in these objects. On the other hand a long is not an object but a primitive.
if((long)number2 == (long)number1)
shall work as well.

Checking for NULL in Java

I am using a Bonded JComboBox. What I want to know is how do I check if that JComboBox has any items in it or not.
Thank you.
To test if the JComboxBox reference is null, you can compare it with null using the == operator. To test if the combo-box contains any items, you can use the instance method getItemCount, which returns the number of items it contains.
JComboxBox box = ...
boolean boxIsNull = (box == null); // answers the title of the question
boolean boxHasItems = (box.getItemCount() > 0);

Categories

Resources