PDA

View Full Version : question about C# in xcode




twoodcc
Feb 21, 2007, 03:09 PM
for class, i have to write a program that reads a file.

i'm using 'StreamReader', but it seems xcode doesn't know what StreamReader is, unless i'm doing something wrong.

can i use StreamReader with xcode and mono and csharpplugin?

thanks



ChrisBrightwell
Feb 21, 2007, 03:10 PM
I'm 99.999% sure that Xcode and C# aren't friends with each other.

twoodcc
Feb 21, 2007, 03:30 PM
I'm 99.999% sure that Xcode and C# aren't friends with each other.

well they are. i actually got it to work, i forgot to change Using System.IO at the beginning. but now i'm having other problems.

i'm supposed to ask the user for a name of the file, and then ask the user for a character input, and then search the file and tell how many of that character is in the file.

here is what i have so far, but it can't compile:

using System;
using System.IO;

namespace WoodT_exercise5
{
class WoodT_exercise5
{
static void Main()
{
string fileName;
char bob;
char targetchar;
int count = 0;

Console.WriteLine("Enter the filename: ");
fileName = Console.ReadLine();

StreamReader inputFile = new StreamReader(fileName);

bob = inputFile.ReadLine();

Console.WriteLine("Please enter a character: ");
targetchar = Convert.ToChar(Console.ReadLine());

while (bob != null)
{
if (bob = targetchar)
{
count++;
bob = inputFile.Read();
}
}
inputFile.Close();
}
}
}

therevolution
Feb 21, 2007, 03:51 PM
here is what i have so far, but it can't compile:


What, you're not going to tell us the error messages?

I don't know any C#. At all. That said, I'll take a stab.

targetchar = Convert.ToChar(Console.ReadLine());

Now, if I had to guess, Console.ReadLine() would return a string. However, you're attempting to convert it to a character? Seems like that wouldn't work for obvious reasons.

The other thing that irks me is:

if (bob = targetchar)
Is that normal for C#? Shouldn't it be "if (bob == targetchar)" ?

twoodcc
Feb 21, 2007, 03:58 PM
What, you're not going to tell us the error messages?

I don't know any C#. At all. That said, I'll take a stab.

targetchar = Convert.ToChar(Console.ReadLine());

Now, if I had to guess, Console.ReadLine() would return a string. However, you're attempting to convert it to a character? Seems like that wouldn't work for obvious reasons.

The other thing that irks me is:

if (bob = targetchar)
Is that normal for C#? Shouldn't it be "if (bob == targetchar)" ?

thanks. here are the error messages:

cannot implicitly convert type string to char
operator != cannot be applied to operands of type char and null

you were right about the last error also, and i changed it.

but this isn't supposed to be a very complex program (shouldn't have to use arrays and stuff, or i don't think)

this should be easy, just read in the file, look for the characters, count for everyone you find, and bame display and your done.

therevolution
Feb 21, 2007, 04:14 PM
thanks. here are the error messages:

cannot implicitly convert type string to char
That would be the string to char thing I talked about....

operator != cannot be applied to operands of type char and null
Ah. Okay, so chars aren't first class objects, they're primitives. That means, among other things, you can't test them for null this way ('bob != null').

It looks like you need to start by changing 'bob' from a char to a string. Actually, you need to rename 'bob' to something more descriptive first; that's a really horrible variable name. :p


you were right about the last error also, and i changed it.

but this isn't supposed to be a very complex program (shouldn't have to use arrays and stuff, or i don't think)

this should be easy, just read in the file, look for the characters, count for everyone you find, and bame display and your done.
It's going to be hard for me to help you seeing as how I have no experience with C# and its various APIs. Maybe someone else can help out here.

It looks like you're most of the way there. You need to find a way to read in each line of the file and, while the current line is not null, count how many of the desired char is in that line. You will likely need a loop just for that counting task (loop through each char in the string). You could also try looking at the string API and see if there are any easy ways to get the char count.

twoodcc
Feb 21, 2007, 06:14 PM
That would be the string to char thing I talked about....


Ah. Okay, so chars aren't first class objects, they're primitives. That means, among other things, you can't test them for null this way ('bob != null').

It looks like you need to start by changing 'bob' from a char to a string. Actually, you need to rename 'bob' to something more descriptive first; that's a really horrible variable name. :p


It's going to be hard for me to help you seeing as how I have no experience with C# and its various APIs. Maybe someone else can help out here.

It looks like you're most of the way there. You need to find a way to read in each line of the file and, while the current line is not null, count how many of the desired char is in that line. You will likely need a loop just for that counting task (loop through each char in the string). You could also try looking at the string API and see if there are any easy ways to get the char count.

thanks.

if i change both 'bob' and 'targetchar' to stings, then i still get this error:

cannot implicitly convert type 'int' to 'string'

and it's talking about this line in the while loop:

bob = inputFile.Read();

but i have this same line earlier in the code, and it doesn't get an error?

therevolution
Feb 21, 2007, 09:40 PM
Okay. I looked it up. Console.Read() gives you back one character (in the form of an int). Console.ReadLine() gives you back a string. Those are different types, so you need to watch that. It seems you're not clear on the difference between a char and a string, so do some research on that.

As I said, you're most of the way there. You're getting the filename and desired char, opening the filestream, etc. Now it's a matter of doing these steps correctly (you've got some partial code for this, but it needs refinement):

- Get the first line in the file.
- If the line is not empty:
- Loop through each char in the line.
- If the current char is equal to the desired char, then count++
- Get the next line
(repeat this as many times as necessary)

So it might look something like this:

string bob = inputFile.ReadLine();
while (bob != null) {
// look for the desired chars in bob - leaving this to you
bob = inputFile.ReadLine();
}
and that will cause that while loop to execute for each line in inputFile. See why? Any questions on the logic?

twoodcc
Feb 21, 2007, 09:55 PM
Okay. I looked it up. Console.Read() gives you back one character (in the form of an int). Console.ReadLine() gives you back a string. Those are different types, so you need to watch that. It seems you're not clear on the difference between a char and a string, so do some research on that.

As I said, you're most of the way there. You're getting the filename and desired char, opening the filestream, etc. Now it's a matter of doing these steps correctly (you've got some partial code for this, but it needs refinement):

- Get the first line in the file.
- If the line is not empty:
- Loop through each char in the line.
- If the current char is equal to the desired char, then count++
- Get the next line
(repeat this as many times as necessary)

So it might look something like this:

string bob = inputFile.ReadLine();
while (bob != null) {
// look for the desired chars in bob - leaving this to you
bob = inputFile.ReadLine();
}
and that will cause that while loop to execute for each line in inputFile. See why? Any questions on the logic?

thank you very much for helping. i'm almost getting there. but here is my main problem :

i can't get my program to find the file. when i get it to compile, it can't find the file. i'm not sure if it's my compiler (xcode) or what. i might have to try it on a windows machine if i can't get it to work.

as far as the rest, i'm still not quite there either. i see what you are saying, and i understand the logic. but i need more variables i think. so i added some more, and i changed bob. now i have a theme of names instead (smallville)

anyway, i'm not done, but i'll go ahead and post what i have right now. thanks again for the help

using System;
using System.IO;

namespace WoodT_exercise5
{
class WoodT_exercise5
{
static void Main()
{
string fileName;
char lana;
string clark;
string chloe;
char lois;
int count = 0;

Console.WriteLine("Enter the filename: ");
fileName = Console.ReadLine();

StreamReader inputFile = new StreamReader(fileName);

lois = (char)inputFile.Read();
chloe = Convert.ToString(lois);

Console.WriteLine("Please enter a character: ");
clark = Console.ReadLine();

lana = Convert.ToChar(clark);

while (chloe != null)
{
if (lois == lana)
{
count++;
}

lois = (char)inputFile.Read();
chloe = Convert.ToString(lois);
}
inputFile.Close();
}
}
}

twoodcc
Feb 21, 2007, 11:30 PM
ok, you're not going to believe this, but i think i got it! please look over it and see if it works for you if you can. here is the code:

using System;
using System.IO;

namespace WoodT_exercise5
{
class WoodT_exercise5
{
static void Main()
{
string fileName;
char lana;
string clark;
string chloe;
char lois;
int count = 0;

Console.WriteLine("Enter the filename: ");
fileName = Console.ReadLine();

StreamReader inputFile = new StreamReader(fileName);

lois = (char)inputFile.Read();
chloe = Convert.ToString(lois);

Console.WriteLine("Please enter a character: ");
clark = Console.ReadLine();

lana = Convert.ToChar(clark);
chloe = inputFile.ReadLine();

while (chloe != null)
{
CharEnumerator x = chloe.GetEnumerator();
while (x.MoveNext() == true)
{
lois = x.Current;
//Console.WriteLine(x.Current);
if (lana == lois)
{
count++;
}
}

chloe = inputFile.ReadLine();
}
inputFile.Close();
Console.WriteLine(count);
}
}
}

twoodcc
Feb 22, 2007, 12:05 AM
well i thought it worked, but it doesn't. i don't know what it's counting.

please let me know if you find or think of anything

thanks

therevolution
Feb 22, 2007, 12:10 AM
Here's a hint: What happened to clark?





And please, PLEASE, call your variables what they are and don't give them a cutesy naming scheme. That's fine for computers on a network, but if I were grading your assignment, I'd give you a D-. Then I'd punch you. Seriously, its for your own good. "Is lana equal to lois" will not help you figure this out any quicker.

twoodcc
Feb 22, 2007, 12:16 AM
Here's a hint: What happened to clark?

And please, PLEASE, call your variables what they are and don't give them a cutesy naming scheme. That's fine for computers on a network, but if I were grading your assignment, I'd give you a D-. Then I'd punch you. Seriously, its for your own good. "Is lana equal to lois" will not help you figure this out any quicker.

well i've changed it since then, but i still don't get it. as for the naming, my teacher actually used "SpongeBob" in class. mainly b/c it really did not matter what you called it. we made the class "SpongeBob".

but i can still change the names if it makes you happy. here is what i have so far, but even if the file has one letter 'm', it will still count 12 a's somehow.

using System;
using System.IO;

namespace WoodT_exercise5
{
class WoodT_exercise5
{
static void Main()
{
string fileName;
char clark;
string chloe;
char lois;
int count = 0;

Console.WriteLine("Enter the filename: ");
fileName = Console.ReadLine();

StreamReader inputFile = new StreamReader(fileName);

Console.WriteLine("Please enter a character: ");
clark = Convert.ToChar(Console.ReadLine());


chloe = inputFile.ReadLine();


while (chloe != null)
{


CharEnumerator x = chloe.GetEnumerator();
while (x.MoveNext() != false)
{

lois = x.Current;

if (clark == lois)
{
count++;
}
}

chloe = inputFile.ReadLine();
}
inputFile.Close();
Console.WriteLine(count);
}
}
}

therevolution
Feb 22, 2007, 12:33 AM
Well, look: it doesn't affect me if you name your variables that way. The program is short enough that I can still see what's going on. It's just that it serves no purpose to name them that way, and when you're looking over your code looking for bugs, it gets in the way. Being cavalier with your variable/method/class names is a really bad habit to develop.

Here's something you can try doing for now:


while (x.MoveNext() != false)
{
lois = x.Current;

Console.WriteLine("Current letter: " + lois);

if (clark == lois)
{
count++;
}
}


Maybe the output will give you something to go on.

twoodcc
Feb 22, 2007, 08:01 AM
Well, look: it doesn't affect me if you name your variables that way. The program is short enough that I can still see what's going on. It's just that it serves no purpose to name them that way, and when you're looking over your code looking for bugs, it gets in the way. Being cavalier with your variable/method/class names is a really bad habit to develop.

Here's something you can try doing for now:


while (x.MoveNext() != false)
{
lois = x.Current;

Console.WriteLine("Current letter: " + lois);

if (clark == lois)
{
count++;
}
}


Maybe the output will give you something to go on.

i've thought about it, and you are right, and i'll change the names. but after running what you just said, i don't know what this thing is reading. i'm even more confused.

i don't see where these letters are coming from, do you?

twoodcc
Feb 22, 2007, 09:16 AM
wait a minute, i think it does work. i created a new text file, and it seems to work. but i'll still change the variale names though. here is what i got:

using System;
using System.IO;

namespace WoodT_exercise5
{
class WoodT_exercise5
{
static void Main()
{
string fileName;
char inputChar;
string line;
char fileChar;
int count = 0;

Console.WriteLine("Enter the filename: ");
fileName = Console.ReadLine();

StreamReader inputFile = new StreamReader(fileName);

Console.WriteLine("Please enter a character: ");
inputChar = Convert.ToChar(Console.ReadLine());


line = inputFile.ReadLine();


while (line != null)
{


CharEnumerator x = line.GetEnumerator();
while (x.MoveNext() != false)
{

fileChar = x.Current;
Console.WriteLine(line);
Console.WriteLine("Current letter: " + fileChar);

if (inputChar == fileChar)
{
count++;
}
}

line = inputFile.ReadLine();
}
inputFile.Close();
Console.WriteLine(count);
}
}
}

aaronbrethorst
Feb 22, 2007, 11:06 PM
wait a minute, i think it does work. i created a new text file, and it seems to work. but i'll still change the variale names though.

Yup, looks like it does. I gave it a quick once over and tightened it up. Don't submit this in place of your code, though :)


using System;
using System.IO;

namespace WoodT_exercise5
{
class WoodT_exercise5
{
public static void Main()
{
int count = 0;

Console.WriteLine("Enter the filename: ");
string fileName = Console.ReadLine();

string inputText = string.Empty;
StreamReader inputFile = null;

try
{
inputFile = new StreamReader(fileName);
inputText = inputFile.ReadToEnd();
}
catch (Exception ex)
{
Console.WriteLine("Caught an exception: {0}", ex.ToString());
return;
}
finally
{
if (null != inputFile)
inputFile.Close();
}

Console.WriteLine("Please enter a character: ");
char inputChar = Convert.ToChar(Console.ReadLine());

for (int i = 0; i < inputText.Length; i++)
{
if (inputChar == inputText[i])
count++;
}

Console.WriteLine("Count was {0}", count);
}
}
}


Also, if you have an Intel Mac and Parallels or a Windows machine sitting around, you should try out Visual C# Express (http://msdn.microsoft.com/vstudio/express/). It's free and should have everything you need. Feel free to email me if you're curious about learning more: abreth@microsoft.com

Cheers,
Aaron
Program Manager - Non-Professional Tools
Visual Studio

twoodcc
Feb 22, 2007, 11:39 PM
Yup, looks like it does. I gave it a quick once over and tightened it up. Don't submit this in place of your code, though :)


using System;
using System.IO;

namespace WoodT_exercise5
{
class WoodT_exercise5
{
public static void Main()
{
int count = 0;

Console.WriteLine("Enter the filename: ");
string fileName = Console.ReadLine();

string inputText = string.Empty;
StreamReader inputFile = null;

try
{
inputFile = new StreamReader(fileName);
inputText = inputFile.ReadToEnd();
}
catch (Exception ex)
{
Console.WriteLine("Caught an exception: {0}", ex.ToString());
return;
}
finally
{
if (null != inputFile)
inputFile.Close();
}

Console.WriteLine("Please enter a character: ");
char inputChar = Convert.ToChar(Console.ReadLine());

for (int i = 0; i < inputText.Length; i++)
{
if (inputChar == inputText[i])
count++;
}

Console.WriteLine("Count was {0}", count);
}
}
}


Also, if you have an Intel Mac and Parallels or a Windows machine sitting around, you should try out Visual C# Express (http://msdn.microsoft.com/vstudio/express/). It's free and should have everything you need. Feel free to email me if you're curious about learning more: abreth@microsoft.com

Cheers,
Aaron
Program Manager - Non-Professional Tools
Visual Studio

hey, thanks a lot! yeah, yours is better, but i won't turn it in.

i do have an Intel Mac, but i just haven't messed around with installing windows or anything yet.

thanks, i'll email you if i get into trouble with more complex programs.

therevolution
Feb 23, 2007, 12:30 AM
thanks, i'll email you if i get into trouble with more complex programs.
I think he was offering help with the Microsoft tools, not with programming questions. :p

twoodcc
Feb 23, 2007, 11:06 AM
I think he was offering help with the Microsoft tools, not with programming questions. :p

well you're probably right. did you try the last bit of code i posted? just to double check. i'm about to turn this in (and yes i added in-line comments)

dcr
Feb 23, 2007, 11:33 AM
http://www2.cis.gsu.edu/cis/program/syllabus/undergraduate/CIS3210.asp

twoodcc
Feb 23, 2007, 11:43 AM
http://www2.cis.gsu.edu/cis/program/syllabus/undergraduate/CIS3210.asp

haha. but that is for Visual Basic. and i don't see anything wrong with what i'm doing here. i've done basically the same thing on WebCT, but just between my classmates and professors

therevolution
Feb 23, 2007, 01:08 PM
well you're probably right. did you try the last bit of code i posted? just to double check. i'm about to turn this in (and yes i added in-line comments)

No, I haven't. As I said before, I'm not a C# developer, so I'm not set up to compile C#, nor do I really want to go to the trouble just for this thread. The code looks pretty much okay to me. And hey, you've got a thumbs up from a Microsoftie, so what do you need me for? :p

twoodcc
Feb 23, 2007, 01:18 PM
No, I haven't. As I said before, I'm not a C# developer, so I'm not set up to compile C#, nor do I really want to go to the trouble just for this thread. The code looks pretty much okay to me. And hey, you've got a thumbs up from a Microsoftie, so what do you need me for? :p

oh, i thought you were able to compile it earlier.

but you think it looks ok?

yeah i did, but it never hurts to get a second opinion :)

therevolution
Feb 23, 2007, 02:03 PM
oh, i thought you were able to compile it earlier.
Nope, unless you count my brain as a compiler.


but you think it looks ok?

Did you take out those Console.WriteLine() statements that I had you add? You don't need them anymore since they were just for debugging. Otherwise, yeah, it looks okay. There is certainly room for improvement -- aaronbrethorst's version is pretty good -- but considering where you are in your education, it's fine. As long as it works. :)

Legolamb
Feb 23, 2007, 04:43 PM
I'm 99.999% sure that Xcode and C# aren't friends with each other.
But bridges can be built between enemies ;)
From http://developer.apple.com/cocoa/pyobjc.html
"...PyObjC is just one of many Cocoa bridges. Apple has offered its Java bridge since Mac OS X 10.0, and Mac OS X Tiger 10.4 rolled out a JavaScript bridge which allows Dashboard Widgets to communicate with Objective-C objects. Third-party developers have also created bridges for C#, Lisp, Perl, Ruby and Smalltalk to name a few. And, of course, Python.

PyObjC's scope ranges all the way from a simple imperative scripting environment for dynamically interacting with Cocoa objects, all the way to building fully-fledged custom applications. It supports the latest technologies, such as Key-Value Observing, Core Data and Xgrid. PyObjC's maturity is unmatched—it's been around longer than even Apple's Java bridge (it originated on NeXTstep). Finally, while PyObjC is a third-party bridge, it is well known internally at at Apple, and boasts an exceptionally talented group of enthusiastic developers."

aaronbrethorst
Feb 23, 2007, 09:36 PM
I think he was offering help with the Microsoft tools, not with programming questions. :p

Nah, I was just offering help in general :)

Aaron