0

The textfile I'm reading from has numbers in it.

The return is System32 Int[] and not the numbers I want to see. What am I doing wrong?

static void Main(string[] args)
{
    StreamReader fromFile = new StreamReader("test.txt");
   int[] numbers = gatherNumbers(fromFile);
   Console.WriteLine("Files from test.txt has been gathered...");
    string textFile = userInput("Enter the filename that you wish to store the result in: ");
    StreamWriter tillFil = new StreamWriter(textFile);            
    tillFil.WriteLine("Summa " + numbers);
    Console.WriteLine("Summa " + numbers);
    tillFil.Close();
    Console.ReadLine();
}

private static int[] gatherNumbers(StreamReader fromFile)
{
    List<int> listan = new List<int>();
    string rad = fromFile.ReadLine();
    while (rad != null)
    {
        int tal = Convert.ToInt32(rad);
        listan.Add(tal);
        rad = fromFile.ReadLine();
    }     
    return listan.ToArray();     
}

private static string userInput(string nameOfTextFile)
{
    Console.WriteLine(nameOfTextFile);
    string answer = Console.ReadLine();
    return answer;
}
p.campbell
  • 98,673
  • 67
  • 256
  • 322
kaktusräv
  • 69
  • 6
  • Is there an error showing? Can you give us some of the inputs in the file. – Kelsey Abreu Jan 05 '15 at 21:25
  • 2
    You're trying to `WriteLine` an array. Don't do that. See [How to print contents of array horizontally?](http://stackoverflow.com/questions/3700448/how-to-print-contents-of-array-horizontally), among others, for solutions. – Paul Roub Jan 05 '15 at 21:27
  • 1
    You need to print the contents of the array. Calling `ToString()` (by doing `tillFil.WriteLine("Summa " + numbers);`) will not do it for you. – Jeff Mercado Jan 05 '15 at 21:27

2 Answers2

4

System.Array does not override ToString -- so you get the default behavior which prints the type name. It's no different than doing:

// namespace X { class Foo {} }
Foo f = new Foo();
Console.WriteLine(f); // Prints "X.Foo"

// http://ideone.com/YyfIqX
Console.WriteLine(new object[0]); // Prints System.Object[]
Console.WriteLine(new int[0]); // Prints System.Int32[]
Console.WriteLine(new string[0]); // Prints System.String[]

If you want something like a comma separated list you need to do that yourself.

var commaSeparated = String.Join(", ", numbers);
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
4

When concatenating a string and an instance of some other type, the compiler emits code that looks something like this:

var tempValue = "This is a string" + thisIsSomeVariable.ToString();

So, your code is more accurately represented by the following:

Console.WriteLine("Summa " + numbers.ToString());

What is numbers in this case? It's an array of integers, not a string or a primitive type. The default implementation of ToString for a non-primitive type simply prints out the type name, which in this case is

System.Int32[]

or an array of integers. What you want to do is something like

Console.WriteLine("Summa " + string.Join(" ", numbers));

The join will call ToString on each int in the array, then combine them with a space inbetween each.