Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Coding Academy Homework 1

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
hakan



Joined: Sat Feb 06, 2010 4:51 pm
Posts: 4
Location: Sweden

PostPosted: Thu Feb 11, 2010 6:39 pm    Post subject: Coding Academy Homework 1 Reply with quote

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.
Quote:
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:
         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
Back to top
View user's profile Send private message
spaceyhase
LXF regular


Joined: Mon Jun 30, 2008 1:07 pm
Posts: 116

PostPosted: Sat Feb 13, 2010 11:40 am    Post subject: Reply with quote

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 Smile



* (Accessing it will raise an out of bounds exception, iirc. The exception description is generally a good indication of what is wrong).
Back to top
View user's profile Send private message
hakan



Joined: Sat Feb 06, 2010 4:51 pm
Posts: 4
Location: Sweden

PostPosted: Sat Feb 13, 2010 5:29 pm    Post subject: Reply with quote

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. Smile

/Håkan
Back to top
View user's profile Send private message
Fay_Zee



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

PostPosted: Sat Feb 13, 2010 8:03 pm    Post subject: Reply with quote

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:
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:
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:
if ((args[0]=="add" || args[0]=="del") && (args.Length < 2)) {
               Console.WriteLine("Usage: list | add <item> | del <num>");
               return;
}


Fay
Back to top
View user's profile Send private message
Fay_Zee



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

PostPosted: Sat Feb 13, 2010 8:11 pm    Post subject: Reply with quote

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

Code:
if (args[0]=="add | del" && args.Length < 2) {
   Console.WriteLine("Usage: list | add <item> | del <num>");
   return;
}
Back to top
View user's profile Send private message
hakan



Joined: Sat Feb 06, 2010 4:51 pm
Posts: 4
Location: Sweden

PostPosted: Sun Feb 14, 2010 4:22 pm    Post subject: Reply with quote

Thank you, it works! Smile

That is, your first suggestion with the brackets does.

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


But as you said..
Quote:
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
Back to top
View user's profile Send private message
Fay_Zee



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

PostPosted: Tue Feb 16, 2010 12:14 am    Post subject: Reply with quote

Yes, both work for me, but you're right to continue now that you have the homework solved.
Back to top
View user's profile Send private message
Evil_Trevor



Joined: Fri Oct 05, 2007 2:02 pm
Posts: 1

PostPosted: Tue Feb 16, 2010 3:47 pm    Post subject: Reply with quote

Hi,

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

Code:
         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:
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:
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.
Back to top
View user's profile Send private message Yahoo Messenger
hakan



Joined: Sat Feb 06, 2010 4:51 pm
Posts: 4
Location: Sweden

PostPosted: Wed Feb 17, 2010 11:12 am    Post subject: Reply with quote

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


I tried your suggestions like this...
Code:
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:
(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
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast