0

I'm reading in a transaction file that looks like this:

1112, D
4444, A, Smith, Jones, 45000, 2, Shipping
6666, U, Jones
8900, A, Hill, Bill, 65000, 0, Accounting

When I attempt to read the file line by line using ", " the token, the program bombs out with a NoSuchElementException error at the first record. I've deduced that the condition in which I'm reading the file is causing the issue, particularly at the while loop below. I've tried using an "if" statement and setting the conditions to "while (st2.hasMoreTokens)" and a combination of the two but the error persists and I'm not sure why? Thank you in advance for any assistance. This is the code below:

Scanner transactionFile = new Scanner (new File(fileName2));

        for (int i = 0; i < T_SIZE; i++) {
            line2[i] = transactionFile.nextLine();

            transaction[i] = new Transaction();

            st2 = new StringTokenizer(line2[i], ", ");

            transaction[i].setEmployeeID(Integer.parseInt(st2.nextToken()));
            transaction[i].setAction(st2.nextToken());

            while ((transaction[i].getAction() != "D")) {
                transaction[i].setLastName(st2.nextToken());
                transaction[i].setFirstName(st2.nextToken());
                transaction[i].setSalary(Integer.parseInt(st2.nextToken()));
                transaction[i].setNumOfDependants(Integer.parseInt(st2.nextToken()));
                transaction[i].setDepartment(st2.nextToken());
            }
        }
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Sotirios Delimanolis Feb 16 '14 at 05:44
  • @SotiriosDelimanolis: Not sure how this is a string comparison issue? As I explained before, I've used several other conditions (i.e. "while (st2.hasMoreTokens)" or "while ((st2.nextToken) != null)" all with the same result. – user3315213 Feb 16 '14 at 05:52
  • 1
    @user3315213 This comparison (transaction[i].getAction() != "D") will probably always return true, I think that's what Sotirios meant by his comment. You should be comparing Strings with equals. – xp500 Feb 16 '14 at 05:56
  • I understand. Though I replaced the while loop condition with "(!(transaction[i].getAction().equals("D")))" and I am still left with the same result. – user3315213 Feb 16 '14 at 06:01
  • What should happen if you have a `U`? – Sotirios Delimanolis Feb 16 '14 at 06:02
  • I'm wondering how the condition is throwing that exception...That exception should be thrown when you call nextToken. The thing here is that you are reaching the end of the file and you still ask for a next token. – xp500 Feb 16 '14 at 06:04
  • @SotiriosDelimanolis: In the event that a record has "U" or an "A", it should continue to read the rest of the values for their respective variables. However, I've tried those conditions as well (i.e. while (transaction[i].getAction().equals("A")) || (transaction[i].getAction().equals("A")) ) but the problem remains. – user3315213 Feb 16 '14 at 06:06
  • The first step is to check where it happens. Look at your stack trace and then look into debuggers. – Sotirios Delimanolis Feb 16 '14 at 06:07
  • can you try changing the while for an if? You are checking for the same line until the action is != D. But the loop doesn't change the value of action, so it loops forever whenever the line doesn't have a D, eventually consuming the file and throwing the exception in the next iteration. – xp500 Feb 16 '14 at 06:08
  • @xp500: I attempted to debug the program and it appears to be reading the file into the array successfully until it reaches the last line so I believe you may be right. I've tried changing the condition from while to if but nothing has changed. – user3315213 Feb 16 '14 at 06:12
  • Ah, I've figured out the problem (which I am assuming @SotiriosDelimanolis was trying to get me to see). The program bombs at the third record (the one with the "U") because there are no more tokens in the line beyond the last name. Thank you – user3315213 Feb 16 '14 at 06:25
  • 1
    Sorry to make you go through all those questions, but one of the first skills you need to develop as a programmer is debugging. Asking the right questions is the first step to mastering that skill. – Sotirios Delimanolis Feb 16 '14 at 06:26
  • @SotiriosDelimanolis: No need for apologies. I'm glad you were able to help me without pointing out the problem right away. Thank you again. – user3315213 Feb 16 '14 at 06:33

2 Answers2

1

Take a look at the your while loop. The == operator in Java checks if two objects are the same reference, which is rarely a good idea to rely on, and probably causes this loop to loop infinately (or at least until the program crashes with an exception). What you'd want to do, logically, is check that both strings are equal, i.e., both contain the string "D":

while (!transaction[i].getAction().equals("D"))
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0
str.nextToken() 

function access an element when it is called and increments index of it so your are calling it more then the elements in array so it cant have the access to the higher indexes and throws an exception of noSuchElementFound

Fazal
  • 3,374
  • 1
  • 15
  • 20