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

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme