It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
public void Save(string path , string fileName , PictureBox pb)
{
int framesNumberX = 0;
int framesNumberY = 0;
string fn;
string t = Path.GetFileNameWithoutExtension(this.name);
if (File.Exists(path + "\\" + "DATABASE" + "\\" + fileName + "\\" + t + ".txt"))
{
try
{
string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName);
File.Delete(f);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not delete file from disk. Original error: " + ex.Message);
}
fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName;
}
else
{
fn = path + "\\" + "DATABASE" + "\\" + fileName + "\\" + this.name + ".txt";
}
OptionsFile setting_file = new OptionsFile(fn);
setting_file.SetKey("File Name", fn);
setting_file.SetKey("Object Name", fileName);
setting_file.SetKey("Animation Name", this.name);
setting_file.SetKey("picturebox.Width", pb.Width.ToString());
setting_file.SetKey("picturebox.Height", pb.Height.ToString());
string[] xFrames = new string[wocl.Count];
string[] yFrames = new string[wocl.Count];
string X="";
string Y="";
for (int i = 0; i < wocl.Count; i++)
{
X = string.Format("Frame_X_{0} ", i + 1);
Y = string.Format("Frame_Y_{0} ", i + 1);
int c = Convert.ToInt32(X);
int d = Convert.ToInt32(Y);
framesNumberX += c;
framesNumberY += d;
for (int j = 0; j < wocl[i].Point_X.Count; j++)
{
xFrames[i] += string.Format("{0},", wocl[i].Point_X[j]);
yFrames[i] += string.Format("{0},", wocl[i].Point_Y[j]);
}
string tt = xFrames[i].Trim(",".ToCharArray());
string yy = yFrames[i].Trim(",".ToCharArray());
setting_file.SetKey(X, tt);
setting_file.SetKey(Y, yy);
}
setting_file.SetKey("Number Of Frames X", framesNumberX.ToString());
}
If im doing in the loop for example : FrameNumber +=; it will count me the number of frames for example 6. But i want to count how many times the frames of X showed up and how many frames of Y.
I tried ot it like this:
X = string.Format("Frame_X_{0} ", i + 1);
Y = string.Format("Frame_Y_{0} ", i + 1);
int c = Convert.ToInt32(X);
int d = Convert.ToInt32(Y);
framesNumberX += c;
framesNumberY += d;
But thats not working not a good way.
Maybe i need to make the counting of X and Y in the end after the variables tt and yy ?
I want to count how many times the loop did the variable tt and how many times it did the variable yy. Just puting int variable and make ++ will give me the overall frames i need to seperate them to X and Y. So if i have for example 6 frames so X will be 3 and Y will be 3.
Convert.Int32() isn't a Val() method, in other words, it won't take just the numbers from any string. When using Convert.Int32(), the entire string has to be numeric, for example:
// doesn't work
string x = "My string 123";
int y = Convert.Int32(x);
// does work
string x = "123";
int y = Convert.Int32(x);
If I understand correctly, you're trying to convert something to an int and count at the same time (here).
X = string.Format("Frame_X_{0} ", i + 1);
Y = string.Format("Frame_Y_{0} ", i + 1);
// blows up with an exception
int c = Convert.ToInt32(X);
int d = Convert.ToInt32(Y);
framesNumberX += c;
framesNumberY += d;
The above could better be written like:
X = string.Format("Frame_X_{0} ", i + 1);
Y = string.Format("Frame_Y_{0} ", i + 1);
framesNumberX++;
framesNumberY++;
// or (depending on what you need)
framesNumberX = i + 1;
framesNumberY = i + 1;
Or - Which would be even more clear; do your math operation first, then use the result in your string.Format() call;
framesNumberX = i + 1;
framesNumberY = i + 1;
X = string.Format("Frame_X_{0} ", framesNumberX);
Y = string.Format("Frame_Y_{0} ", framesNumberY);
However, framesNumberX and framesNumberY would be equal to eachother AND to the iterator i + 1. This is where I'm starting to lose the purpose of the variables. It would help if you could clarify in pseudo-code what exactly you're trying to accomplish:
public void MyMethod()
{
... code here
// trying to `your explanation`
... code here
// and here, I'm trying to `your explanation`
... code here
}
EDIT
for this code to work
X = string.Format("Frame_X_{0} ", i + 1);
Y = string.Format("Frame_Y_{0} ", i + 1);
int c = Convert.ToInt32(ExtractNumbers(X));//modfied code line
int d = Convert.ToInt32(ExtractNumbers(Y));//modfied code line
framesNumberX += c; framesNumberY += d;
you need to extract number something like this
static string ExtractNumbers( string expr )
{
return string.Join( null,System.Text.RegularExpressions.Regex.Split( expr, "[^\\d]" ) );
}
Prev Ans
Good way to do is
static void Main()
{
// Convert string to number.
string text = "123";
int num = -1;
if( int.TryParse (text,out num))
Console.WriteLine(num);
}
check that string is convertable or not
Related
string path = "xx\\xx.txt";
string[] memo = System.IO.File.ReadAllLines(path);
int totalLines = textBox2.Lines.Length;
string lastLine = textBox2.Lines[totalLines - 1];
for (int i = 0; i < 5; i++)
{
string a = memo[i];
if (a.Contains(lastLine))
{
textBox2.Text = (textBox2.Text + Environment.NewLine + memo[i + 1]);
}
}
Check the notepad from lines 0 to 4
if (a.Contains(lastLine))
{
textBox2.Text = (textBox2.Text + Environment.NewLine + memo[i + 1]);
}
I want to make above code run only once.
However, because there is a memo[i+1] variable i.
Under the influence of the for statement that repeats 5 times, the text 5 times is input.
How can I fix it?
Simply add a break statement
if (a.Contains(lastLine))
{
textBox2.Text = (textBox2.Text + Environment.NewLine + memo[i + 1]);
break;
}
I recently make a program to calculate sum and avg of a given array. The program also want to print the histogram or stars pattern match with value of index.
Like this:
This is the code I wrote so far:
private void process_Click(object sender, EventArgs e)
{
string temp2 = null;
string temp3 = null;
float sum=0;
int countingNumber = 1;
string message;
int count = Convert.ToInt32(inputArray.Text);
int[] varray = new int[count];
for (int i = 0; i < count; i++)
//for (int j = 1; j <= count; j++)
{
varray[i] = Convert.ToInt32(Interaction.InputBox(message = "enter the value of array number " + countingNumber));
sum = sum+ varray[i];
temp += countingNumber + " "+varray[i] + Environment.NewLine;
temp2 += countingNumber + " " + varray[i] + " *" + Environment.NewLine;
box1.Text = Convert.ToString("Index Value" + Environment.NewLine + temp);
boxSum.Text = Convert.ToString(sum);
boxAvg.Text = Convert.ToString(sum/count);
countingNumber++;
}
for (int stars = 0; stars <= i; stars++)
{
temp3 = " ";
box2.Text = Convert.ToString("Element Value Histogram" + Environment.NewLine + temp2+temp3);
}
}
}
My code won't print the stars match with the value. Could anybody help me?
Try replacing this line:
temp2 += countingNumber + " " + varray[i] + " *" + Environment.NewLine;
with this line:
temp2 += countingNumber + " " + varray[i] + " " + new String('*', i) + Environment.NewLine;
I am trying to write to a CSV file, and have researched a bit on how to do this and it seems pretty straightforward but it is not populating my CSV file when I run it. The program will write to a txt and my console with no issue.
StreamWriter vWriteFile = new StreamWriter("Positions2.csv");
var path = #"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReader3\Position_2016_02_25.0415.csv";
if (vShowBoth == false)
{
//This determines whether to view by account or by underlying.
Console.WriteLine("Please enter 0 if you wish to see sums by account, or 12 if you wish to see by underlying");
int vViewControl = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Account" + " Settle Sum" + " Open Sum" + " Buy Sum" + " Sell Sum");
using (var parsedLines = File.ReadLines(path).Select(line => line.Split(',')).GetEnumerator())
{
bool vMore = parsedLines.MoveNext();
while (vMore)
{
// Initialize
bool vWasSuccessful;
var vAccount = parsedLines.Current[vViewControl];
double vSettleMMSum = 0;
double vOpenSum = 0;
int vBuySum = 0;
int vSellSum = 0;
do
{
double vParseSettleMM = 0;
double vParseOpen = 0;
int vParseBuy = 0;
int vParseSell = 0;
//Parsing data read in from strings, into temp variables
vWasSuccessful = double.TryParse(parsedLines.Current[7], out vParseSettleMM);
vWasSuccessful = double.TryParse(parsedLines.Current[8], out vParseOpen);
vWasSuccessful = int.TryParse(parsedLines.Current[6], out vParseBuy);
vWasSuccessful = int.TryParse(parsedLines.Current[10], out vParseSell);
//adding temp variabels to sum
vSettleMMSum += vParseSettleMM;
vOpenSum += vParseOpen;
vBuySum += vParseBuy;
vSellSum += vParseSell;
vMore = parsedLines.MoveNext();
}
//sets up when to break
while (vMore && vAccount == parsedLines.Current[vViewControl]);
//After each Break need to print out Account name and sums from above.
// Do printing here as part of the loop, at the very end of the loop code block.
Console.WriteLine("--------------------------------------------------------");
Console.WriteLine(vAccount + " " + vSettleMMSum + " " + vOpenSum + " " + vBuySum + " " +
vSellSum);
//vWriteFile.Write(vAccount + "," + vSettleMMSum + "," + vOpenSum + "," + vBuySum + "," +
// vSellSum);
vWriteFile.WriteLine("{0},{1},{2},{3},{4}", vAccount, vSettleMMSum, vOpenSum, vBuySum, vSellSum);
//reset sums for next loop
vSettleMMSum = 0;
vOpenSum = 0;
vBuySum = 0;
vSellSum = 0;
}
}
}
You should make it a habit to wrap your streams with the using keyword so you don't run into situations where you forget to close it out. It's a good practice to use the using keyword around anything that inherits IDisposable
So your code would look like this.
using(StreamWriter vWriteFile = new StreamWriter("Positions2.csv"))
{
var path = #"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReader3\Position_2016_02_25.0415.csv";
if (vShowBoth == false)
{
//This determines whether to view by account or by underlying.
Console.WriteLine("Please enter 0 if you wish to see sums by account, or 12 if you wish to see by underlying");
int vViewControl = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Account" + " Settle Sum" + " Open Sum" + " Buy Sum" + " Sell Sum");
using (var parsedLines = File.ReadLines(path).Select(line => line.Split(',')).GetEnumerator())
{
bool vMore = parsedLines.MoveNext();
while (vMore)
{
// Initialize
bool vWasSuccessful;
var vAccount = parsedLines.Current[vViewControl];
double vSettleMMSum = 0;
double vOpenSum = 0;
int vBuySum = 0;
int vSellSum = 0;
do
{
double vParseSettleMM = 0;
double vParseOpen = 0;
int vParseBuy = 0;
int vParseSell = 0;
//Parsing data read in from strings, into temp variables
vWasSuccessful = double.TryParse(parsedLines.Current[7], out vParseSettleMM);
vWasSuccessful = double.TryParse(parsedLines.Current[8], out vParseOpen);
vWasSuccessful = int.TryParse(parsedLines.Current[6], out vParseBuy);
vWasSuccessful = int.TryParse(parsedLines.Current[10], out vParseSell);
//adding temp variabels to sum
vSettleMMSum += vParseSettleMM;
vOpenSum += vParseOpen;
vBuySum += vParseBuy;
vSellSum += vParseSell;
vMore = parsedLines.MoveNext();
}
//sets up when to break
while (vMore && vAccount == parsedLines.Current[vViewControl]);
//After each Break need to print out Account name and sums from above.
// Do printing here as part of the loop, at the very end of the loop code block.
Console.WriteLine("--------------------------------------------------------");
Console.WriteLine(vAccount + " " + vSettleMMSum + " " + vOpenSum + " " + vBuySum + " " +
vSellSum);
//vWriteFile.Write(vAccount + "," + vSettleMMSum + "," + vOpenSum + "," + vBuySum + "," +
// vSellSum);
vWriteFile.WriteLine("{0},{1},{2},{3},{4}", vAccount, vSettleMMSum, vOpenSum, vBuySum, vSellSum);
//reset sums for next loop
vSettleMMSum = 0;
vOpenSum = 0;
vBuySum = 0;
vSellSum = 0;
}
}
}
}
I am trying to implement Best fit memory allocation. I have done the code, which i think should work but somehow it appears its getting stuck in the loop. I can't figure out why. The following is my code. I want to allocate jobs to the memory with the least memory waste.
public void bestFit(int job_size)
{
string str1 = "";
string str2 = "";
string str3 = "";
int memory_block = 99999;
int subscript = 0;
int initial_memory_waste = memory_block - job_array[0];
int job_counter = 0;
int counter = 1;
int memory_waste = 0;
while (counter <= memory_array.Length)
{
if (job_size > memory_array[counter - 1])
{
counter += 1;
}
else
memory_waste = memory_array[counter - 1] - job_size;
{
if (initial_memory_waste > memory_waste)
{
subscript = counter;
initial_memory_waste = memory_waste;
counter += 1;
}
}
}
queued_jobs = counter;
str3 = ("Job number: " + (queued_jobs).ToString() + " of size: " + job_size.ToString());
if (job_counter < job_array.Length)
{
bf_waiting_queue.Add(str3);
}
else
{
str1 = ("Job number: " + (job_counter).ToString() + " of size: " + job_size.ToString() + " is allocated to Memory block: " + (subscript).ToString() + " of size: " + memory_array[subscript - 1]).ToString();
memory_waste = memory_array[subscript - 1] - job_size;
str2 = ("Memory waste is: " + memory_waste.ToString());
bf_total_memory_waste += memory_waste;
memory_array[counter - 1] = (memory_array[counter - 1] - job_size);
bf_jobsInMemory.Add(str1 + "\t" + str2);
job_counter += 1;
counter = 1;
}
}
Aside from the braces issue others pointed out, your logic makes it possible for you to never increment 'counter', which is why you are stuck in the loop. The following reorganization is cleaner and guarantees you always increment 'counter'. Also, a couple of comments couldn't hurt.
while (counter <= memory_array.Length)
{
// If block fits, consider it
if (job_size <= memory_array[counter - 1])
{
memory_waste = memory_array[counter - 1] - job_size;
// If this block is less wasteful, remember it
if (initial_memory_waste > memory_waste)
{
subscript = counter;
initial_memory_waste = memory_waste;
}
}
// Next block
counter += 1;
}
I would like to ask you about this programing part, is it everything ok?
the task was:
Write the pseudocode or flow diagram and code for the theorem of Pythagoras - for right-angle triangle with three ribs (a, b, and c) of type integer
int KendiA = 0;
int KendiB = 0;
int H = 0;
string Trekendeshi = null;
int gjetja = 0;
for (KendiA = 1; KendiA <= 15; KendiA++)
{
for (KendiB = 1; KendiB <= 15; KendiB++)
{
for (H = 1; H <= 30; H++)
{
if ((Math.Pow(KendiA, 2) + Math.Pow(KendiB, 2) == Math.Pow(H, 2)))
{
gjetja = gjetja + 1;
Trekendeshi = gjetja + "\t" + KendiA + "\t" + KendiB + "\t" + H;
Console.WriteLine(Trekendeshi);
}
}
}
}
It's much easier to find pythagorean triples than to iterate through every set of three. Have a look at http://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples for instance.
Also, it's better to square integers by multiplying it with itself rather than using pow(i,2).
Your code works well but it prints repeated ones. Count KendiB from KendiA solves your problem
for (KendiA = 1; KendiA <= 15; KendiA++){
for (KendiB = KendiA; KendiB <= 15; KendiB++){
Here is simplest way
for(int i=2; i<10; i++){
int a = 2*i;
int b = i*i-1;
int c = i*i+1;
System.out.println(a + " " + b + " " + c);
}