FileStream Class C# Input from txt file to arrayHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?Create ArrayList from arrayAdding values to a C# arrayCreating a byte array from a streamRemove empty elements from an array in JavascriptPHP: Delete an element from an arrayGet int value from enum in C#How to remove item from array by value?How do I remove a particular element from an array in JavaScript?Remove duplicate values from JS array
Is it logically or scientifically possible to artificially send energy to the body?
Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?
Is there an expression that means doing something right before you will need it rather than doing it in case you might need it?
What mechanic is there to disable a threat instead of killing it?
Why doesn't using multiple commands with a || or && conditional work?
In 'Revenger,' what does 'cove' come from?
How can saying a song's name be a copyright violation?
Assassin's bullet with mercury
Unable to supress ligatures in headings which are set in Caps
Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?
Could the museum Saturn V's be refitted for one more flight?
Can a virus destroy the BIOS of a modern computer?
Alternative to sending password over mail?
Why does this cyclic subgroup have only 4 subgroups?
Unlock My Phone! February 2018
Determining Impedance With An Antenna Analyzer
Can the Meissner effect explain very large floating structures?
Personal Teleportation: From Rags to Riches
How does a predictive coding aid in lossless compression?
How to show a landlord what we have in savings?
Avoiding the "not like other girls" trope?
Why didn't Boeing produce its own regional jet?
Examples of smooth manifolds admitting inbetween one and a continuum of complex structures
What's the in-universe reasoning behind sorcerers needing material components?
FileStream Class C# Input from txt file to array
How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?Create ArrayList from arrayAdding values to a C# arrayCreating a byte array from a streamRemove empty elements from an array in JavascriptPHP: Delete an element from an arrayGet int value from enum in C#How to remove item from array by value?How do I remove a particular element from an array in JavaScript?Remove duplicate values from JS array
I am trying to make use of StreamReader and taking data from text files and store it into an array. I am having an issue where I think the fix is simple, but I am stumped. When I print the array, it prints every single token in the txt file instead of the single line of data containing the search name along with the 11 int tokens.
Long_Name.txtsample
public class SSA
public void Search()
Console.WriteLine("Name to search for?");
string n = Console.ReadLine();
Search(n, "Files/Names_Long.txt");
public int[] Search(string targetName, string fileName)
int[] nums = new int[11];
char[] delimiters = ' ', 'n', 't', 'r' ;
using (TextReader sample2 = new StreamReader("Files/Exercise_Files/SSA_Names_Long.txt"))
string searchName = sample2.ReadLine();
if (searchName.Contains(targetName))
Console.WriteLine("Found 0!", targetName);
Console.WriteLine("YeartRank");
else
Console.WriteLine("0 was not found!", targetName);
while (searchName != null)
string[] tokensFromLine = searchName.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
int arrayIndex = 0;
int year = 1900;
foreach (string token in tokensFromLine)
int arrval;
if (int.TryParse(token, out arrval))
nums[arrayIndex] = arrval;
year += 10;
Console.WriteLine("0t1", year, arrval);
arrayIndex++;
searchName = sample2.ReadLine();
return nums;
c# arrays delimiter streamreader
|
show 1 more comment
I am trying to make use of StreamReader and taking data from text files and store it into an array. I am having an issue where I think the fix is simple, but I am stumped. When I print the array, it prints every single token in the txt file instead of the single line of data containing the search name along with the 11 int tokens.
Long_Name.txtsample
public class SSA
public void Search()
Console.WriteLine("Name to search for?");
string n = Console.ReadLine();
Search(n, "Files/Names_Long.txt");
public int[] Search(string targetName, string fileName)
int[] nums = new int[11];
char[] delimiters = ' ', 'n', 't', 'r' ;
using (TextReader sample2 = new StreamReader("Files/Exercise_Files/SSA_Names_Long.txt"))
string searchName = sample2.ReadLine();
if (searchName.Contains(targetName))
Console.WriteLine("Found 0!", targetName);
Console.WriteLine("YeartRank");
else
Console.WriteLine("0 was not found!", targetName);
while (searchName != null)
string[] tokensFromLine = searchName.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
int arrayIndex = 0;
int year = 1900;
foreach (string token in tokensFromLine)
int arrval;
if (int.TryParse(token, out arrval))
nums[arrayIndex] = arrval;
year += 10;
Console.WriteLine("0t1", year, arrval);
arrayIndex++;
searchName = sample2.ReadLine();
return nums;
c# arrays delimiter streamreader
putsearchName = sample2.ReadLine();
outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine);nums[i] = arrval;
you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.
– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.for (int i = 0; i < nums.Length; i++)
delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.
– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39
|
show 1 more comment
I am trying to make use of StreamReader and taking data from text files and store it into an array. I am having an issue where I think the fix is simple, but I am stumped. When I print the array, it prints every single token in the txt file instead of the single line of data containing the search name along with the 11 int tokens.
Long_Name.txtsample
public class SSA
public void Search()
Console.WriteLine("Name to search for?");
string n = Console.ReadLine();
Search(n, "Files/Names_Long.txt");
public int[] Search(string targetName, string fileName)
int[] nums = new int[11];
char[] delimiters = ' ', 'n', 't', 'r' ;
using (TextReader sample2 = new StreamReader("Files/Exercise_Files/SSA_Names_Long.txt"))
string searchName = sample2.ReadLine();
if (searchName.Contains(targetName))
Console.WriteLine("Found 0!", targetName);
Console.WriteLine("YeartRank");
else
Console.WriteLine("0 was not found!", targetName);
while (searchName != null)
string[] tokensFromLine = searchName.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
int arrayIndex = 0;
int year = 1900;
foreach (string token in tokensFromLine)
int arrval;
if (int.TryParse(token, out arrval))
nums[arrayIndex] = arrval;
year += 10;
Console.WriteLine("0t1", year, arrval);
arrayIndex++;
searchName = sample2.ReadLine();
return nums;
c# arrays delimiter streamreader
I am trying to make use of StreamReader and taking data from text files and store it into an array. I am having an issue where I think the fix is simple, but I am stumped. When I print the array, it prints every single token in the txt file instead of the single line of data containing the search name along with the 11 int tokens.
Long_Name.txtsample
public class SSA
public void Search()
Console.WriteLine("Name to search for?");
string n = Console.ReadLine();
Search(n, "Files/Names_Long.txt");
public int[] Search(string targetName, string fileName)
int[] nums = new int[11];
char[] delimiters = ' ', 'n', 't', 'r' ;
using (TextReader sample2 = new StreamReader("Files/Exercise_Files/SSA_Names_Long.txt"))
string searchName = sample2.ReadLine();
if (searchName.Contains(targetName))
Console.WriteLine("Found 0!", targetName);
Console.WriteLine("YeartRank");
else
Console.WriteLine("0 was not found!", targetName);
while (searchName != null)
string[] tokensFromLine = searchName.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
int arrayIndex = 0;
int year = 1900;
foreach (string token in tokensFromLine)
int arrval;
if (int.TryParse(token, out arrval))
nums[arrayIndex] = arrval;
year += 10;
Console.WriteLine("0t1", year, arrval);
arrayIndex++;
searchName = sample2.ReadLine();
return nums;
c# arrays delimiter streamreader
c# arrays delimiter streamreader
edited Mar 10 at 2:08
pijoborde
asked Mar 8 at 21:45
pijobordepijoborde
86
86
putsearchName = sample2.ReadLine();
outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine);nums[i] = arrval;
you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.
– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.for (int i = 0; i < nums.Length; i++)
delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.
– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39
|
show 1 more comment
putsearchName = sample2.ReadLine();
outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine);nums[i] = arrval;
you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.
– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.for (int i = 0; i < nums.Length; i++)
delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.
– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39
put
searchName = sample2.ReadLine();
outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine); nums[i] = arrval;
you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.– Irdis
Mar 8 at 22:07
put
searchName = sample2.ReadLine();
outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine); nums[i] = arrval;
you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.
for (int i = 0; i < nums.Length; i++)
delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.– Irdis
Mar 8 at 22:31
looks fine.
for (int i = 0; i < nums.Length; i++)
delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39
you are welcome
– Irdis
Mar 8 at 22:39
|
show 1 more comment
1 Answer
1
active
oldest
votes
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071436%2ffilestream-class-c-sharp-input-from-txt-file-to-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
answered Mar 8 at 22:46
vscodervscoder
1499
1499
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071436%2ffilestream-class-c-sharp-input-from-txt-file-to-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
put
searchName = sample2.ReadLine();
outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine);nums[i] = arrval;
you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.
for (int i = 0; i < nums.Length; i++)
delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39