473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Loop test itterates to many times.

110 100+
I am writing a program which reads a file into an array creating a member for every line of text. I am trying to create a test that will examine each member for a given list of char's and IF any of the listed char's are found it needs to spit out an error. If no errors are found it needs to go to the next set of instructions. It seems like a simple If Else statement but in order to iterate for every line I'm using a for loop as such:
Expand|Select|Wrap|Line Numbers
  1. if (cleancheckbox.Checked == false)
  2.                 {
  3.  
  4.                     // Return an error for file with illegal characters
  5.                     for (int i = 0; i < lines.Length; i++)
  6.                     {
  7.  
  8.                         if (lines[i].IndexOfAny(illegal) != -1)
  9.                         {
  10.                             newobject.illegalError();
  11.                             break;
  12.                         }
  13.  
  14.  
  15.                     // If file has no illegal characters this creates the files.
  16.  
  17.                         else
  18.                         {
  19.                             // stores the string entered for the new folder name
  20.                             newobject.ParentFolder = newfldrbx.Text;
  21.  
  22.                             // Specify a folder to house everything new inside
  23.                             //This is not the folder that holds the created files/folders
  24.                             // based off of csv/txt values
  25.                             string outputdir = folderBrowserDialog1.SelectedPath;
  26.  
  27.                             //Create a new subfolder under the current active folder
  28.                             // this new folder will hold all files/folders created
  29.                             // per project.
  30.                             string newPath = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
  31.  
  32.                             // Create the subfolder
  33.                             System.IO.Directory.CreateDirectory(newPath);
  34.  
  35.                             for (int j = 0; j < lines.Length; j++)
  36.                             {
  37.  
  38.                                 //takes the input from the new folder text box
  39.                                 // and assigns it to the filename variable
  40.                                 string fileName = lines[j] + ".txt";
  41.  
  42.                                 // creates new file based off of file name input annd
  43.                                 // parent directory
  44.                                 string newFile = System.IO.Path.Combine(newPath, fileName);
  45.                                 System.IO.File.Create(@newFile);
  46.  
  47.                             }
  48.  
The problem, if I am understanding correctly, is that having my if else inside the FOR loop is causing it to read every line for the given char's and if it fails the IF statement, it goes to the ELSE and loops repeatedly until the FOR condition is met.

I think a Break; would work to cause it to only run thru the ELSE statement once but I can't figure it out exactly.

If anyone has a solution to this or a recommendation to an alternative method (I toyed with a while loop but got even more lost) I would be very appreciative.

Thanks in advance.
Jul 4 '10 #1
7 2011
GaryTexmo
1,501 Expert 1GB
I don't understand why you have two loops here. You've got an outer for loop that reads through every item in lines and performs a test to see if the current item contains illegal characters or not. That makes sense to me, but then you go on to do another loop through all the lines and create the files.

With this logic, lets say you had 4 lines with the first one being illegal. On the second pass through the outer loop, you'll enter into the else condition which will then still attempt to create the first file, along with the second, third, and fourth. The 3rd iteration will attempt to create all four again, as well as on the last iteration.

Can you not remove that loop in the else block and simply perform your operation on the current item (lines[i])?

Perhaps I'm missing something or not reading your code correctly. My apologies if that's the case.
Jul 5 '10 #2
Fuzz13
110 100+
Thank you for your reply. The reason for the 2 loops is that (and mind you I am very new to this so it is the only way I could figure it out):
I use the for loop to read each line. From what I can tell I need something like a for loop where "i" increases each time so it will continue to read line by line.
But the "for" loop is not a test, it is only causing it to go thru each line. The if-else test is my test. I understand what you're saying, i read the lines then no matter what it attempts to create the files as well.

Ultimately I want it to read each line to test it for illegal characters, but then IF there are illegal characters it needs to return my error (newobject.IllegalError) and if there are no illegal characters it needs to go to my ELSE and create the files.

I hope that helps clear it up a little. Thanks.
Jul 6 '10 #3
GaryTexmo
1,501 Expert 1GB
Right, so in your else condition I think you are fine without the second loop. Can you just work on the line from the top for loop?

As pseudo code...

Expand|Select|Wrap|Line Numbers
  1. for each line in files
  2. {
  3.   if (line contains illegal chars)
  4.   {
  5.     output error
  6.   }
  7.   else
  8.   {
  9.     create file with name of line
  10.   }
  11. }
Jul 7 '10 #4
Fuzz13
110 100+
I see what you're sugesting which definately is one solution however I idealy want the initial test to read every line and if at any point any line fails the test I don't want it to do my else statement. By following your pseudocode what happens is it goes line by line spitting out errors for each failure and creating files for each line that passes. How do I set it up to test for failures first and if there are zero failures then create based off the lines?
Jul 7 '10 #5
GaryTexmo
1,501 Expert 1GB
You want it to stop when it encounters an error? If that's the case, use "break" to stop a loop.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < 10; i++)
  2. {
  3.   if (i == 5) break;
  4. }
This is a for loop that goes from 0 to 9, but when i is 5, it stops the loop prematurely.

If you want to test every line first, then if they are all ok, do your operations on them, you'll need two separate loops. In your original code you have nested loops, which generates length * length operations. You want length * 2 operations.

Expand|Select|Wrap|Line Numbers
  1. bool linesOK = false;
  2. for each line in files
  3. {
  4.   if (line contains illegal chars)
  5.   {
  6.     linesOK = false;
  7.     break;
  8.   }
  9. }
  10.  
  11. if (linesOK == true)
  12. {
  13.   for each line in files
  14.   {
  15.     do whatever
  16.   }
  17. }
Jul 7 '10 #6
Fuzz13
110 100+
BAM! That was perfect. Thank you so much. I knew there was a moderately simple solution I was missing which was what you gave me (of course by no means am I trying to undercut the genious of the solution you gave :) )

I appreciate you working thru this with me greatly!
Jul 7 '10 #7
GaryTexmo
1,501 Expert 1GB
You're quite welcome :)

In the future, when it comes to loops, it really helps to sit down and trace through them. Figure out what they're doing and try to compare that with what you want them to do.

Sometimes tracing through it with a pen and paper just helps get your mind around things.

Good luck with the rest of your project!
Jul 8 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
1
by: Marcin Floryan | last post by:
Hello! My question regards opening (and re-opening) Form and the Load event. I have a main form (frmMain) and I also have a data form (frmData). In the main form I have created: Private...
4
by: Arpan | last post by:
Consider the following sentence: Peter and I play together and also go to the same school and both of us are in class V. How do I find out how many times the string "and" has appeared in the...
17
by: keith | last post by:
I am trying to get a exact count of different distinct entries in an Access column. At first, I was trying to work with three columns, but I've narrowed it down to one to simplify it. I've searched...
2
by: Steve | last post by:
A main form contains ten textboxes and ten subforms. All ten subforms have the same query (Query2) for their recordsource. Query2 is based on a query (Query1). Each subform is linked to a different...
3
by: Advo | last post by:
I've made a small search function as of here: if (stristr($bleh,$search_for)) { if(preg_match('/<title>(*)<\/title>/i', $bleh, $matches)) { $name = str_replace("./","/",$name); echo "<li><a...
1
by: GS | last post by:
the windows form application works but the eventhandler for binding source postionChanged got executed too many times: 12 times from saveitemclicked for the data navigator many time from additem ...
3
by: sukatoa | last post by:
For example: Cat Dog Cow Rat Cat Cat Rat Cow
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.