| View previous topic :: View next topic |
| Author |
Message |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Wed Jun 23, 2010 10:05 pm Post subject: Coding Academy - Unexpected Error Msg. |
|
|
Hi, all--
I'm working on Project 2 of the Coding Academy (WordScramble), and I got an unexpected error message while doing Homework #3. The offending code is:
static string lines;
public static void Main (string[] args)
{
if (args.Length > 12){
string[] lines = File.ReadAllLines(args[1]);
}
else{
string[] lines = File.ReadAllLines("wordlist");
}
foreach(string word in lines){
and the error message for the last line is this:
Cannot convert type `char' to `string' (CS0030)
This problem didn't crop up until I began the 3rd homework assignment (ie. the code worked fine up to that point), so I assume I did something wrong there, but I can't figure out what it was. The variable 'word' is declared to be a string in the code I pasted above, and the variable 'lines' is an array, so why the compiler thinks they are 'char' and 'string' types, I have no idea. Any suggestions?
I can paste in the code for the rest of the program if that will be of any help.
Thanks,
chriswadams |
|
| Back to top |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1550 Location: Guisborough
|
Posted: Thu Jun 24, 2010 1:17 am Post subject: |
|
|
Think I need to see the rest of your code.. I can't replicate the error using your code.
From your snippet, I don't see the need for this line.
| Code: | | static string lines; |
However, before you paste your code, can I ask the logic on this snippet of code.
| Code: | {
if (args.Length > 12){
string[] lines = File.ReadAllLines(args[1]);
} |
As I see it, you would need to launch the program with 14 arguments to read the second argument (args[1]). As in the above snippet.
This might help
Don't forget that counting begins at zero.
So to launch the program without any arguments do this
To launch with 1 argument do this
| Code: | | ./wordscramble mywordlist |
mywordlist would then be stored in (args[0]) _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Fri Jun 25, 2010 12:30 am Post subject: Re: Coding Academy - Unexpected Error Msg. |
|
|
Hi Ram--
This line:
[code]static string lines;[/code]
was me declaring the string 'lines' prior to the main body of the program. If I don't put it in there I get this error message:
[code]"The name `lines' does not exist in the current context (CS0103)"[/code]
in reference to the last line of code:
[code]foreach(string word in lines){ [/code]
You asked about this snippet:
[code]{
if (args.Length > 12){
string[] lines = File.ReadAllLines(args[1]);
}[/code]
Well, I assumed that the argument "./wordscramble" would be 12 letters (not counting the ./). I also assumed that "args[1]" would refer to the second [i]element[/i] of the array "args" ie. the file name entered by the user. (Yeah, I know what happens when I assume!)
You also wrote:
[quote]mywordlist would then be stored in (args[0])[/quote]
So, the command "./wordscramble" isn't considered part of the "args" array? OK, I'll work with that, and see what happens.
Anyway, here is the full "WordScramble" code (note that I have a comment with the error message next to the line that MonoDevelop doesn't like):
[code]
using System; //created by Chris Adams 6/16/10
using System.Collections.Generic;
using System.IO;
namespace WordScramble
{
class MainClass
{
static List<string> WordList = new List<string>();
static List<string> UsedList = new List<string>();
static Random Rand = new Random();
static string PromptLetters;
static string lines;
public static void Main (string[] args)
{
if (args.Length > 0){
string[] lines = File.ReadAllLines(args[1]);
}
else{
string[] lines = File.ReadAllLines("wordlist");
}
foreach(string word in lines){ //Error Msg. -> Cannot convert type `char' to `string' (CS0030)
if (word.Length < 3) continue;
if (word.Contains("'")) continue;
WordList.Add(word.ToLower());
}
Console.WriteLine("");
Console.WriteLine("Welcome to WordScramble!");
Console.WriteLine("Type !quit to exit, !letters for a reminder, or !next for a new word.");
Console.WriteLine("");
var score = 0;
GetLetters(); //goes to GetLetters subroutine
Console.WriteLine(PromptLetters);
bool running = true;
while (running){
string input = Console.ReadLine(); //get player input
switch (input){ //look for !quit, !next, !letters commands
case "!quit":
running = false;
break;
case "!next":
GetLetters();
Console.WriteLine(PromptLetters);
Console.WriteLine("Score = " + score);
break;
case "!letters":
Console.WriteLine(PromptLetters);
break;
default: //check to be sure word exists, & hasn't been used already
if (!UsedList.Contains(input)){
if (WordIsPossible(input)){
if (WordList.Contains(input)){
UsedList.Add(input);
var WordScore = WordValue(input);
score = score + WordScore;
Console.WriteLine("Good! Score = " + score);
} else {
Console.WriteLine("That word doesn't exist!");
}
} else {
Console.WriteLine("Did you forget your letters, or something?");
Console.WriteLine("Reminder: " + PromptLetters);
}
} else {
Console.WriteLine("You've used that word already!");
}
break;
}
}
}
static void GetLetters(){ //GetLetters subroutine
PromptLetters="";
UsedList.Clear();
string word = "";
while (word.Length < {
word = WordList[ Rand.Next(WordList.Count) ];
}
foreach (char letter in word){
PromptLetters = PromptLetters + letter + " ";
}
}
static bool WordIsPossible (string word){
string letters = PromptLetters;
foreach (char letter in word){
int pos = letters.IndexOf(letter);
if (pos != -1){
letters = letters.Remove(pos, 1);
}else{
return false;
}
}
return true;
}
static int WordValue(string input) {
var wvalue = 0;
foreach (char letter in input){
wvalue++;
}
return wvalue;
}
}
}
[/code]
Thanks for your time, Ram.
chriswadams |
|
| Back to top |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1550 Location: Guisborough
|
Posted: Fri Jun 25, 2010 8:39 am Post subject: |
|
|
Hi Chris,
Your placement of a { was in the wrong place.
| Code: |
class MainClass
{
static List<string> WordList = new List<string>();
static List<string> UsedList = new List<string>();
static Random Rand = new Random();
static string PromptLetters;
//static string lines; Not needed
public static void Main (string[] args)
{
if (args.Length > 0){
string[] lines = File.ReadAllLines(args[1]);
} else {
string[] lines = File.ReadAllLines("wordlist");
// } Brace is in the wrong place - it closes the else code block giving the lines error when called in the following foreach code block so need to go after it.
foreach(string word in lines){
if (word.Length < 3) continue;
if (word.Contains("'")) continue;
WordList.Add(word.ToLower());
}
} The brace goes here |
_________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Wed Jun 30, 2010 12:30 am Post subject: Re: Coding Academy - Unexpected Error Msg. |
|
|
Ram -
You were right about the placement of the { . I moved it to the location you suggested and the error message disappeared. Thanks!
However, I still have a question about launching a program with arguments.
You wrote:
| Quote: | So to launch the program without any arguments do this
Code:
To launch with 1 argument do this
Code:
| Code: | | ./wordscramble mywordlist |
mywordlist would then be stored in (args[0]) |
So I changed my code from this:
| Code: | {
if (args.Length > 12){
string[] lines = File.ReadAllLines(args[1]);
}
|
to this:
| Code: | {
if (args.Length > 0){
string[] lines = File.ReadAllLines(args[0]);
}
|
but when I tried to launch the program with a single argument, I got this error message:
| Quote: |
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range.
at WordScramble.MainClass.Main (System.String[] args) [0x00000]
|
If the 1st argument is held in "args[0]" then why is the index out of range?
Thanks again for your time,
chriswadams. |
|
| Back to top |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1550 Location: Guisborough
|
Posted: Wed Jun 30, 2010 2:05 am Post subject: |
|
|
That because you have not told it what to do with with with the supplied file (args.Length > 0)
It reads
| Code: | {
if (args.Length > 0){
string[] lines = File.ReadAllLines(args[0]);
}
|
Then skips to
| Code: | Console.WriteLine("");
Console.WriteLine("Welcome to WordScramble!");
Console.WriteLine("Type !quit to exit, !letters for a reminder, or !next for a new word.");
Console.WriteLine("");
GetLetters();
Console.WriteLine(PromptLetters);
|
And the error is coming from GetLetters() as the word array is empty.
I'll let you figure out what's missing  _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Thu Jul 01, 2010 11:11 pm Post subject: Re: Coding Academy - Unexpected Error Msg. |
|
|
Aha! What was missing was this code block:
| Code: | foreach(string word in lines){
if (word.Length < 3) continue;
if (word.Contains("'")) continue;
WordList.Add(word.ToLower()); |
The way I had it, if I started the program with one argument, the program skipped right over the above code. By copying and pasting it like so:
| Code: | if (args.Length > 0){
string[] lines = File.ReadAllLines(args[0]);
foreach(string word in lines){
if (word.Length < 3) continue;
if (word.Contains("'")) continue;
WordList.Add(word.ToLower());
}
} |
the error message disappears, and the program accepts arguments like it should. Eureka!
Thanks again for your time, Ram!
chriswadams |
|
| Back to top |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1550 Location: Guisborough
|
Posted: Fri Jul 02, 2010 7:49 pm Post subject: |
|
|
Ready for project 3 now
Good luck. _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|