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:
if (str4 == null) {
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:
if (str4.equals(null)) {
I get a null pointer exception on both machines. I am hoping someone can help me. I have enclosed the whole 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); } } } }