2

I am trying to make a command line program that will ask if fast and long you want it to beep. I keep getting System.FormatException on the code below. I get the problem right after Console.WriteLine("how many times should i beep?");. I've found a fix by putting a console.read();//pause right after this line.

My question is what am I doing wrong? or am I suppose to have the pause after that line?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("how fast would you like the sounds to play?");
        Console.WriteLine("70 = fast and 300 = slow can pick any number inbetween");
        string choice = Console.ReadLine();
        int speed = Convert.ToInt32(choice);
        Console.Write(speed);
        Console.Read();//pause
        Console.WriteLine("how many times should i beep?");
        string choice2 = Console.ReadLine();
        int j = Convert.ToInt32(choice2);
        Console.Write(j);
        Console.Read();//pause
        for (int i = 0 ; i < j; i++)
        {
            Console.Beep(1000, speed);
        }
    }
}

4 Answers 4

7

My psychic debugging skills tell me that it's this line throwing the exception:

int j = Convert.ToInt32(choice2);

Putting the Console.Read() in like you mentioned before that causes that line not to execute immediately and delaying the throwing of the exception.

If you input something that isn't an integer on this line:

string choice2 = Console.ReadLine();

You will get a FormatException on the following Convert.ToInt32 call.

See the documentation for Convert.ToInt32 where it tells you a FormatException is thrown when the value you pass does not consist of an optional sign followed by a sequence of digits (0 through 9).

To solve your issue, use Int32.TryParse (or just make sure you enter a valid integer). That will return a boolean indicating the success or failure of the parsing rather than throwing an exception.

Also, welcome to StackOverflow! Be sure to upvote answers you find helpful and accept the one that best resolves your question.

Sign up to request clarification or add additional context in comments.

2 Comments

It's an odd policy IMO, but the guy can't upvote answers with just 6 rep. I struggled with that for a while myself.
@KevinT Strange, I never knew that. Oh well
3

why are those extra Console.Read() in there? They are breaking your program, because the user will press return too many times

    Console.WriteLine("how fast would you like the sounds to play?");
    Console.WriteLine("70 = fast and 300 = slow can pick any number inbetween");
    string choice = Console.ReadLine();
    int speed = Convert.ToInt32(choice);
    Console.WriteLine(speed);
    Console.WriteLine("how many times should i beep?");
    string choice2 = Console.ReadLine();
    int j = Convert.ToInt32(choice2);
    Console.Write(j);
    for (int i = 0; i < j; i++)
    {
        Console.Beep(1000, speed);
    }

2 Comments

Kinda sorta. Though you're correct, the cause of the exception is bad user input... which could very well be because of having to press return so many times and likely causing them to enter an empty string for the one ReadLine that actually matters.
I tested this quickly. It's as if the return press for the Read() call gets passed right into the ReadLine() call, so it immediately tries to parse an empty string. It's kinda bizarre behavior.
0

Actually, all you need to do is remove those Read() pauses you threw in. I'd probably also change the Write() calls to WriteLine() calls.

You can also change the Read() calls to ReadLine() calls, if you insist on "pausing" there.

Comments

-1

Try this:

    Console.Read();
    string choice2 = Console.ReadLine();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.