Noob needs help with C#

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Noob needs help with C#

Postby Judda » Sun Jan 10, 2010 9:59 pm

Been Ubuntu user for a long while now and I'd thought I'd have a good at learning to program (no previous experience). So I bought the recent special edition "Paul Hudson's Coding Academy".

Fell at the first hurdle :(

Having trouble with homework(1/3) on Project One. Done 2 & 3 with ease.

How would I count the number of items (it's a to do list) I have in an array (single dim) as I need to print out their total?

Any help/suggestions welcome and appreciated.
Judda
 
Posts: 9
Joined: Sun Jan 10, 2010 9:41 pm

Postby ollie » Mon Jan 11, 2010 2:47 am

You may find C# Count Array Elements useful, it is only a new posting.

HTH
User avatar
ollie
Moderator
 
Posts: 2749
Joined: Mon Jul 25, 2005 11:26 am
Location: Bathurst NSW Australia

Postby Judda » Mon Jan 11, 2010 12:01 pm

Thanks Ollie I'll have a look.
Judda
 
Posts: 9
Joined: Sun Jan 10, 2010 9:41 pm

Postby Judda » Mon Jan 11, 2010 12:14 pm

I've just had a read and that's too advanced for what is expected for Project1.

Here is the code:

using System;
using System.IO;

namespace ToDoList
{
class MainClass
{
public static void Main(string[] args)
{
if (args.Length == 0) {
Console.WriteLine("Usage: list | add <item> | del <num>");
return;
}

if (args[0] == "add") {
File.AppendAllText("todo.txt", args[1] + "\n");
} else if (args[0] == "del") {
var contents = File.ReadAllLines("todo.txt");
File.Delete("todo.txt");

var counter = 1;
var deleteme = int.Parse(args[1]);

foreach(var item in contents) {
if (counter != deleteme) {
File.AppendAllText("todo.txt", item + "\n");
}

counter++;
}
} else {
var contents = File.ReadAllLines("todo.txt");
var counter = 1;

foreach(var item in contents) {
Console.WriteLine(counter + ": " + item);
counter++;
}
}
}
}
}

I was thinking is there any way to just take the value of the counter when it finishes as this will have counted the items?

P.S. sorry it won't stay indented
Judda
 
Posts: 9
Joined: Sun Jan 10, 2010 9:41 pm

Postby pasti » Mon Jan 11, 2010 1:15 pm

Hello

I just got the Lxf special edition "coding Academy", but I'm stuck at the first project although I don't seem to be the only person who is, one of my problems is with the first command you have to enter into the terminal:-

cd ToDoList/ToDoList/bin/Debug

when I hit enter all I get is "no such file or directory"

And when I enter this line:-

echo Meh > todo.txt

nothing at all happens, just a blinking cursor next to my name

then I get completely stuck, both from the magazine & the online tutorial, especially the part where it says you have to change the line "using System" to "using System.IO;", I mean do you delete the first line, add another line, spacing or what?,
anyway where do I enter:-

var contents = File.ReadAllText("todo.txt");

as in the magazine it doesn't tell you, but online it says this:

"Put that into your program just before the Console.WriteLine() line; we'll be using it soon.",
but there aren't any screenshots or anything, And I really feel cheated out of £10, I didn't need the dvd I use ubuntu, and there seems to be more info for free in the online version, all in all feel very frustrated, and as I said a little bit cheated
pasti
 
Posts: 2
Joined: Wed Sep 17, 2008 4:36 pm

Postby Ram » Mon Jan 11, 2010 1:40 pm

pasti wrote: cd ToDoList/ToDoList/bin/Debug

when I hit enter all I get is "no such file or directory"


This is the directory you should have enter in MonoDevelop Location Field just below the Name Field when you named the Project.

If you have not changed it from the default, then you'll find it in /home/hudzilla/Projects/ToDoList/ToDoList/bin/Debug

Provided that you have ran the initial Hello World program by pressing F5

pasti wrote: And when I enter this line:-

echo Meh > todo.txt

nothing at all happens, just a blinking cursor next to my name



This will have created a text file in the directory you ran the command from, probably your home directory as you had not cd as above..

pasti wrote:then I get completely stuck, both from the magazine & the online tutorial, especially the part where it says you have to change the line "using System" to "using System.IO;", I mean do you delete the first line, add another line, spacing or what?,



The line actually says:

Change that one using line to this:
Code: Select all
 
using System;
using System.IO;



pasti wrote: I didn't need the dvd I use ubuntu, and there seems to be more info for free in the online version, all in all feel very frustrated, and as I said a little bit cheated


You'll find all the code on the DVD, just browse it if you don't wish to use it.

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

Postby pasti » Mon Jan 11, 2010 4:32 pm

ram

thanks for the reply, I'm still hopelessly lost however, I don't have an "hudzilla" directory anywhere, I'm not using the "live" dvd, just my install of karmic with monodevelop, I know where mono has created the directory, but it hasn't created a text file anywhere in it, I've gone through both the magazine instructions & the online tutorial and still get the same problem, I just don't know what to do next, the instructions aren't exactly newbie programmer friendly, I say that as I'm not new to linux, ah well!
pasti
 
Posts: 2
Joined: Wed Sep 17, 2008 4:36 pm

Postby Ram » Mon Jan 11, 2010 5:10 pm

As you not a linux newbie then you should that echo Meh > todo.txt will create a file in the current directory that your terminal is using. echo is a stand unix command to display something usually on screen, in this case it's used with > which redirects it from the screen to a file, so it echos Meh to a file call todo.txt. If the file did not exist it would create it.

to test. open a terminal and do the following

Code: Select all
echo test > todo.txt
ls -ltr


The last file in the list should be todo.txt, is it ?

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

Postby Ram » Mon Jan 11, 2010 6:30 pm

Judda wrote:I was thinking is there any way to just take the value of the counter when it finishes as this will have counted the items?

P.S. sorry it won't stay indented


You could, I did first time round, but there is a one line solution.
It does not involve counter but "XXXX.Length"
I'll let you work out what replaces the XXXX

P.S.
For indentation use the code tags

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

Postby compo » Sat Jan 16, 2010 8:42 pm

[quote="pasti"]Hello

I just got the Lxf special edition "coding Academy", but I'm stuck at the first project although I don't seem to be the only person who is, one of my problems is with the first command you have to enter into the terminal:-

cd ToDoList/ToDoList/bin/Debug

when I hit enter all I get is "no such file or directory"

Hi pasti,

The files are stored in a folder/directory called "Projects" in your home directory. The command in the magazine should read:-

cd Project/TodoList/ToDoList/bin/Debug

It took me a while to discover this. I know everyone makes mistakes but if your trying to teach someone to write code little things like that can really put you off.

compo
compo
 
Posts: 4
Joined: Sat Dec 12, 2009 8:24 pm

Postby Judda » Sun Jan 17, 2010 1:02 pm

Ram wrote:
Judda wrote:I was thinking is there any way to just take the value of the counter when it finishes as this will have counted the items?

P.S. sorry it won't stay indented


You could, I did first time round, but there is a one line solution.
It does not involve counter but "XXXX.Length"
I'll let you work out what replaces the XXXX

P.S.
For indentation use the code tags


Thanks for the pointer, much much simplier than using the counter :)

Thanks again :)
Judda
 
Posts: 9
Joined: Sun Jan 10, 2010 9:41 pm

noob needs help

Postby branmask » Mon Mar 01, 2010 7:57 pm

Hello,

I'm having trouble actually deleting something from the file. When I try running the del 1(or 2 or 3) command I get this error message:

Unhandled Exception: System.IO.FileNotFoundException: Could not find file "/home/.../Projects/ToDoList/ToDoList/bin/Debug/todo.txt.".
File name: '/home/.../Projects/ToDoList/ToDoList/bin/Debug/todo.txt.'
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000]
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
at System.IO.File.OpenRead (System.String path) [0x00000]
at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) [0x00000]
at System.IO.StreamReader..ctor (System.String path) [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string)
at System.IO.File.OpenText (System.String path) [0x00000]
at System.IO.File.ReadAllLines (System.String path) [0x00000]
at ToDoList1.MainClass.Main (System.String[] args) [0x00000]

This is my code:


if(args.Length==0){
Console.WriteLine("Usage:list|add<item>|del<num>");
return;
}
if (args[0]=="add"){
var contents=File.ReadAllLines("todo.txt");
var counter=1;
foreach(var item in contents){
Console.WriteLine(counter+":"+item);
counter++;
}
}else if(args[0]=="del"){
var contents=File.ReadAllLines("todo.txt.");
File.Delete("todo.txt");
var counter=1;
var deleteme=int.Parse(args[1]);
foreach(var item in contents){
if(counter!=deleteme){
File.AppendAllText("todo.txt",item +"\n");
}

counter++;
}
}else{
var contents=File.ReadAllLines("todo.txt");
var counter=1;

foreach(var item in contents){
Console.WriteLine(counter+":"+item);
counter++;
}

}
}
}
}

I haven't been able to figure out what I'm doing wrong as of yet. Any help would be most appreciated. Thanks
branmask
 
Posts: 14
Joined: Thu Feb 18, 2010 1:19 am

Postby Ram » Mon Mar 01, 2010 10:29 pm

branmask no need to multi post.

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

Postby branmask » Tue Mar 02, 2010 12:58 am

Ram,
"branmask no need to multi post."

sorry for the multi post! thanks for the info.
branmask
 
Posts: 14
Joined: Thu Feb 18, 2010 1:19 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest