Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im working on the last portion of my code and I cant seem to write 7 items to a line. Right now my output has 10 items to a line. I am open to all suggestions.
I get 10 row and 10 columns, then a blank line followed by items that
are greater than 500 in my output.
Thanks so much. Code is below

C#
class ReadArray
    {
       
        static void Main()
        {
            int[] numberArray = new int[100];   //A 100 array element
            string number;                      //String input
            int item;                           //Array index
            
            int flag = 0;
            
            FileStream f = null;
            StreamReader s = null;
            try  
            {
                f = new FileStream("d:\\L01data.txt", FileMode.Open);
                s = new StreamReader(f);
            }
            catch 
            {
                Console.WriteLine("Does not exist");
                flag = 1; //set flag to indicate file failed to open
            }
            if (flag == 0) // File opened as expected
            {
                item = 0;
                while ((number = s.ReadLine()) != null)
                {
                    numberArray[item] = Convert.ToInt32(number);
                    item++;
                }
                for (item = 0; item < numberArray.Length; item++)
                {
                   Console.Write(numberArray[item]);
                   Console.Write('\t');
                }
                Console.WriteLine();
                //foreach loop to only write out the elements in numberArray
                //greater than 500
                foreach (int i in numberArray)
                {
                    if (i > 500)
                    {
                        Console.Write(i);
                    }
                    
                    Console.Write('\t');
                }
                
                s.Close();
                f.Close();
            }
}
}
Posted
Comments
Tanacia 29-Sep-10 15:12pm    
Thanks guys I appreciate the help. Alan yours worked perfectly I just needed to add a reset and it worked like a charm.

Have a count variable outside of your last loop.
Inside the loop increment it. If it equals 7 then write Environment.NewLine and reset count, else write the tab
 
Share this answer
 
for (item = 0; item < numberArray.Length; item++)                {                   
  Console.Write(numberArray[item]);                   
  Console.Write('\t');                
}                
Console.WriteLine();


This section of code actually writes numberArray.Length items in one line and it is the automatic text wrapping of the console that just happens to give you 10 rows of 10 items. The tab width is 8 characters and when the console buffer width is the default 80 characters then wrapping will occur at every 10th tab.

What you should do is start a new line after every N items, where N is the desired number of columns.

Alan.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900