| View previous topic :: View next topic |
| Author |
Message |
hakan
Joined: Sat Feb 06, 2010 4:51 pm Posts: 4 Location: Sweden
|
Posted: Thu Feb 11, 2010 6:39 pm Post subject: Coding Academy Homework 1 |
|
|
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 |
|
 |
spaceyhase LXF regular
Joined: Mon Jun 30, 2008 1:07 pm Posts: 116
|
Posted: Sat Feb 13, 2010 11:40 am Post subject: |
|
|
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). |
|
| Back to top |
|
 |
hakan
Joined: Sat Feb 06, 2010 4:51 pm Posts: 4 Location: Sweden
|
Posted: Sat Feb 13, 2010 5:29 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Fay_Zee
Joined: Sat Feb 13, 2010 4:49 pm Posts: 3 Location: South East England
|
Posted: Sat Feb 13, 2010 8:03 pm Post subject: |
|
|
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:
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 |
|
 |
Fay_Zee
Joined: Sat Feb 13, 2010 4:49 pm Posts: 3 Location: South East England
|
Posted: Sat Feb 13, 2010 8:11 pm Post subject: |
|
|
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 |
|
 |
hakan
Joined: Sat Feb 06, 2010 4:51 pm Posts: 4 Location: Sweden
|
Posted: Sun Feb 14, 2010 4:22 pm Post subject: |
|
|
Thank you, it works!
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 |
|
 |
Fay_Zee
Joined: Sat Feb 13, 2010 4:49 pm Posts: 3 Location: South East England
|
Posted: Tue Feb 16, 2010 12:14 am Post subject: |
|
|
| Yes, both work for me, but you're right to continue now that you have the homework solved. |
|
| Back to top |
|
 |
Evil_Trevor
Joined: Fri Oct 05, 2007 2:02 pm Posts: 1
|
Posted: Tue Feb 16, 2010 3:47 pm Post subject: |
|
|
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 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 |
|
 |
hakan
Joined: Sat Feb 06, 2010 4:51 pm Posts: 4 Location: Sweden
|
Posted: Wed Feb 17, 2010 11:12 am Post subject: |
|
|
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 previous topic :: View next topic |
|