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













0















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;











share|improve this question
























  • 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















0















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;











share|improve this question
























  • 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













0












0








0








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;











share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 10 at 2:08







pijoborde

















asked Mar 8 at 21:45









pijobordepijoborde

86




86












  • 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















0














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();






share|improve this answer























  • 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











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
);



);













draft saved

draft discarded


















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









0














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();






share|improve this answer























  • 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















0














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();






share|improve this answer























  • 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













0












0








0







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();






share|improve this answer













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();







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page