2

I need to show some message to user when a file exist, that display the message "the files exist... do you want to overwrite it??"

if (File.Exists(binaryFilePath))
{
    Program.DisplayMessage("The file: " + binaryFileName + " exist. You want to overwrite it? Y/N");
    string overwrite = Console.ReadLine();
    while (overwrite != null)
    {
        if (overwrite.ToUpper() == "Y")
        {
        WriteBinaryFile(frameCodes, binaryFilePath);

        } if (overwrite.ToUpper() == "N")
        {
        throw new IOException();
        overwrite = null;
        } if (overwrite.ToUpper() != "Y" && overwrite.ToUpper() != "N")
        {
        Program.DisplayMessage("!!Please Select a Valid Option!!");
        overwrite = Console.ReadLine();
        }
    }
}

if the user write "Y" the process start and done normal... the problem is how can stops?? I try with this while, but not work...

How can I do this?

Raj More
  • 47,048
  • 33
  • 131
  • 198
ale
  • 3,301
  • 10
  • 40
  • 48
  • 1
    please don't revert people's edits. They are trying to improve the formatting of the code to make the question more readable and hence answerable. – ChrisF Apr 25 '11 at 22:32
  • 3
    Why do you use the while loop at all? And why do you throw an exception if the user answers no and then set overwrite to null? – mylopeda Apr 25 '11 at 22:32
  • @esrange it appears he meant to ReadLine() until a valid option was input, then break from the loop, by setting overwrite to null. @ale break; is the way to get out of the loop. – adorablepuppy Apr 25 '11 at 22:35
  • @esrange because its in a while loop, saying you can always overwrite, until they say no, he (wrongly) throws the exception then sets overwrite to null to break the while loop! thats why overwrite is set to null XD – RhysW Jan 08 '13 at 15:02
  • not to mention it constantly spams the message !!Please Select A Valid Option!! to the program until they choose yes or no.. – RhysW Jan 08 '13 at 15:03

3 Answers3

3
if (File.Exists(binaryFilePath))
{
  while (true)
  {
  Program.DisplayMessage("The file: " + binaryFileName + " already exist. Do you want to overwrite it? Y/N");
    string overwrite = Console.ReadLine();
    if (overwrite.ToUpper().Equals("Y"))
    {
      WriteBinaryFile(frameCodes, binaryFilePath);
      break;
    } 
    else if (overwrite.ToUpper().Equals("N"))
    {
      Console.WriteLine("Aborted by user.");
      break;
    } 
    else
    {
      Program.DisplayMessage("!!Please Select a Valid Option!!");
      overwrite = Console.ReadLine();
      continue; // not needed - for educational use only ;)
    }
  }
}

Try that and go learn your basics (conditions, loops, english, ...). Then you can come back and ask why throwing an exception (particularly that one) is wrong in your case ;)

craigmoliver
  • 6,499
  • 12
  • 49
  • 90
atamanroman
  • 11,607
  • 7
  • 57
  • 81
  • this application is in console, I need some using for EqualsIgnoreCase?? does not identify... – ale Apr 25 '11 at 22:51
  • 2
    This code should work fine in a console app (though ataman, I think it's `ToUpper()`, not `toUpper()`)... Converting it to uppercase handles the case differences; you don't need to consider case with this approach. Other approaches you could use for case-insensitive string comparison include: `overwrite.Equals("Y", StringComparison.CurrentCultureIgnoreCase)`, `overwrite.Equals("Y", StringComparison.InvariantCultureIgnoreCase)`, `string.Equals(overwrite, "Y", StringComparison.CurrentCultureIgnoreCase);` or simply `string.Equals(overwrite, "Y", true);` – James King Apr 26 '11 at 19:51
  • Yeah, ToUpper and Equals are both uppercase, I always mix that up when hopping between java and c#. – atamanroman Apr 26 '11 at 22:20
0

Try using break; to break out of the loop (also use if-else-if rather than if-if.. )

if (File.Exists(binaryFilePath))
{


    while (true)
    {
        Program.DisplayMessage("The file: " + binaryFileName + " exist. You  want to overwrite it? Y/N");
        string overwrite = Console.ReadLine();
        if (overwrite.ToUpper() == "Y")
        {
            WriteBinaryFile(frameCodes, binaryFilePath);
            break;

        } 
        else if (overwrite.ToUpper() == "N")
        {
            throw new IOException();
            overwrite = null;
            break;
        } 
        else if (overwrite.ToUpper() != "Y" && overwrite.ToUpper() != "N")
        {
            Program.DisplayMessage("!!Please Select a Valid Option!!");
            overwrite = Console.ReadLine();
        }
    }
}

although the break; after "N" is useless but I hope you are handling the exception that you are throwing elsewhere.

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • 2
    You really don’t need the third `if`. The elses already take care of it. You also probably don’t need any of the two lines after `throw`; they would be unreachable anyway. – Timwi Apr 25 '11 at 22:49
0

I believe, user choice reading should be delegate to another method. Like this:

static void Main(string[] args)
        {
            //...

            if (File.Exists(binaryFilePath))
            {
                if(ReadBool("The file: " + binaryFileName + " exist. You want to overwrite it? Y/N"))
                    WriteBinaryFile(frameCodes, binaryFilePath);
                else
                    throw new IOException();
            }
        }

static bool ReadBool(String question)
        {
            while (true)
            {
                Console.WriteLine(question);
                String r = (Console.ReadLine() ?? "").ToLower();
                if (r == "y")
                    return true;
                if (r == "n")
                    return false;
                Console.WriteLine("!!Please Select a Valid Option!!");
            }
        }
Tom Esterez
  • 21,567
  • 8
  • 39
  • 44