By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,070 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,070 IT Pros & Developers. It's quick & easy.

Split function wont work

P: n/a
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();

Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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" wrote:
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();

Nov 16 '05 #2

P: n/a
Christine <Ch*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
Christine <Ch*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
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" <Ch*******@discussions.microsoft.com> wrote in message
news:38**********************************@microsof t.com...
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" wrote:
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();

Nov 16 '05 #5

P: n/a
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 [C# MVP]" wrote:
Christine <Ch*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6

P: n/a
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.
Nov 16 '05 #7

P: n/a
Christine <Ch*******@discussions.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.