Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, what did I do wrong when reading a file? It only reads as far as "price" is and doesnt read any further. For surname, name, bank and num it gives ,,unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.''
What should I do?
My code is:
C#
static void Read(string fd)
        {
            int nn, mm;
            string surname, name, bank, num;
            double price;
            using (StreamReader reader = new StreamReader(fd))
            {
                string line = reader.ReadLine();
                string[] parts=line.Split(' ');
                nn = int.Parse(parts[0]);
                mm = int.Parse(parts[1]);
                price = double.Parse(parts[2]);
                for (int i = 0; i < nn; i++)
                {
                    line = reader.ReadLine();
                    surname = parts[3];
                    name = parts[4];
                    bank = parts[5];
                    num = parts[6];
                }       
            }
        }


this is the file I want to read
4 7 2.99
Auksutis Auksas Seb AA988957532897855
Labutis Labas Swedbank BB623378348393499
Gerutis Geras Luminor CC234132535698906
Blogutis Blogas Seb DD989677678986896


What I have tried:

I tried adding another string[] partss = line.Split(' '); after the for cycle, but that didnt help.
Posted
Updated 6-Dec-20 20:47pm
Comments
PIEBALDconsult 6-Dec-20 18:51pm    
Well, you're splitting only the first line, sooo...

When you read the lines inside your for loop, you have not split them at all - and you don't do anything useful with them even if you had.
The first thing you need to do is read the comment you teacher put in your homework:
C#
// you should try to split line in parts at this point

So replace that comment with another call to string.Split and save the new array of strings that generates.

But then the array will be a fresh, new one - so it's indexes will start from zero again, not be randomly numbers ass 3, 4, 5, and 6!

So change the indexes inside your for loop to reference the actual data.

But ... even then, you aren't doing anything with the data, other than storing it in four variables which you then overwrite the next time round the for loop, throwing away the previous lines data!
So you need a collection (an Array, List, whatever you are familiar and comfortable with) to save the data in, and I'd suggest that creating a Customer class to hold a single customers data would be a good idea - you can then add each separate customer to your collection for later processing.
 
Share this answer
 
C#
static void Read(string fd)
        {
            int nn, mm;
            string surname, name, bank, num;
            double price;
            using (StreamReader reader = new StreamReader(fd))
            {
                string line = reader.ReadLine();
                string[] parts=line.Split(' ');
                nn = int.Parse(parts[0]);
                mm = int.Parse(parts[1]);
                price = double.Parse(parts[2]);
                for (int i = 0; i < nn; i++)
                {
                    line = reader.ReadLine();
                    // you should try to split line in parts at this point
                    surname = parts[3];
                    name = parts[4];
                    bank = parts[5];
                    num = parts[6];
                }       
            }
        }

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900