-2

Okay this is kind of hard to explain but I'll try my best.

I'm creating a program that reads from a .data file, saves each line of the file into an array list, then the user specifies the building and room number to look for, the program then searches through the whole array list (couple thousand lines) for matches, then with those matches calculates how often those rooms are used using some of the data obtained from the data file.

Now, the problem is that I can get up to the program searching through the array list, but it seems to be doing something incorrectly.

String buildingNum = jTextField2.getText();
String roomNum = jTextField1.getText();

int strDuration = 0;
int totalDuration = 0;
int meetings = 0;

for (int x = 0; x < Global.globalLength; x++) {
    if ((buildingNum != "XXXX") && (list.get(x).building != "XXXX") && (list.get(x).room != "XXXX") && (roomNum != "XXXX")) {
        int intBuildingNum = Integer.parseInt(buildingNum);
        int intRoomNum = Integer.parseInt(roomNum);
        int listBuildingNum = Integer.parseInt(list.get(x).building);
        int listRoomNum = Integer.parseInt(list.get(x).room);
        if (listBuildingNum == intBuildingNum) {
            System.out.println("Second STEP WOO");
            if (listRoomNum == intRoomNum) {
                strDuration = Integer.parseInt(list.get(x).duration);
                meetings = list.get(x).days.length();
                totalDuration = totalDuration + (meetings * strDuration);
                System.out.println(strDuration + " and " + meetings);
                System.out.println(" and " + totalDuration);
            }
        }
    }
}

int utilization = totalDuration / 50;
jTextArea1.setText("The room utilization is: " + utilization);

In the data file, there are a few places in which data is entered as XXXXXXXXXXXX for whatever reason. Because of this, when trying to convert said data into an integer there's a problem (for obvious reasons). So to fix this, I made that odd if block right after the for loop so that the program would try not to do so.
However, When running it, I am able to get the program to display the total duration a couple of times before it gives an error that says

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "XXXX"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

Which I THOUGHT I had fixed. Can anyone see why it would still be trying to convert the XXXX into integers?

fishinear
  • 6,101
  • 3
  • 36
  • 84
  • 3
    when you compare `String` use `equals()` not `=` – Haifeng Zhang Dec 11 '14 at 22:00
  • 1
    `if ((buildingNum != "XXXX") && (list.get(x).building != "XXXX") && (list.get(x).room != "XXXX") && (roomNum != "XXXX"))` is wrong. Use `equals(String)`or `equalsIgnoreCase(String)` – jhamon Dec 11 '14 at 22:02
  • @jhamon thanks! that actually fixed it (though i implemented that differently than where you see). Please submit that as an answer so I can mark it correct? I had never heard of that function (still new to java) – RELENTLESS00G Dec 11 '14 at 22:09

3 Answers3

1

You use ==operator on a String here:

if ((buildingNum != "XXXX") && (list.get(x).building != "XXXX") && (list.get(x).room != "XXXX") && (roomNum != "XXXX")) 

== operator used on Strings compare the reference of each string as they are objects. When writing myString == "aString", you create a new String object containing "aString". As it's a new object, there is no chance it will reference the same String object referenced by myString. The condition will always be false.

To compare 2 strings, you have to use String#equals() or String#equalsIgnoreCase()

jhamon
  • 3,603
  • 4
  • 26
  • 37
  • Thanks! For those interested, I deleted that if statement and the several int conversions after it, and instead just compared the original strings using that functions in the final 2 if statements – RELENTLESS00G Dec 11 '14 at 22:17
0

You convert duration:

strDuration = Integer.parseInt(list.get(x).duration);

without sanity checking.

Aaryn Tonita
  • 490
  • 3
  • 7
  • duration is saved as a String through reading the data file, it made it much easier for me than looking at several hundreds of characters across for each line – RELENTLESS00G Dec 11 '14 at 22:08
0

Another option, you can put a try/catch around the statement looking for NumberFormatExeption rather than having all the condition checks.

cmparish
  • 66
  • 3