473,651 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ReadL ine();
if (aLine != null)
aColumn.Add(aLi ne);
}
sra.Close();

//Splits the text file as space delimited, begins new line there
foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(n ew 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(A Field);
}

}

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

Nov 16 '05 #1
7 4461
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(n ew char [] {' '});
Console.WriteLi ne("aOutput" + " " + "aFields");
Console.WriteLi ne(aOutput + " " + aFields);
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
AField = System.Convert. ToDouble (aField);
Console.WriteLi ne(">"+ AField +"<");
}
}
}
}
Console.ReadLin e();
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.ReadL ine();
if (aLine != null)
aColumn.Add(aLi ne);
}
sra.Close();

//Splits the text file as space delimited, begins new line there
foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(n ew 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(A Field);
}

}

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

Nov 16 '05 #2
Christine <Ch*******@disc ussions.microso ft.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Christine <Ch*******@disc ussions.microso ft.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(n ew char [] {' '});
Console.WriteLi ne("aOutput" + " " + "aFields");
Console.WriteLi ne(aOutput + " " + aFields);
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
AField = System.Convert. ToDouble (aField);
Console.WriteLi ne(">"+ AField +"<");
}
}
}
}


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

--
Jon Skeet - <sk***@pobox.co m>
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.WriteLi ne(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*******@disc ussions.microso ft.com> wrote in message
news:38******** *************** ***********@mic rosoft.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(n ew char [] {' '});
Console.WriteLi ne("aOutput" + " " + "aFields");
Console.WriteLi ne(aOutput + " " + aFields);
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
AField = System.Convert. ToDouble (aField);
Console.WriteLi ne(">"+ AField +"<");
}
}
}
}
Console.ReadLin e();
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.ReadL ine();
if (aLine != null)
aColumn.Add(aLi ne);
}
sra.Close();

//Splits the text file as space delimited, begins new line there
foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(n ew 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(A Field);
}

}

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

Nov 16 '05 #5
using System;
using System.IO;
using System.Collecti ons;
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.ReadL ine();
if (aLine != null)
aColumn.Add(aLi ne);
}
sra.Close();

//Split function here
foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(n ew char[] {' '});
Console.WriteLi ne("aOutput" + " " + "aFields");
Console.WriteLi ne(aOutput + " " + aFields);
{
foreach (string aField in aFields)
{
if (aField.Length !=0)
{
AField = System.Convert. ToDouble (aField);
Console.WriteLi ne (">"+ AField +"<");
swa.WriteLine(A Field);
}
}
}
}
Console.ReadLin e();
swa.Flush();
swa.Close();
}
}
}
}

"Jon Skeet [C# MVP]" wrote:
Christine <Ch*******@disc ussions.microso ft.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
You get "System.Str ing[]" 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*******@disc ussions.microso ft.com> wrote:
using System;
using System.IO;
using System.Collecti ons;
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.ReadL ine();
if (aLine != null)
aColumn.Add(aLi ne);
}
sra.Close();

//Split function here
foreach (string aOutput in aColumn)
{
string[] aFields = aOutput.Split(n ew char[] {' '});
Console.WriteLi ne("aOutput" + " " + "aFields");
Console.WriteLi ne(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.co m>
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
7562
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 = "field1;value1"; local($field, $value) = split(/$separator/, $line);
2
12821
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, the employer code in column 23 for 29 spaces and employer description in column 53 for 30 spaces. I have tried modifying an existing file with no real success. I haven't found anything that specifically answers my question. Any guidance would be...
4
2574
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 functions like "Split" that I can use to do the job? Any idea of the simpliest way to do the job? Thanks in advance, Henry
5
4042
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. Here's part of the code. Dim strDelim As String = "*~" Dim delimiter As Char() = strDelim.ToCharArray Dim split As String() = Nothing Dim fieldCount As Integer Dim s As String
4
3601
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 just hangs and keeps loading forever. I checked around on the web and in USENET, but I haven't seen any mention of split() not working in Mozilla. Thoughts? Thanks in advance. <HTML> <HEAD> </HEAD>
2
1854
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 funciton to break these lines down to seperate ones and then process the server commands etc. However lately the split funciton seems incapable of finding chr(10)+chr(13) and wont split the lines. When I dump the data to a text file however and open...
12
1860
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 has each word as an element. Is there a shortcut to do this and, if not, what's the best and most efficient token group for the split function to achieve this?
4
2510
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, maxsplit=None): pre_processing() result = S.split(sep, maxsplit) post_processing() return result
2
13248
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 requires a delimiter within the string to split on and returns . Just going to use it for some error checking and validation used in a wx.TextCtrl in my GUI. Cheers. Ed
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8579
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7297
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.