Problem with a Java if Statement using an array?
-
I am writing a program and am having difficulty with a particular if statement, that does not seem to be acting the way it should. I have an array of JTextFields, w1TF[], and in this portion of the program I am using a "for loop" to increment through and read the text fields. The if statement is used to determine if a text field is blank, and if it is blank it will skip it, otherwise it will parse the string to a double and write it to an excel file. Below is the code snippet for this portion: for(int i=0, j=1; i<determination-1; i++, j++){ String cnS, cw1S, cw2S, cw3S, cvS; String uniqS = snString+j; Cell uniqC = resWS.findCell(uniqS); int uniqRow = uniqC.getRow(); if(w1TF[i].getText() != ""){ cw1S = w1TF[i].getText(); double c1 = Double.parseDouble(cw1S); Number c1Cell = new Number(7, uniqRow, c1); resWWS.addCell(c1Cell); } } The problem I am having is that the if statement is not working and it is executing the code regardless of whether the Text Field is blank or not, and then causing an error when attempting to parse the double. I have tried different variations like: if(w1TF[i].getText() != null) and even if(w1TF[i].getText() != "g") and then putting g in the text field and it will still execute. I even put a System.out.println(w1TF[i].getText()); and it will show me that the field is reading "" or "g", yet it still executes. I was able to change it to: if(w1TF[i].getText() == "g") and test it by putting the g in the text field or not, and it then executed normally. I am not sure why it is giving me problems with the if statement using the != "". Any help would be greatly appreciated. Thank you.
-
Answer:
Do you want to check whether there exists some thing in the textfield or not. It should not be null. Why dont you think of doing like this if(w1TF[i].getText().length()>0) { }
Walter Zinsmeister III at Yahoo! Answers Visit the source
Other answers
Do you want to check whether there exists some thing in the textfield or not. It should not be null. Why dont you think of doing like this if(w1TF[i].getText().length()>0) { }
James Bond
Related Q & A:
- how to Create a Java Package from MATLAB Code?Best solution by Stack Overflow
- How to load a Java web app in the terminal?Best solution by stackoverflow.com
- How to create a java applet?Best solution by Stack Overflow
- What is a basic policy statement?Best solution by answers.yahoo.com
- What is a great thesis statement about homework?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.