473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Split function wont work

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
7 4450
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Arjen | last post by:
Hi All, What I want to is using a string as PATTERN in a split function. This makes it possible for me to change the PATTERN on one place in my script... For example: $separator = ";"; $line...
2
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces,...
4
by: Henry Chen | last post by:
Hi, I have a string that needs to be parsed into the string. The separator is not char. It is something like " at ". With current string.Split function, it doesn't work. Is there any exist...
5
by: NewToThis | last post by:
I am trying to use the split function to bread up lines in a file I am reading from. Some lines are working just fine, but a couple of the lines don't split up the way I would have thought. ...
4
by: Brian Glen Palicia | last post by:
My goal is to accept input from the user into a text box and then parse the data using split(). The first step is this tiny program to test the split() function. It runs in IE, but in Mozilla it...
2
by: Jay | last post by:
So I'm writting this software that talks to an IRC server; and occasionaly IRC servers send back data two lines at a time with lines breaks inside. For my own sanity I had been using the split...
12
by: Jay | last post by:
Let's say, for instance, that one was programming a spell checker or some other function where the contents of a string from a text-editor's text box needed to be split so that the resulting array...
4
by: Steven D'Aprano | last post by:
I'm having problems passing a default value to the maxsplit argument of str.split. I'm trying to write a function which acts as a wrapper to split, something like this: def mysplit(S, sep=None,...
2
by: eddiefisher41 | last post by:
Hey Guys. If I have a string say "abcdefg" how do i split the string without delimiters. What i need is a list containing each of the chars: e.g. The string.split() method wont work as it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.