|
well now when I place += it tells me the left side must be a variable or indexer oh well
|
|
|
|
|
I stuck it in the wrong place!
Try:
richTextBox1.Text += "The smallest Number is: " + searchKey1.ToString();
It's the Alzheimer's, I tell you...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
that worked but now its doing this:
The smallest Number is: 2The smallest Number is: 3The smallest Number is: 5The smallest Number is: 8The smallest Number is: 9The smallest Number is: 14The smallest Number is: 16The smallest Number is: 17The smallest Number is: 18The smallest Number is: 22The smallest Number is: 24The smallest Number is: 25The smallest Number is: 31The smallest Number is: 32The smallest Number is: 34
I thought the break statement was to stop the loop?
sorry I had the break out of focus
that worked great thanks for the help I just could not see it. that happens to me after i've been looking at something for a while.
take care
|
|
|
|
|
"I thought the break statement was to stop the loop?"
It does.
But it doesn't stop all the other calls to the same method from outside!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
how would I go about finding the lowest number in a text file w/o comparing each string in order without parsing it into an array and doing a bubble,selection or insertion sort on it.....any ideas, I'm at a loss.
|
|
|
|
|
OK. What's in your text file?
Is it just the numbers? Or is there other text in there?
Any line breaks or other characters?
Can you post a small sample?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
the text file looks like this it is all #s and it is randomly generated each time
the app is run
40
12
1
7
24
28
7
33
27
I have to do 2 things with the text file that is generated. I have to put the #s in order and find the lowest one without using an array or assigning variables to be parsed. I have been researching the file class trying to see if i could create a temp file to hold them and then sort the temp file contents in some way. It is a hard problem given the constraints of not being able to use an array and a simple sort.
|
|
|
|
|
You can't put the numbers in order without storing them in some way!
There is a Linq/Regex based solution:
string inp = "40 12 1 7 24 28 7 33 27";
int min = Regex.Matches(inp, @"\d+").Cast<Match>().Min(m => int.Parse(m.Value));
But if this is your homework then that probably isn't any use to you!
What are you allowed to do, and what do you know how to do? (Did any of that one line of code make sense?)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
a little of it made sense I have studied regex a little. the problem is this is only my 3rd C#
class and I try to watch utube video tutorials as often as I can. I started in java.
the .NET library is impressive and I'm just now getting into it. The only clues the professor has given are that after we build the random generated text file with 20 numbers we are to use file manipulation methods to sort the #s. I only know the basics of streamreader and writer. the other hint he gave us was that we could create as many temp files to hold the numbers and merge them into a Sorted.txt file which we have to print those to a windows form richTextBox which does not have the capability of sorting. I was researching the capabilities of using the text files as containers.
|
|
|
|
|
That expression worked well....
However how do you get it to only write it one time..
it wrote it 40 times. I guess I'll research the regular expressions classes
thanks
|
|
|
|
|
Forget that way to do it - you don't need it (if your numbers are all on separate lines) and it's a bit advanced for what you have been asked to do.
There are a large number of ways to do this within the spirit and (mostly) the letter of what you professor asked.
The first is probably cheating, but it very, very simple:
Read each line from the fle, convert the number to an integer (which you need to do in order to compare them, string comparisons are different) and write each number to a separate file in a new empty folder:
File.WriteAllText(@"D:\Temp\AF\" + i.ToString("0000") + ".txt", i.ToString());
That will create each value as a separate file, called the number it contains (padded to 4 digits with zeros).
Then when you read the files list, Windows will sort it for you...
Probably cheating, as I said...
The next is to use Seek to treat a file as a block of memory.
If you read your input file line by line, and write it to a new file using ToString("0000") you will end up with a file containing equally spaced values - each one occupies 4 bytes. So if you use Stream.Seek, you can treat the file as an array, and write a quick Bubble sort method to sort the values within that file. You can then read the file, and generate your sorted.txt file from that.
The options tend to get a bit more complex from there on!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
thanks for the direction, How long have you been doing this?
|
|
|
|
|
C#? 5 or 6 years, probably.
Computing? Started at Uni in 1977...with COBOL
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
If your still around I cant get thee files to assign the values of the temp file. Ill keep working on it as usual. When I assign i.toString it just names the files 1-20 in order and does not assign the actual number read from the other file.
private void button2_Click(object sender, EventArgs e)
{
string path = @"C:Random # File.txt";
StreamReader readtext = new StreamReader(path);
StreamWriter writetext = new StreamWriter("write.txt");
for (int j = 0; j < 20; j++)
{
string readmetext = readtext.ReadLine();<br />
writetext.WriteLine(Convert.ToInt32(readmetext));
<pre>
}
writetext.Close();
readtext.Close();
StreamReader rt = new StreamReader("write.txt");
File.WriteAllText(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\temp\"
+ i.ToString("0000") + ".txt", i.ToString());
MessageBox.Show("temp created");</pre>
|
|
|
|
|
Stop guessing, and look at your code.
This isn't a subject where guesswork is rewarded - you have to think about what you are doing!
Your code writes sequential numbers into the files, because that is exactly what you told it to do.
Instead of using the number you read from the input file, you are creating the files using a random value of "i"...
And what do you expect your code in the actual loop to do? What does writing pretty much exactly what you read do that is useful?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Ok I got some sleep and looked at it again and your right I was guessing. I did what I thought would work. I read the method description and how it works and sent the actual line value to the file names and it only produced files with numbers that were not ones that were being read from the lines in the imported file.
for (int i = 0; i < 20; i++)
{
int files = rt.Read();
File.WriteAllText(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\temp.txt"
+ files.ToString("0000") + ".txt", files.ToString());
}
MessageBox.Show("temp created"); lang="cs">
|
|
|
|
|
That looks a bit better!
Now, remove the "temp.txt" bit, and use an empty folder!
Create a folder in "My Documents", and give it read and write permissions to everybody.
then
File.WriteAllText(@"C:\Users\grantlinhart\Documents\WorkingFolder\" + files.ToString("0000") + ".txt", files.ToString()); And you should get the files (and just the files)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Yeah when I ran it in that version it called every file temp0001, temp0002....etc.
I fixed that bit I put them in a temp folder all by themselves and this is what I get when i run it now. it should be the random #s that are in the random file as individual files right? now when i run it i get 12 files named 0010 0013 0048 0049 0050 0051 0052 0053 0054 0055 0056 0057 all of the files values correspond to the name, however the random numbers generated by my file are from 1-40 and shouldn't there be 20 files since the loop iterated 20 times or should i use a while loop that iterates to the end of the file?
I tried to put it in the workingfolder but it always threw an exception. Is there a way to paste a picture on these posts
}
<pre>writetext.Close();
readtext.Close();
StreamReader rt = new StreamReader(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\write.txt");
<pre>
for (int i = 0; i < 20; i++)
{
int files = rt.Read();
File.WriteAllText(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\temp\"
+ files.ToString("0000") + ".txt", files.ToString());
}
}</pre></pre>
|
|
|
|
|
this is what I tried to do to sort the #s into files: I found a getFiles method in a Process directory method and my intent was to import the whole directory and read each file and write the contence to the sorted file as you suggested, however nothing appears in the Sorted file after I run it. If you see why please let me know I will still keep trying to debug it but if a glaring error pops out to you please let me know, thanks
private void button2_Click(object sender, EventArgs e)
{
string path = @"C:Random # File.txt";
StreamReader readtext = new StreamReader(path);
StreamWriter writetext = new StreamWriter("write.txt");
for (int j = 0; j < 20; j++)
{
string readmetext = readtext.ReadLine();
writetext.WriteLine(Convert.ToInt32(readmetext));
<pre>
}
writetext.Close();
readtext.Close();
StreamReader rt = new StreamReader(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\write.txt");
for (int i = 0; i < 20; i++)
{
int files = rt.Read();
File.WriteAllText(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\temp\"
+ files.ToString("0000") + ".txt", files.ToString());
}
rt.Close();
ProcessDirectory(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\temp\");
}
public static void ProcessDirectory(string targetDirectory)
{
string variable1;
StreamReader readToSort = new StreamReader(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\write.txt");
StreamWriter writeSorted = new StreamWriter(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\Sorted_File.txt");
string[] fileEntries = Directory.GetFiles(@"C:\Users\grantlinhart\Documents\Visual Studio 2013\Projects\InputOut\InputOut\bin\Debug\temp\");
foreach (string fileName in fileEntries)
{
variable1 = readToSort.ReadLine();
writeSorted.WriteLine(variable1.ToString() + "\n");
}
}</pre>
|
|
|
|
|
the text file looks like this it is all #s and it is randomly generated each time
the app is run
I have to do 2 things with the text file that is generated. I have to put the #s in order
and find the lowest one without using an array or assigning variables to be parsed. I have been researching the file class trying to see if i could create a temp file to hold them and then sort the temp file contents in some way. It is a hard problem given the constraints of not being able to use an array and a simple sort. there is one # per line this thread does not show it that way
40
21
2
1
7
18
32
10
|
|
|
|
|
i spoke too soon, now its just pulling the first # in the line of random numbers and not comparing the other #s that come after it in the list oh well back to the drawing board
|
|
|
|
|
Hi, how to split list to two columns in winform gridview.
I have some list
public static string[] addresses = { "192.168.1.45", "10.36.15.1", "10.36.47.1", "10.36.63.1", "10.36.50.1", "10.36.15.16", "10.36.47.11", "10.36.63.11", "10.36.50.16" };
and results are shown in a single column.
List<string> resultsList = new List<string>();
Parallel.ForEach(addresses, address =>
{
string ip = "";
<pre>
Ping pinger = new Ping();
PingReply reply = pinger.Send(address);
if (reply.Status.ToString() != "Success")
ip = address + " Failed";
else
ip = reply.Address.ToString() + " , Time: " + reply.RoundtripTime.ToString() + "ms , " + " OK";
pinger.Dispose();
resultsList.Add(ip);
});
foreach (var something in resultsList)
{
dataGridView1.Rows.Add(something);
}</pre>
I want to separate the results in two columns. Especially ip, especially time
Please help
|
|
|
|
|
arsale wrote: I want to separate the results in two columns.
dataGridView1.Rows.Add(new object[] { "Row 1, Col 1", "Row 1, Col 2" });
dataGridView1.Rows.Add(new object[] { "Row 2, Col 1", "Row 2, Col 2" }); Would only work if the dgv actually has two columns.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes, but i want to split ping result (var something in resultsList) on two columns.
|
|
|
|
|
arsale wrote: Yes, but i want to split ping result (var something in resultsList) on two
columns. Which two ping-results would you want in there?
dataGridView1.Rows.Add(new object[] { reply.Address.ToString(), reply.RoundtripTime.ToString() + "ms" });
dataGridView1.Rows.Add(new object[] { reply.Address.ToString(), reply.RoundtripTime.ToString() + "ms" }); Further, I'd recommend Convert.ToString(T) over (T).ToString, as the former does not throw an exception on null values.
--edit;
along the lines of
dataGridView1.Rows.Add(new object[] { Convert.ToString(reply.Address), string.Format("{0} ms", reply.RoundtripTime) });
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|