Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:34:04+00:00 2026-06-03T20:34:04+00:00

I am using TextReader to read in each line of a tab delimited file.

  • 0

I am using TextReader to read in each line of a tab delimited file.
For each line that it reads I split on the tabs and populate a string array.
I can then access specific positions in the string array to get the values.

This works great for strings like: userWorkload.SSN = arrColumns[0];
However, I get an error message at runtime when I try to convert one of the columns to an int: userWorkload.contactHours = Convert.ToInt32(arrColumns[9]);

Here is my code:

List<UserWorkload> userWorkloads = new List<UserWorkload>();
TextReader tr = File.OpenText("fall11-tab.txt");
string strLine = string.Empty;
string[] arrColumns = null;
while ((strLine = tr.ReadLine()) != null)
{
     UserWorkload userWorkload = new UserWorkload();

     arrColumns = strLine.Split('\t');
     userWorkload.SSN = arrColumns[0];
     userWorkload.contactHours = Convert.ToInt32(arrColumns[9]);     

     userWorkloads.Add(userWorkload);
 }

and the UserWorkload class just contains simple getter / setters:

class UserWorkload 
{
     public string SSN { get; set; }
     public int contactHours { get; set; }
}

Here is my error:
Unhandled Exception: System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ToInt32(String value)
at Snyder5Creator.InputFileReader.buildRowList() in C:\Users\baxter.NET\Documents\Visual Studio 2010\Projects\User5Creator\User5Creator\InputFileReader.cs:line 31
at Snyder5Creator.Program.Main(String[] args) in C:\Users\baxter.NET\Documents\Visual Studio 2010\Projects\User5Creator\Snyder5Creator\Program.cs:line 24

Line 31 is: userWorkload.contactHours = Convert.ToInt32(arrColumns[9]);

Any help on this would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:34:05+00:00Added an answer on June 3, 2026 at 8:34 pm

    You probably have invalid data in one or more of the records. If you’re OK with ignoring these errors, you could use int.TryParse:

     int parsedNumber;
     userWorkload.contactHours = int.TryParse(arrColumns[9], out parsedNumber) ? parsedNumber : -1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, this is how I'm opening a file to read it: using (TextReader reader
I am parsing a very large file of records (one per line, each of
Is there a way to know that a call to TextReader.Read or TextReader.ReadToEnd call
I have to read line-by-line a log file. It's about 6MB in size and
With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
I am using a Socket to receive data via TCP, and TextReader.ReadLine to read
I am using a XML Text reader on a XML file that may contain
I am developing a webpart that should read an XML file. For this I
This is my solved program that will read a text file with delimiters and
using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.