Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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:

Code:
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

macrumors 6502
May 12, 2003
468
0
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.

Code:
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:

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

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

Code:
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:

Code:
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

macrumors 6502
May 12, 2003
468
0
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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

macrumors 6502
May 12, 2003
468
0
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):
Code:
- 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:
Code:
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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):
Code:
- 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:
Code:
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

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);
			
			while (chloe != null)
			{
				if (lois == lana)
				{
					count++;
				}
				
				lois = (char)inputFile.Read();
				chloe = Convert.ToString(lois);
			}
			inputFile.Close();
		}
	}
}
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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:

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);
		}
	}
}
 

therevolution

macrumors 6502
May 12, 2003
468
0
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

Code:
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

macrumors 6502
May 12, 2003
468
0
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:

Code:
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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:

Code:
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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:

Code:
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

macrumors member
Feb 1, 2007
30
0
Seattle, WA
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 :)

Code:
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. 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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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 :)

Code:
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. 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

macrumors 6502
May 12, 2003
468
0
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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

macrumors 6502
May 12, 2003
468
0
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. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.