Stuck at the First Hurdle!

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Stuck at the First Hurdle!

Postby Buster » Sat Jan 02, 2010 7:18 pm

Hi All

I have never programmed before and decided to have a crack at C# on Monodevelop using the LXF coding academy tutorials on tuxradar. Unfortunately I have got stuck half way through the first one :oops:

This is my code:
Code: Select all
using System;
using System.IO;

namespace ToDoList
{
   class MainClass
   {
      public static void Main(string[] args)
      {
         if (args[0] == "add") {
            File.AppendAllText("todo.txt", args[1] + "\n");
      } else {
            var contents = File.ReadAllText("todo.txt");
            Console.WriteLine(contents);
            }            
      }
   }
}


And this is the error I get:

Code: Select all
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range.
  at ToDoList.MainClass.Main (System.String[] args) [0x00000] in /home/stu/Projects/ToDoList/ToDoList/Main.cs:16


This error occurs when I run "./ToDo.exe" without parameters. When I run it with something like ./ToDo.exe add "This is a test" it works fine.

If I understand the error correctly it is telling me that the array is not initialising properly from this line (I think):

Code: Select all
if (args[0] == "add") {


Having gone over my code several times, and spending a long time googling the issue I have got no further on.

Can anyone please explain to me where (and why) I have gone wrong so that I can owrk out how to get it right.

Cheers

Buster
My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing!
Buster
 
Posts: 7
Joined: Sat Jan 02, 2010 5:29 pm

Postby Buster » Sat Jan 02, 2010 10:03 pm

It's okay, I've finally worked out what is going wrong!

I haven't completed enough of the code to deal with no parameters at all being entered so when I run:

Code: Select all
./ToDoList.exe


it causes the error because these lines:

Code: Select all
if (args[0] == "add") {
            File.AppendAllText("todo.txt", args[1] + "\n");


are basically saying "if the first parameter equals 'add' then write 'parameter 2' to the file todo.txt".

Therefore when I run it with no parameters at all I get the error because the first part of the code (argument?) fails meaning that the second part cannot complete because it depends on part 1.

If I run it with a first parameter of anything but 'add', for instance:

Code: Select all
./ToDoList.exe foo


the program correctly displays the contents of todo.txt as it should.

If anyone has the time to confirm that I have understood this correctly it would be appreciated.

Buster
My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing!
Buster
 
Posts: 7
Joined: Sat Jan 02, 2010 5:29 pm

Postby Bazza » Sat Jan 02, 2010 11:30 pm

Well done that man...

If this is your first programming experience and you`ve
solved it yourself then you will easily get to grips with
programming in general...

In your codes` case...

args[x] are the "arguments" that follow the executable command.

Computer definition or arguments:-

(From the WWW.)
`In programming, a value that is passed between programs,
subroutines or functions. Arguments are independent items,
or variables, that contain data or codes. When an argument
is used to customize a program for a user, it is typically
called a "parameter."`

For example...

"MyCommand My name is Bazza" has four arguments,

args[0] == My
args[1] == name
args[2] == is
args[3] == Bazza

"MyCommand" by itself has NO arguments hence, (in your case),
your ORIGINAL error.

SO........

Judging by your code it requires two arguments

ToDoList add sometext<RETURN/ENTER>

"add" is the first argument args[0] that sets the condition
for the "if" statement.

AND

Assuming "todo.txt" exists "sometext" along with a "newline"
character will be added to the end of the file.

Note: I suspect "sometext" is not critical as args[1] becomes
a NULL, (no character in this case), and only a "newline" will
be added BUT "add", args[0], must be included for it to do
this...

that`s the best way I can describe it...

And finally your conclusion was basically correct.

Welcome to the fascination world of programming... :)
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1483
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby Buster » Sun Jan 03, 2010 9:47 pm

Bazza

Thanks for the comprehensive explanation, it is really appreciated :D

Your response raised another question for me though:

Note: I suspect "sometext" is not critical as args[1] becomes
a NULL, (no character in this case), and only a "newline" will
be added BUT "add", args[0], must be included for it to do
this...


I can see what you are saying here; and it makes perfect sense however that is not what is happening in practice and I wondered if you (or anyone else) could perhaps explain why?

I have clipped some code output below so that you can see what is going on:

Code: Select all
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ ./ToDoList.exe add Test
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ cat todo.txt
Test
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ ./ToDoList.exe add "This is a test"
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ cat todo.txt
Test
This is a test
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ ./ToDoList.exe add

Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range.
  at ToDoList.MainClass.Main (System.String[] args) [0x00000]
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ cat todo.txt
Test
This is a test
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$


The source code remains as previously posted.

It's not hugely important, I'm just curious that's all.

Thanks again

Buster
My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing!
Buster
 
Posts: 7
Joined: Sat Jan 02, 2010 5:29 pm

Postby Ram » Mon Jan 04, 2010 12:12 am

./ToDoList.exe add

Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range.
at ToDoList.MainClass.Main (System.String[] args) [0x00000]


You have not passed a second argument (args[1]) but the following code is expecting it.

The array currently only holds one element (argument) so crashes trying to read the second element.


Code: Select all
using System;
using System.IO;

namespace ToDoList
{
   class MainClass
   {
      public static void Main(string[] args)
      {
         if (args[0] == "add") {
            File.AppendAllText("todo.txt", args[1] + "\n");
      } else {
            var contents = File.ReadAllText("todo.txt");
            Console.WriteLine(contents);
            }           
      }
   }


An array is a group or set of things

args[] is the array in the above code.

colours[red,green,orange,purple] is an array of 4 colours.

Arrays usually index (count) from zero.

colours[0] = red
colours[1] = green
colours[2] = orange
colours[3] = purple


Code: Select all

 Console.WriteLine("The first colour is ", colours[0] + "\n" );



That would just print out The first colour is red

Code: Select all

 Console.WriteLine("The fifth colour is ", colours[4] + "\n" );



Would error with a Index out of range as there is no 5th element.
Last edited by Ram on Mon Jan 04, 2010 9:00 am, edited 1 time in total.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1684
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Buster » Mon Jan 04, 2010 8:53 am

Got it, thanks very much

Buster
My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing!
Buster
 
Posts: 7
Joined: Sat Jan 02, 2010 5:29 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest