I'm currently trying to write a 2d int array to file. I've gotten it to write the data to file but it all appears on the same line.
Currently its appearing like this:
15101219816911171076171411881213567131441415101069101413878101181210156976139188711571210891267910
But I want it to appear like this:
15,10,12,19,8
16,9,11,17,10
7,6,17,14,11
8,8,12,13,5
6,7,13,14,4
1,4,15,10,10
6,9,10,14,13
8,7,9,10,11
8,12,10,15,6
9,7,6,13,9
18,8,7,11,5
7,12,10,8,9
12,6,7,9,10
This is my code.
{
StreamWriter outputfile;
File.WriteAllText("ClosingStock.Txt", string.Empty);
outputfile = File.AppendText("ClosingStock.Txt");
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(Global_Stock[i , j]);
}
}
outputfile.Close();
}
}
Any help with this problem would be much appreciated. Thanks.
{
StreamWriter outputfile;
string comma = "";
File.WriteAllText("ClosingStock.Txt", string.Empty);
outputfile = File.AppendText("ClosingStock.Txt");
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(comma);
outputfile.Write(Global_Stock[i , j]);
comma = ",";
}
comma = System.Environment.NewLine);
}
outputfile.Close();
}
You should first create a string to write into a file and then write it at once
StringBuilder sb = new StringBuilder();
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
sb.Append(Global_Stock[i , j]);
sb.Append(",")
}
}
File.WriteAllText("ClosingStock.Txt",sb.ToString());
Just add a new line after each inner loop:
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(Global_Stock[i , j]);
}
outputfile.Write(Environment.NewLine);
}
If I had to guess you should switch StockColumns with StockRows in your loops to have the output you want.
Also, see #Mihir Dave for better implementation of writing a string to a file.
Related
I want to remove a specific object from an array, put it in a smaller array without getting out of range. This is what I've tried but it won't work.
Skateboard[] newSkateboard = new Skateboard[_skateboards.Length - 1];
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[i] = _skateboards[i];
}
}
Sure.
var j = 0;
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[j] = _skateboards[i];
j = j + 1;
}
}
This question already has an answer here:
What is an "index out of range" exception, and how do I fix it? [duplicate]
(1 answer)
Closed 4 years ago.
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[6];
doors[i] = "#";
for (int j = 1; j <=i; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
Hi guys. I need to print # one and then # twice, until i get to six times. It says System.index.out.of.range. How come?
You should try to extend your array, it's limited to 6 elements but you try to access 7 elements as you go through 0 to 6.
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[7];
doors[i] = "#";
for (int j = 1; j <=i; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
If
I need to print # one and then # twice, until i get to six times.
You don't want any array - string[] doors = new string[6];, just loops:
for (int line = 1; line <= 6; ++line) {
for (int column = 1; column <= line; ++column) {
Console.Write('#');
}
Console.WriteLine();
}
If you have to work with array (i.e. array will be used somewhere else), get rid of magic numbers:
// Create and fill the array
string[] doors = new string[6];
for (int i = 0; i < doors.Length; i++)
doors[i] = "#";
// Printing out the array in the desired view
for (int i = 0; i < doors.Length; i++) {
for (int j = 0; j < i; j++) {
Console.Write(doors[j]);
}
Console.Writeline();
}
Please, notice that arrays are zero-based (array with 6 items has 0..5 indexes for them)
because it is out of range.
change it to this:
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[6];
doors[i] = "#";
for (int j = 0; j <=i.length; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
No need to use 2 loops. Just repeat that character
for (int i = 0; i <= 6; i++)
{
Console.Write(new String("#",i));
Console.WriteLine():
}
Ok, so lets say i have two arrays like these;
int[] wow = new int[50];
for (int j = 0; j < wow.Length; j++)
{
wow[j] = j + 1;
}
int[] wew = new int[50];
for (int i = 0; i < wew.Length; i++)
{
wew[i] = i + 10;
}
and i want to print them like;
1 , 11
2 , 12
3 , 13
for (int j = 0; j < wow.Length; j++)
{
wow1[j] = j + 1;
wow2[j] = j + 10;
//print wow1 & wow2 here.
Console.WriteLine("{0},{1}", wow1[j], wow2[j]);
}
Note that in your two loops, i is no different with j, they are essentially the same!
How about using two for-loops?
for(int i = 0; i < wow.Length;i++)
{
for(int j = 0; j < wew.Length;i++)
{
//Print
Console.WriteLine("{0} , {1}", wow[i].ToString(), wew[j].ToString());
}
}
Try this code in case both arrays are the same length
for (int i=0; i<wew.Length; i++)
{
Console.WriteLine(wow[i] + ", " + wew[i]);
}
If the length is different more logic is needed
I am creating a program that holds customers details and sends them an email alerting them of special offers. I am having trouble when I export the email data as I want to store it in the sendto.text field separating each address with a comma, How can I get the data from each row of the datagrid and can it be stored and separated with a comma in a textfield?.
Hope this makes sense. Thanks
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sendto.text = dataGridView1.Rows[i].Cells[3].Value.ToString());
}
Like this?:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sendto.text += dataGridView1.Rows[i].Cells[3].Value.ToString() + (i < (dataGridView1.Rows.Count-1) ? "," : "");
}
you can add comma from second emailid onwards.
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if(i>0)
sendto.text +=","+ dataGridView1.Rows[i].Cells[3].Value.ToString());
else
sendto.text = dataGridView1.Rows[i].Cells[3].Value.ToString());
}
StringBuilder stremail = new StringBuilder();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
stremail.Append(dataGridView1.Rows[i].Cells[3].Value.ToString() + ",");
}
int stringWithoutLastComma = --stremail.Length;
sendto.text = stremail.ToString(0,stringWithoutLastComma);
You can:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sendto.text += string.Format("{0},", dataGridView1.Rows[i].Cells[3].Value.ToString());
}
I want to sort an array[10] with bubblesort and place the unsorted/sorted items in an listbox.
But the array isn't sorted and gives me always the same array of numbers back.
private void button1_Click(object sender, EventArgs e)
{
lblB.Show();
lblQ.Hide();
lbS.Items.Clear();
lbU.Items.Clear();
Random r = new Random();
int n = 10;
int[] arr = new int[n];
//listbox fill random
for (int i = 0; i < arr.Length; i++)
{
arr[i] = r.Next(0, 20);
lbU.Items.Add(arr[i]);
}
int temp = 0;
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[i] < arr.Length)
{
temp = arr[i];
arr[i] = j;
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)
{
lbS.Items.Add(arr[i]);
}
}
Am i missing something here,
Any help would be greatly appreciated.
You have a mistake in your code that's sorting the array:
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[i] < arr.Length)
{
temp = arr[i];
arr[i] = j; // <<-- Mistake here - should be arr[i] = arr[j];
arr[j] = temp;
}
}
}