Coding Academy Homework 1

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Coding Academy Homework 1

Postby hakan » Thu Feb 11, 2010 6:39 pm

I am a 62 years old "linux.fan" from Sweden who never done any programming before, but was curious when I saw this, so I bought the magazine and started.

I finished Projekt 1....almost...couldn't solve part 2 of the homework.
Fix the program so that if "add" or "del" are used without second arguments, they print the usage message and exit.

This is what I tried...
"if args[0] is add or del and args[1] don't exist then write ...."
Code: Select all
         if (args[0]=="add | del" && args[1].Length == 0) {
            Console.WriteLine("Usage: list | add <item> | del <num>");
            return;
         }

..is my last try, it don't work either...i think I'm not doing this the right way at all.

I appreciate all help, and excuse me for my "bad" English.


/Håkan
hakan
 
Posts: 4
Joined: Sat Feb 06, 2010 4:51 pm
Location: Sweden

Postby spaceyhase » Sat Feb 13, 2010 11:40 am

You should know that 'args' is an array of the command line arguments passed to the program so args[0] refers to the first argument and args[1] the second. However, as the program has not been passed the second argument following 'add' or 'del' then args[1] doesn't exist*; it's not in the array boundary. You should test this :)



* (Accessing it will raise an out of bounds exception, iirc. The exception description is generally a good indication of what is wrong).
spaceyhase
LXF regular
 
Posts: 116
Joined: Mon Jun 30, 2008 12:07 pm

Postby hakan » Sat Feb 13, 2010 5:29 pm

Thank you for answering, now I understand a little about "out of bounds", even though I don't know how to solve it...

It is a little "space" in your answer, did you have any suggestions that I could try there?

I have done a little "searching"...and found this...
http://tuxradar.com/hca
..with a solution to the problem...and I belive it'is another way of "thinking/doing"...
"if args[0] isn't "list" or "clear" then.."
..by putting the "code" in the right position..that's smart. :)

/Håkan
hakan
 
Posts: 4
Joined: Sat Feb 06, 2010 4:51 pm
Location: Sweden

Postby Fay_Zee » Sat Feb 13, 2010 8:03 pm

Hi Håkan, you've got the basic idea, so congratulations on that - you'll make a programmer!

In the first portion of your expression you have to repeat the " args[0]== " part for each test, so that should read:

Code: Select all
args[0]=="add" || args[0]=="del"


Then in the last portion, you don't yet know if the second argument, " args[1] ", exists, so you can't use it in your test. Instead, you should do something like:

Code: Select all
args.Length < 2


Lastly, because your test involves both an " or " and an " and " operand, you might use brackets for clarity and to assure yourself they will be run in the correct order (there are specific rules on order but that can come later):

Code: Select all
if ((args[0]=="add" || args[0]=="del") && (args.Length < 2)) {
               Console.WriteLine("Usage: list | add <item> | del <num>");
               return;
}


Fay
Fay_Zee
 
Posts: 3
Joined: Sat Feb 13, 2010 4:49 pm
Location: South East England

Postby Fay_Zee » Sat Feb 13, 2010 8:11 pm

Of course, what you had for your first portion was just fine. Here it is again:

Code: Select all
if (args[0]=="add | del" && args.Length < 2) {
   Console.WriteLine("Usage: list | add <item> | del <num>");
   return;
}
Fay_Zee
 
Posts: 3
Joined: Sat Feb 13, 2010 4:49 pm
Location: South East England

Postby hakan » Sun Feb 14, 2010 4:22 pm

Thank you, it works! :)

That is, your first suggestion with the brackets does.

But not the second (no brackets), then I get..
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range.
at ToDoList.MainClass.Main (System.String[] args) [0x00000]


But as you said..
there are specific rules on order but that can come later

..so unless you belive it's some "typing-error" on my side (ie. it works for you), I think I leave it for now and move on to Projekt 2

/Håkan
hakan
 
Posts: 4
Joined: Sat Feb 06, 2010 4:51 pm
Location: Sweden

Postby Fay_Zee » Tue Feb 16, 2010 12:14 am

Yes, both work for me, but you're right to continue now that you have the homework solved.
Fay_Zee
 
Posts: 3
Joined: Sat Feb 13, 2010 4:49 pm
Location: South East England

Postby Evil_Trevor » Tue Feb 16, 2010 3:47 pm

Hi,

I noticed a few things in the code that it might help to know

Code: Select all
         if (args[0]=="add | del" && args[1].Length == 0) {
            Console.WriteLine("Usage: list | add <item> | del <num>");
            return;
         }


The first reason this doesn't work is that the first condition tested is always false as what you are testing is to see if the user entered todolist.exe "add | del". What you meant was probably
Code: Select all
args[0]=="add" || "del"
where the two things are quoted but the or operator '||' is not.

The second is a bit of a fault with the lack of detail Paul has at this stage in the tutorial. Testing the length of args[1] could be a mistake if only 1 argument was past to the program, the correct thing to do is to check how many arguments were past, which is done by checking the size of the array with
Code: Select all
args.length==2
the mono/.net environments are helpfully unhelpful in that to access an array you start at 0 but if you test the length the results start at 1 if there is 1 element args[0] !

Trev.
Evil_Trevor
 
Posts: 1
Joined: Fri Oct 05, 2007 1:02 pm

Postby hakan » Wed Feb 17, 2010 11:12 am

Hi,
Thank you for the response, I have tried it and the only thing I got working is suggestion 1 (from Fay_Zee).
Code: Select all
if ((args[0]=="add" || args[0]=="del") && (args.Length < 2))


I tried your suggestions like this...
Code: Select all
if ((args[0]=="add" || "del") && (args.Length < 2))

..gives me "build-error" Operator || cannot be applied to operands of type "bool" or "string"

..and this (whitout brackets)..
Code: Select all
(args[0]=="add" || "del" && args.Length < 2)

..gives me "build-error" Operator && cannot be applied to operands of type "string" or "bool"

I think it must be some "typing-error" from me, but I can't figure out what?

/Håkan
hakan
 
Posts: 4
Joined: Sat Feb 06, 2010 4:51 pm
Location: Sweden


Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest