-
string==null or string.equals(null) problem
Hi everyone,I have a small problem I am trying to figure out. I have coded the below program using Eclipse with Java 1.6. Our unix system at school uses java 1.5. The problem is with the last if statement. If I use:
on Eclipse at home the program does exactly what it is suppose to do. At school it seems like it jumps over the if loop and prints the last result I entered.
If I use:
I get a null pointer exception on both machines. I am hoping someone can help me. I have enclosed the whole code.
Code :
import java.io.*;
import java.util.*;
public class Pe2 {
static String str1;
static String str2;
static String str3;
static String str4;
static boolean continueOn = true;
//Main class
public static void main(String[] args) {
while (continueOn) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a word: ");
str1 = input.nextLine();
//str1 = str1.toLowerCase();
if (str1.equals("END")){
System.out.println("Program will exit.");
System.exit(0);
}
str1 = str1.toLowerCase();
char[] arrayOfChar1 = str1.toCharArray();
int i = str1.length();
str2 = "/abcdefghijklmnopqrstuvwxyz"; //alphabet comparison
char[] arrayOfChar2 = str2.toCharArray();
int[] arrayOfInt1 = new int[27];
int[] arrayOfInt2 = new int[27];
for (int j = 1; j < 27; j++) {
arrayOfInt1[j] = 0;
}
for (int j = 0; j < i; j++) {
for (int k = 1; k < 27; k++) {
if (arrayOfChar1[j] != arrayOfChar2[k])
continue;
arrayOfInt1[k] += 1;
}
}
//Loading dictionary with exception handling
try {
BufferedReader bf = new BufferedReader(new FileReader("/usr/share/lib/dict/words"));
while ((str3 = bf.readLine()) != null) {
int m = str3.length();
char[] arrayOfChar3 = str3.toCharArray();
for (int n = 1; n < 27; n++) {
arrayOfInt2[n] = 0;
}
for (int n = 0; n < m; n++) {
for (int i1 = 1; i1 < 27; i1++) {
if (arrayOfChar3[n] != arrayOfChar2[i1])
continue;
arrayOfInt2[i1] += 1;
}
}
int i1 = 0;
for(int i2 = 1; i2 < 27; i2++) {
if (arrayOfInt2[i2] == arrayOfInt1[i2]) {
i1++;
}
}
if (i1 == 26) {
str4 = str3;
}
}
bf.close();
}
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
if (str4 == null){
//if (str4.equals("null")){ //commented outfor testing purposes
System.out.println("String not found");
continueOn = true;
}
else {
System.out.println("Word found is: " + str4);
}
}
}
}
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
csharp100
the last if loop.
<pet_peeve>
There is no such thing as an if loop.
-
Re: string==null or string.equals(null) problem
if (str4 == null) {
Your assumption that this line does different things in different versions of Java is incorrect. There must something else going on, something else you changed.
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
Junky
<pet_peeve>
There is no such thing as an if loop.
Thanks for the correction.
-
Re: string==null or string.equals(null) problem
The following is testing if the string variable is null in memory.
A string variable can exist without any content
The following is checking if the contence of the string variable is a null string.
To avoid problems like this, use a constructor to initialize you variables, then you can test if str.equals("") This way the string will always exist and always have something in it.
Edit: fixed a fail on typing code tags
-
Re: string==null or string.equals(null) problem
Hi,
Please make a test before that condition,
System.out.println("str ="+str);
I think this value is null in your case so you are getting the null pointer exception.
bean factory
-
Re: string==null or string.equals(null) problem
You've had quite a bit of guessing at your problem, but without an SSCCE, that's all it is. Post an SSCCE that demonstrates your problem, and you'll get a more definitive answer.
-
Re: string==null or string.equals(null) problem
tbh if ur that desperate use both lmao..
Code :
if (str4.equals(null) || str4.equals("")){
// CODE HERE
}
only difference is.. str4 has been initialized on the 2nd statement.. the first is not initialized..
so it will check if the string has been initialized .. and if it hasnt.. or has but is empty.. it will execute the further code..
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
macko
tbh if ur that desperate use both lmao..
Code :
if (str4.equals(null) || str4.equals("")){
// CODE HERE
}
only difference is.. str4 has been initialized on the 2nd statement.. the first is not initialized..
so it will check if the string has been initialized .. and if it hasnt.. or has but is empty.. it will execute the further code..
That won't work. Hint- What happens if str4 is indeed null?
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
KevinWorkman
You've had quite a bit of guessing at your problem, but without an
SSCCE, that's all it is. Post an SSCCE that demonstrates your problem, and you'll get a more definitive answer.
Test output on Eclipse at home:
Code :
Enter a word:
emsl
String not found
Enter a word:
umsl
Word found is: slum
Enter a word:
sailplane
Word found is: slum
Enter a word:
END
Program will exit.
Test output on unix at school:
Code :
Enter a word:
emsl
String not found
Enter a word:
umsl
Word found is: slum
Enter a word:
sailplane
Word found is: slum
Enter a word:
END
Program will exit.
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
KevinWorkman
That won't work. Hint- What happens if str4 is indeed null?
If it is null it should generate the if statement as true am i correct?
Although isn't that what he wanted.. if the string is null then execute an if statement
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
csharp100
If I enter the word or anagram "umsl" it prints out the word slum. Slum is an anagram of umsl and is the last word in the dictionary that umsl is being checked against. If I put in the word sailplane the program sould print out, "String not found" and loop back to enter another word. What happens is instead it prints out the word slum again. Almost as if it is skipping the last if statement. The program does not end until "END" is typed in. Bottom line is if a word is not found in the dictionary it prints the last word entered again instead of "string not found."
Okay. Like I said, throw together an SSCCE (should be just a main method with a single if statement that tests a single String) that demonstrates what's going on that you don't understand, and we'll be happy to help you.
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
macko
If it is null it should generate the if statement as true am i correct?
Although isn't that what he wanted.. if the string is null then execute an if statement
Nope. I'd suggest you throw together a little test program, too! :p
One more hint- It almost never makes sense to do someObject.equals(null).
-
Re: string==null or string.equals(null) problem
true heh, Well lets stick to the empty string then :-)
Also I'm getting confused whilst helping people with programming, I cannot understand half of what they mean lol.. no console error shown etc..
-
Re: string==null or string.equals(null) problem
Interesting, now that I see the output, it is the same. I guess the question is how do I fix this? Any ideas?
-
Re: string==null or string.equals(null) problem
ideas you say eh?
I'd try a java book :P
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
csharp100
Interesting, now that I see the output, it is the same. I guess the question is how do I fix this? Any ideas?
I'm going to repeat this- without an SSCCE, we're having a really hard time understanding exactly what you're talking about. Post an SSCCE, and we'll go from there.
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
macko
true heh, Well lets stick to the empty string then :-)
You can still use == to test against null.
Quote:
Originally Posted by
macko
Also I'm getting confused whilst helping people with programming, I cannot understand half of what they mean lol.. no console error shown etc..
Yep, that's why half of my responses are asking for an SSCCE. People think I'm just being a jerk, but it's actually because oftentimes the OP is talking about something completely different from what I'm answering, and it's just a big waste of everybody's time. You can explain your problem until you're blue in the face (er, fingers, whatever), but a simple piece of easily runnable code goes much further towards explaining the problem. Plus, half the time the OP figures out their own problem during the process of creating the SSCCE! They invariable come back with a grumpy "whatever, nevermind, I figured it out on my own, thanks for nothing". Ah, the joys of education.
-
Re: string==null or string.equals(null) problem
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
KevinWorkman
You can still use == to test against null.
Yep, that's why half of my responses are asking for an SSCCE. People think I'm just being a jerk, but it's actually because oftentimes the OP is talking about something completely different from what I'm answering, and it's just a big waste of everybody's time. You can explain your problem until you're blue in the face (er, fingers, whatever), but a simple piece of easily runnable code goes much further towards explaining the problem. Plus, half the time the OP figures out their own problem during the process of creating the SSCCE! They invariable come back with a grumpy "whatever, nevermind, I figured it out on my own, thanks for nothing". Ah, the joys of education.
I do not think of you being a "jerk." I guess what I am not understanding is if the code could have been written with fewer lines, I would have written the code with fewer lines. I assume you are saying just write a simple if statement and go from there.
It seems to be the last if statement. The string that is in while loop stays in the while loop unless there is a "concrete" string that has been found in the dictionary. If no string has been found in the dictionary, it prints the string that is still in the while loop if there is a string.
-
Re: string==null or string.equals(null) problem
Well, Anything can be simplified... If your doing the same thing in a loop more then 4 times you should be able to narrow it down, as for the null of a string..
-
Re: string==null or string.equals(null) problem
I don't mean that you necessarily think I'm a jerk, but other people do get impatient when I ask to see their code- even though they're asking questions about their code!
The reason I'm asking for an SSCCE is because it sounds like you've got a lot going on that doesn't have to do with the actual problem. We don't really need to see the looping, or the dictionary loading, stuff like that. What String is going into the if statement? What is the result of that if statement? What did you expect it to be? That's what an SSCCE will demonstrate.
-
Re: string==null or string.equals(null) problem
Kevin why have my other 2 posts been deleted :O.. is there a rule on the contents of them?
-
Re: string==null or string.equals(null) problem
Quote:
Originally Posted by
macko
Kevin why have my other 2 posts been deleted :O.. is there a rule on the contents of them?
They weren't deleted, I moved them to the cafe. That's where non-technical questions go, so you'll be more likely to receive a response there.
-
Re: string==null or string.equals(null) problem
ahh thought i was doin something wrong :O... though i guess you would've told me if i had been xD