简体   繁体   中英

Getting Unhandled Exception: System.FormatException: String must be exactly one character long when a character is not contained in an array

I'm working on a hangman game that runs in the console and trying to check whether a guessed letter is part of a word that is stored in a char array. When I try to run this and input a letter that is true, no exceptions are raised; however, when I enter a letter that is not part of the word, I get a FormatException saying that the string must be only one character long. Keep in mind that I enter one character when I get this exception. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        Game game = new Game();
        if(game.CheckAnswer())
        {
            Console.WriteLine("true");
        }
        else if(!game.CheckAnswer())
        {
            Console.WriteLine("false");
        }
    }   
}

class Word
{
    public static string GetWord()
    {
        string[] words = new string[5]{"alpha", "bravo", "charlie", "delta", "echo"};
        Random random = new Random();
        return words[random.Next(5)];
    }
    public char[] correctAnswer = GetWord().ToCharArray();
}

class Game
{
    static char guessLetter;
    static List<char> correctGuesses = new List<char>();
    static List<char> incorrectGuesses = new List<char>();
    Word word = new Word();

    public bool CheckAnswer()
    {
        guessLetter = Convert.ToChar(Console.ReadLine());
        if (word.correctAnswer.Contains(guessLetter))
        {
            return true;
        }
        else if (!word.correctAnswer.Contains(guessLetter))
        {
            return false;
        }
        else
        {
            return false;
        }
    }

}

Yes, I've checked other answers, and no, they did not have the solution to my problem.

My bad didn't check your code thoroughly. When you call the method in if else cases it will run them. So you were checking the results twice.

if the letter you enter is a wrong one it will be checked in both if and else cases which calls it twice.

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        Game game = new Game();
        //dont call the method in if else save the result in a var first maybe
        Console.WriteLine(game.CheckAnswer());
    }   
}

class Word
{
    public static string GetWord()
    {
        string[] words = new string[5]{"alpha", "bravo", "charlie", "delta", "echo"};
        Random random = new Random();
        return words[random.Next(5)];
    }
    public char[] correctAnswer = GetWord().ToCharArray();
}

class Game
{
    static char guessLetter;
    static List<char> correctGuesses = new List<char>();
    static List<char> incorrectGuesses = new List<char>();
    Word word = new Word();

    public bool CheckAnswer()
    {
        guessLetter = Convert.ToChar(Console.ReadLine());

        //you can return the result directly
        return word.correctAnswer.Contains(guessLetter);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        while (true)
        {
            Game game = new Game();
            if (game.CheckAnswer())
            {
                Console.WriteLine("true");
            }
            else 
            {
                Console.WriteLine("false");
            }                
        }            
    }
}

class Word
{
    public static string GetWord()
    {
        string[] words = new string[5] { "alpha", "bravo", "charlie", "delta", "echo" };
        Random random = new Random();
        return words[random.Next(5)];
    }

    public char[] correctAnswer = GetWord().ToCharArray();
}

class Game
{
    static char guessLetter;
    static List<char> correctGuesses = new List<char>();
    static List<char> incorrectGuesses = new List<char>();
    Word word = new Word();

    public bool CheckAnswer()
    {
        guessLetter = Console.ReadKey().KeyChar;

        if (word.correctAnswer.Contains(guessLetter))
        {
            Console.WriteLine();
            return true;
        }
        Console.WriteLine();
        return false;
    }    
}

Your problem is likely in your Main function. If the input character isn't in the word you actually end up asking for input from the console a second time.

Try changing it to:

static void Main(string[] args)
{
    Game game = new Game();

    bool answer = game.CheckAnswer(); // now you are only asking for input once

    if(answer) Console.WriteLine("true");
    else Console.WriteLine("false");
}   

and see what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM