Coding Academy - Unexpected Error Msg.

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Coding Academy - Unexpected Error Msg.

Postby chriswadams » Wed Jun 23, 2010 9:05 pm

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
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am

Postby Ram » Thu Jun 24, 2010 12:17 am

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: Select all
static string lines;


However, before you paste your code, can I ask the logic on this snippet of code.

Code: Select all
{
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
Code: Select all
./wordscramble


To launch with 1 argument do this
Code: Select all
./wordscramble mywordlist   

mywordlist would then be stored in (args[0])

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

Re: Coding Academy - Unexpected Error Msg.

Postby chriswadams » Thu Jun 24, 2010 11:30 pm

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 < 8) {
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
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am

Postby Ram » Fri Jun 25, 2010 7:39 am

Hi Chris,

Your placement of a { was in the wrong place.

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

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

Re: Coding Academy - Unexpected Error Msg.

Postby chriswadams » Tue Jun 29, 2010 11:30 pm

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:

So to launch the program without any arguments do this
Code:
Code: Select all
./wordscramble


To launch with 1 argument do this
Code:
Code: Select all
./wordscramble mywordlist   


mywordlist would then be stored in (args[0])


So I changed my code from this:

Code: Select all
{
if (args.Length > 12){
string[] lines = File.ReadAllLines(args[1]);
}


to this:

Code: Select all
{
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:

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.
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am

Postby Ram » Wed Jun 30, 2010 1:05 am

That because you have not told it what to do with with with the supplied file (args.Length > 0)

It reads

Code: Select all
{
if (args.Length > 0){
string[] lines = File.ReadAllLines(args[0]);
}

Then skips to
Code: Select all
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 :)

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

Re: Coding Academy - Unexpected Error Msg.

Postby chriswadams » Thu Jul 01, 2010 10:11 pm

Aha! :D What was missing was this code block:
Code: Select all
            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: Select all
      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
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am

Postby Ram » Fri Jul 02, 2010 6:49 pm

Ready for project 3 now :)

Good luck.

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


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests