I am using split to split a string into a int, int, string. When I try int, string, string my program works fine. When I try int, int , string I get a run time error of an input string was not in a correct format. I am leaving a note by were the code is that is giving me problems. It is in main. I am including my code for class in case it helps. The file I am reading in is 15 lines and in each line is 10(two ints) 15(two ints) PM(two letters).
this is my main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace P3
{   
    
    class Program
    {
        static void Main(string[] args)
        {
            
            int limit = 15;
            TimePiece [] timepiece = new TimePiece[limit];
            string line=" ";
          
            //int toggleCount;
            string filename = "f:\\date.txt";

            FileStream f = new FileStream(filename, FileMode.Open);
            StreamReader stream = new StreamReader(f);

            for (int i = 0; i < limit; i++)
            {
                //maybe bring in strin and do spit in constuctor.
                line = stream.ReadLine();    // header line
                string[] array = line.Split(' ');
                int milTime = Convert.ToInt32(array[0]);
                int pieceID = Convert.ToInt32(array[1]);//this is line giving me problems
                string tog = array[2];
                timepiece[i] = new TimePiece(milTime, pieceID, tog);
              

            }
            stream.Close();
            for (int i = 0; i < limit; i++)
            {

                timepiece[i].display();
             
               
            }
          

        }
    }
}

This is my class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace P3
{
    public class TimePiece
    {
        private static int num = 99;
        private int ID;
        private int hoursTime;
        private int minute;
        private string m;

        public TimePiece(int hoursTime, int minute, string m)
        {
            this.hoursTime = hoursTime;
            this.minute = minute;
            this.m = m;
            ID = num;
            num++;
        }
      

        public void display()
        {
            Console.WriteLine(hoursTime + " " + minute + " " + m +" " + ID);
        }
        //Precondition-none
        //Postcondition- ID is incremented by one.
        public int  IDnum()
        {
            num++;
            return num;
        }


       
    }
}

Recommended Answers

All 2 Replies

Check the Lenght property before you use any index

Set a breakpoint at line 30 of your Main program.
See in the debugger what your string[] array looks like.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.