Split function wont work

G

Guest

My code has a split function that should split the text file of numbers.
I've run this in previous programs as it is here and it worked, but now it
wont work for some reason and returns System_String[]. I don't see a
difference in the previous code and what I have now. Can anyone find where I
went wrong? Thanks a bunch!

//Open new stream reader and writer to read in file
//And write to a new file.
StreamWriter swa=new StreamWriter("c:\\afinal.txt");
StreamReader sra=new StreamReader("c:\\Acolumn.txt");
string aLine="";
double AField;

//Create and initialize arraylist
ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
//Read each line in and add to previous
//As long as the line contains a string of characters.
aLine=sra.ReadLine();
if (aLine != null)
aColumn.Add(aLine);
}
sra.Close();

//Splits the text file as space delimited, begins new line there
foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(new char[] {' '});
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
//Convert each line/item of array to double data type
AField = System.Convert.ToDouble (aField);
//Multiply each decimal value by constant
AField = AField*2;
//Write new value to console and new text file.
Console.Write (">"+ AField +"<");
swa.WriteLine(AField);
}

}

}
}
Console.Read();
swa.Flush();
swa.Close();
 
G

Guest

I'm sorry, I posted the code from the working program. The program I am
dealing with is the same as that code except as follows:

foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(new char [] {' '});
Console.WriteLine("aOutput" + " " + "aFields");
Console.WriteLine(aOutput + " " + aFields);
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
AField = System.Convert.ToDouble (aField);
Console.WriteLine(">"+ AField +"<");
}
}
}
}
Console.ReadLine();
swa.Flush();
swa.Close();
 
J

Jon Skeet [C# MVP]

Christine said:
My code has a split function that should split the text file of numbers.
I've run this in previous programs as it is here and it worked, but now it
wont work for some reason and returns System_String[]. I don't see a
difference in the previous code and what I have now. Can anyone find where I
went wrong? Thanks a bunch!

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jon Skeet [C# MVP]

Christine said:
I'm sorry, I posted the code from the working program. The program I am
dealing with is the same as that code except as follows:

foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(new char [] {' '});
Console.WriteLine("aOutput" + " " + "aFields");
Console.WriteLine(aOutput + " " + aFields);
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
AField = System.Convert.ToDouble (aField);
Console.WriteLine(">"+ AField +"<");
}
}
}
}

Again, please post a short but *complete* program.
 
L

Laura T.

Christine,

are you referring to the Console.WriteLine(aOutput + " " + aFields) line
that outputs System.String[]?
In that case the output correct, since aFields is really an array of
strings, and it's ToString() method does not print out
the elements, but it's underlying type (inherited from Object). You should
use aFields[<counter>] to print out an element.
The rest of the code should still work.

Laura

Christine said:
I'm sorry, I posted the code from the working program. The program I am
dealing with is the same as that code except as follows:

foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(new char [] {' '});
Console.WriteLine("aOutput" + " " + "aFields");
Console.WriteLine(aOutput + " " + aFields);
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
AField = System.Convert.ToDouble (aField);
Console.WriteLine(">"+ AField +"<");
}
}
}
}
Console.ReadLine();
swa.Flush();
swa.Close();

Christine said:
My code has a split function that should split the text file of numbers.
I've run this in previous programs as it is here and it worked, but now it
wont work for some reason and returns System_String[]. I don't see a
difference in the previous code and what I have now. Can anyone find where I
went wrong? Thanks a bunch!

//Open new stream reader and writer to read in file
//And write to a new file.
StreamWriter swa=new StreamWriter("c:\\afinal.txt");
StreamReader sra=new StreamReader("c:\\Acolumn.txt");
string aLine="";
double AField;

//Create and initialize arraylist
ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
//Read each line in and add to previous
//As long as the line contains a string of characters.
aLine=sra.ReadLine();
if (aLine != null)
aColumn.Add(aLine);
}
sra.Close();

//Splits the text file as space delimited, begins new line there
foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(new char[] {' '});
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
//Convert each line/item of array to double data type
AField = System.Convert.ToDouble (aField);
//Multiply each decimal value by constant
AField = AField*2;
//Write new value to console and new text file.
Console.Write (">"+ AField +"<");
swa.WriteLine(AField);
}

}

}
}
Console.Read();
swa.Flush();
swa.Close();
 
G

Guest

using System;
using System.IO;
using System.Collections;
namespace SplitWork
{
class Class1
{
static public void Main(string[] args)
{
{
//Open new streamreader and writer to read in file
//And write to a new file.
StreamWriter swa=new StreamWriter("c:\\afinal.txt");
StreamReader sra=new StreamReader("c:\\Acolumn.txt");
string aLine="";
double AField;

//Create and initialize arraylist to capture data
ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
aLine=sra.ReadLine();
if (aLine != null)
aColumn.Add(aLine);
}
sra.Close();

//Split function here
foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(new char[] {' '});
Console.WriteLine("aOutput" + " " + "aFields");
Console.WriteLine(aOutput + " " + aFields);
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
AField = System.Convert.ToDouble (aField);
Console.WriteLine (">"+ AField +"<");
swa.WriteLine(AField);
}
}
}
}
Console.ReadLine();
swa.Flush();
swa.Close();
}
}
}
}

Jon Skeet said:
Christine said:
My code has a split function that should split the text file of numbers.
I've run this in previous programs as it is here and it worked, but now it
wont work for some reason and returns System_String[]. I don't see a
difference in the previous code and what I have now. Can anyone find where I
went wrong? Thanks a bunch!

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
P

Paul E Collins

You get "System.String[]" when printing aFields to the console,
because it is a string array, not a string. If you want to print the
*contents* of the array, you will need to loop over its elements and
print them one at a time.

P.
 
J

Jon Skeet [C# MVP]

Christine said:
using System;
using System.IO;
using System.Collections;
namespace SplitWork
{
class Class1
{
static public void Main(string[] args)
{
{
//Open new streamreader and writer to read in file
//And write to a new file.
StreamWriter swa=new StreamWriter("c:\\afinal.txt");
StreamReader sra=new StreamReader("c:\\Acolumn.txt");
string aLine="";
double AField;

//Create and initialize arraylist to capture data
ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
aLine=sra.ReadLine();
if (aLine != null)
aColumn.Add(aLine);
}
sra.Close();

//Split function here
foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(new char[] {' '});
Console.WriteLine("aOutput" + " " + "aFields");
Console.WriteLine(aOutput + " " + aFields);

Right - you're trying to write out a string array, and Array.ToString
just prints out the type. What did you want it to do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top