1

How do I add a regular expression which will accept decimal places (5.23) but nothing else in a break like so? i.e handle only numbers and decimal places which will throw an error if anything other than this is typed or returned:

    case 1:
    double[] myArrai1 = new double[3];
    Console.WriteLine("Insert a number");
    myArrai1[0] = double.TryParse(Console.ReadLine()); // no overload method?
     Console.WriteLine("Insert a number");
     myArrai1[1] = double.Parse(Console.ReadLine());
    Console.WriteLine("Insert a number");
    myArrai1[2] = double.Parse(Console.ReadLine());
    break;

Cheers guys.

P.s not sure on how to programme it in with the break also has to be without exception.

5 Answers 5

7

Regex is a little heavy for validating a double. Use double.TryParse instead (it'll return false if the input is invalid).

double dbl;
if (!double.TryParse(Console.ReadLine(), out dbl))
    Console.WriteLine("Invalid input");
Sign up to request clarification or add additional context in comments.

3 Comments

Badcock this throws an exception I got confused earlyier I need to handle the error, soon the user hits return an error is displayed in the console saying they have entered a wrong character only numbers allowed. Sorry Im quite tired and its been a heavy day.
Yeah this method doesnt work for some reason? If I encapsulate it just in the first insert I get an invalid input straight away if it put it at the bottom and hit a letter and return i still get an exception?
A guess: Potentially CultureInfo would be valuable to pass as well, and maybe just debugging it and seeing what value is being passed in would help. In the end your question here has been answered.
3

You don't need a Regex for this.

You can simply use decimal.Parse or double.Parse - if the input string is in the wrong format, you will get a FormatException.

The code you posted appears to be right - what's not working?

13 Comments

decimal.TryParse is an alternative for verifying that it can be parsed without needing to use exceptions.
@Servy - True, but it is not clear to me from the question whether exceptions are wanted or not.
@MattBurland - No, it is different. It depends on the wanted behaviour. If you want an exception, TryParse is not better.
@Oded Which is why I mentioned an alternative, rather than a correction. Let the OP decide which is appropriate.
@Garrith - M.Babcock added an example in his answer.
|
2

try this :

/^\d+(\.\d{1,2})?$/;

   Regex regex = new Regex ("^\d+(\.\d{1,2})?$");
    MatchCollection matches = regex.Matches(text);
     if (matches.Count>0)......

1 Comment

+1 for regular expression, also OP probably should not use one to start with.
2

You will probably be better off just using .NET's double parsing instead of trying to re-invent it in Regex. You can use Double.TryParse to test the string and do the conversion if the number can be parsed:

 Console.WriteLine("Insert a number");
 string input = Console.ReadLine();
 double value;
 if (!Double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out value)) 
    throw new InvalidOperationException(String.Format("{0} could not be parsed as a valid number.", input));

3 Comments

thats abit hefty wouldnt i have to do this for each insertion?
If you're going to throw an exception anyway then why bother with the TryParse?
@Garrith That's what methods are for, so that you don't need to repeat the same code over and over again. Make a method GetDoubleFromUser() that returns a double and contains code more or less like this (but with a return).
1

Did you want to just validate what the user inputs, or stop them from putting in invalid characters in the first place?

In the first case, regex aren't really needed. You are on the right track with double.Parse, but what you want to do is double.TryParse. First read the user input into a string, then TryParse it into a double. If TryParse returns false, tell you user their input is invalid and prompt them to enter it again.

FWIW: here's how I'd do it (warning: untested code), which may or may not be what you need:

case 1:
double[] myArrai1 = new double[3];
for (int i=0; i < myArrai1.length; i++) {
    Console.WriteLine("Insert a number");
    while (!double.TryParse(Console.Readline(), out myArrai1[i])) {
        Console.WriteLine("Invalid entry. Please enter a number.");
    }
}
break;

3 Comments

myArrai1[0] = double.TryParse(Console.ReadLine()); gives no overload method for try parse?
@Garrith You can google double.TryParse to see the signature for the method and examples of its use.
Read the MSDN documentation. You need to pass a double as an out parameter. driis' answer is almost correct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.