What's new

.NET VB.NET Console Application Text Validation Help!

  • Thread starter Legless
  • Start date
  • Views 1,238
L

Legless

Be spontaneous, be kind, be open-minded.
Messages
4,375
Reaction score
2,536
Points
665
Sin$
0
I am working on this huge project in college that is an ATM machine and well I don't know how to validate text. I have, for example, a user input that is read to the console and it's an integer but if a user enters a string then the console application breaks. How can I get an error message to come up to say that the string is invalid without the project crashing?

I know Console.WriteLine("Please enter only numeric characters!") for example, as the error message itself but I don't know anything else to do with text validation.

Here, below, is a snippet of my code; it's a simple menu system but the problem lies in here also:
Code:
        Console.WriteLine(CentralAlignment & "  Main Menu")
        Console.WriteLine(CentralAlignment & "  =========")
        Console.WriteLine()
        Console.WriteLine(CentralAlignment & "1. Account Details")
        Console.WriteLine()
        Console.WriteLine(CentralAlignment & "2. Withdraw")
        Console.WriteLine()
        Console.WriteLine(CentralAlignment & "3. Transaction Log")
        Console.WriteLine()
        Console.WriteLine(CentralAlignment & "4. Current Balance")
        Console.WriteLine()
        Console.WriteLine(CentralAlignment & "5. Exit")
        Console.WriteLine()
        Console.Write(CentralAlignment & "Please select your choice: ")
        MenuChoice = Console.ReadLine

I'm aware that there's probably easier methods of aligning text and such but I like to use my own ways, and we haven't been taught that yet; except for VbTab etc..

Thank you in advance.
 
P

pwfdc

Member
Bright Idea Programmer Experienced Veteran
Messages
1,540
Reaction score
677
Points
465
Sin$
0
A try-catch would be the best.
Try {
Convert to int
}
Catch (error and such here) {
Write error message to console.
}


Sorry. On mobile.
 
I

ILLusiveman

Enthusiast
Messages
289
Reaction score
30
Points
85
Sin$
0
While I agree with coldfire202,
You can also try int.TryParse(string input, out int)

The thing to know about it is that if it can parse the passed variable it will assign a variable to the output but if it cannot, nothing happens to the output.

Ex.
int number = 0;
int.TryParse("", out number);

This will not assign a value to number while using "1" as the input will assign 1 to number.

My approach if I had to use this might be:
int answer = 0;
while(answer==0)
{
string MenuChoice = Console.ReadLine();
int.TryParse(MenuChoice, out answer);
if(answer == 0)
{
Console.WriteLine("Incorrect Input, Please try again: "); //prompt for choice again
}
else
{
//no need for else since they will exit while loop
}
}

Sorry if there are any mistakes here, I am doing this on the fly and from memory how some of these things work.
 
P

pwfdc

Member
Bright Idea Programmer Experienced Veteran
Messages
1,540
Reaction score
677
Points
465
Sin$
0
I don't thin that TryParse would be useful because he's needing to base other things off of it. Using catch will allow something to be said to the user with no issue.
Also. Using Try-Catch he can run the code only if it's an integer. Which is what should more than likely happen.
 
Top Bottom
Login
Register