• Post Reply Bookmark Topic Watch Topic
  • New Topic

For loop stops after calling first object  RSS feed

 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a for loop that should be calling a number of different file paths. However, it is stopping after the first iteration of the loop (stops after the first file path is called). Any suggestions?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing I'd do is print out what your fileArray.length returns. Maybe you only have an array with one element.
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just did that and it seems the array length is the length it is supposed to be.

Could there maybe be an issue when I am using the getAbsolutePath method? I don't think that's the cause, but I'm not 100% sure.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is fileArray filled with values?

Hint: it's not
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand your point, but I think that fileArry is loaded with the proper file paths. The arrayLength value outputs a value of "17", and the System.out.println(listOfFiles); returns this:

17
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\AL6000803.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\AL6000903.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\AL6024103.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\AL6029803.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\MS6002003.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\MS6002103.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\MS6002403.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\MS6004403.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\MS6004603.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\TX6036503.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\TX6040403.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\TX6041403.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\TX6042903.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\TX6043003.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\TX6043803.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\TX6044303.PAT
M:\3030 014 DOE EIA NEMS\NEMS_API\EXEC_TEST\TX6046103.PAT




 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I see what you are doing. Rather than putting the output in another loop, put it inside your original loop so you can trace its execution.
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing so shows that the for loop returns the first file path, then 16 nulls; then the for loop returns the first two file paths, then 15 nulls, etc.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am missing something here. The second for loops prints out the 17 absolute paths. Doesn't it imply that it was set in the first for loop? Why do you think that the first for loop didn't run 17 times, when the output from the second for-loop shows the correct result?

Henry
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ultimate purpose of the for loop is to list a number of file paths that are subsequently processed through a FileInputStream/BufferedReader. The results of this processing are formatted to an output text file.

The code as it stands works (file is processed and results are printed to an output text file) but only for the first (of 17) filePaths supposedly contained in fileArray.

I need to loop through fileArray to input the filePaths for all the ".PAT" files in my directory.

The next part of my processing code looks like this:

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a method that process one file path. Make sure it works correctly.
Then and only then worry about looping through an array on paths.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Wallace wrote:
I need to loop through fileArray to input the filePaths for all the ".PAT" files in my directory.

The next part of my processing code looks like this:



Perhaps it would be a good idea to show us a bit more code -- or the very least, the part where you loop through processing all the pat files.

Henry
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the whole set of code as it stands. Really appreciate the helpful comments. I'm learning a great deal!

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the cde above you have given the Sting a misleading name.
It should probably be named file.

Like this:

Here file stands for one element in the array. The loop iterates over every element in fileArray. file is not any list...

Does your code even compile? I guess i is undefined.
Do you realize that your try block is outside of any loop?

I don't like "magic numbers" in line 30.

If you take my advice from my previous post, it will be much easier to write this program.

Also, you are cramming too much code in one method. Try to split it a little.
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand my code is a bit "rough", but I am very new to coding of any sort. I've taken this task on for my job with a tight deadline.

The process worked exactly as I intended for a single file path, which is why I am trying to get the for loop straightened out to cycle through all of the file paths correctly.

Is there another method of cycling through the list of file paths that I can try?
 
Alex Miel
Greenhorn
Posts: 9
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Wallace, I rewrite your code from first post a bit and test and it seems to be working.

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Wallace wrote:Is there another method of cycling through the list of file paths that I can try?

Why would you want another way of doing this instead of using the most straightforward an obviuos methos of looping through an array. The for-each loop (for loop would also do the job).

First, create a method that deals with one path. You said your code works with one path so put it there.Then, suppose you have an array of paths.You can process it by writing:

If you separate your path-processing code completely from your looping code, everything should go easier for you.

BTW, I see you have an array of String in your code. It is not a list. Do not confuse lists with arrays.
It doesn't change the fact that an approach with a list would be exactly the same.

Alex Miel wrote:Matt Wallace, I rewrite your code from first post a bit and test and it seems to be working.

Alex, I don't think it is a good idea to bring lambdas and streams here... I think it would be too much for OP.
Also, it is worth to mention that your code requires Java 8 to work.
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate all the input. Will let you know how it ends up.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!