0

I have a lot of data stored in multiple arrays that I would like to get a value from but I'm having trouble. The identifier arrays are

String[] seqNum2 = new String[600];
String[] seqNum = new String[600];

I want to be able to find the match data in those arrays and get information that aligns with the data in these arrays

String[] netOil2 = new String[600];
String[] netOil = new String[600];

So here is my code so far but it is not outputting the correct answer that I want, all it is outputting is an infinite loop that says "System.String[], NaN".

    private void netOilRadBtn_CheckedChanged(object sender, EventArgs e)
    {
        StreamWriter sw = new StreamWriter("test.txt"); //testing purposes only
        //StreamReader sr = new StreamReader("OUTPUT.CSV");
        double first;
        double second;


        for (int i = 0; i < netOil2.Length; i++)
            {
                for (int j = 0; j < netOil.Length; j++)
                {
                    if (seqNum2[i] == seqNum[j])
                    {
                        //sw.WriteLine("Find New Seq Num");
                        first = Convert.ToDouble(netOil2[i]);
                        second = Convert.ToDouble(netOil[j]);

                        double answer = (first - second) / first;

                        sw.WriteLine("{0}, {1}", seqNum2, answer);
                    }
                }
            }

    }

All I want to be able to do is output the matching seqNum and then the double answer. Any help would be greatly appreciated.

7
  • "String seqNum2 = new String[600];" will this compile? Commented Jun 4, 2013 at 14:49
  • @David I just fixed it to make it look right. Commented Jun 4, 2013 at 14:51
  • I think you need to write current item from array sw.WriteLine("{0}, {1}", seqNum2[i], answer); Commented Jun 4, 2013 at 14:53
  • 1
    It can't actually be an infinite loop, though it will print that out 600 x 600 = 360000 times. I can see why you might think it's infinite, but it will even eventually exit. Commented Jun 4, 2013 at 14:55
  • 3
    Paired arrays like that are an anti-pattern. don't do it. Define a class that has a seqNum and netOil property, and then have collections of that class type. Commented Jun 4, 2013 at 14:55

4 Answers 4

2

So, this line:

sw.WriteLine("{0}, {1}", seqNum2, answer);

outputs System.String[], NaN.

seqNum2 is an array, that's why you get System.String[] in the output. You probably wanted seqNum2[i] if you want to see the element of seqNum2 which is currently being processed. Of course, since you are not initializing the array elements (see below), even with this change you won't see what you expect, as the value is null and it is formatted to an empty string, so you will get , Nan as output.

The reason you get NaN in output is line:

double answer = (first - second) / first;

I guess that first and second are both 0. If only first were 0, you would get Infinity, and otherwise you would get 0.

These values are set to 0 most likely because you don't set string values in netOil and netOil2 arrays, so you have 600 nulls and Convert.ToDouble(null) returns 0.

Debug your code, and see on what values you call Convert.ToDouble and what is the returned value.

Sign up to request clarification or add additional context in comments.

3 Comments

I changed it to seqNum2[i] but now it's not outputting anything
sure, what value would you expect?
which happens to have the value of?
1
private void netOilRadBtn_CheckedChanged(object sender, EventArgs e)
{
    using (var sw = new StreamWriter("test.txt")) //testing purposes only
    { 
        //StreamReader sr = new StreamReader("OUTPUT.CSV");

        var items =  netOil.Zip(seqNum, (oil, seq) => new {Oil = oil, Seq = seq});
        var items2 =  netOil2.Zip(seqNum2, (oil, seq) => new {Oil = oil, Seq = seq});

        foreach (var item in items.Join(items2,
                     i=>i.Seq,i=>i.Seq, (a,b)=>
                     {
                         double first = Convert.ToDouble(a.Oil);
                         double second = Convert.ToDouble(b.Oil);

                         double answer = (first - second) / first;
                         return string.Format("{0}, {1}", a.Seq, answer);
                     }))

        { 
            sw.WriteLine(item);
        }
    }
}

1 Comment

@Joel this gives me an error on the if statement "cannot implicity convert type 'string' to 'bool
0

You get "System.String[]" since you are calling Array.ToString(). You get NaN since obviously answer is NaN. My guess is that one of those

                    first = Convert.ToDouble(netOil2[i]);
                    second = Convert.ToDouble(netOil[j]);

returns NaN. Put a breakpoint in those lines and check whether the values of netOil2[i] netOil[j] actually represent numbers (keep in mind that what Convert.ToDouble returns is based on your current culture).

Comments

0

Instead of pair arrays, I would use Dictionary collections.

For example:

Dictionary<Int32, Double> dictOil1 = new Dictionary<Int32, Double();
Dictionary<Int32, Double> dictOil2 = new Dictionary<Int32, Double();

//If seqNum and netOil _really are_ strings...
Int32 seq, seq2;
Double oil, oil2;
if (Int32.TryParse(seqNum, out seq) && Double.TryParse(netOil, out oil))
    dictOil1.Add(seq, oil);
if (Int32.TryParse(seqNum2, out seq2) && Double.TryParse(netOil2, out oil2))
    dictOil2.Add(seq2, oil2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.