Mud Droplet Mud Droplet - 7 months ago 36
Vb.net Question

Conversion from string "" to type 'Double' is not valid In .NET Fiddle's VB

I am writing a project in school (on .NET Fiddle's VB Console) and am encountering an error.

In this task, we have to:
Enter 10 test results with the name of the student.
Validate all data as it is entered and refuse poor data
We need to print out the Mean (arithmetic average), Highest Mark and who got it (assume 1 max per test), Lowest Mark and who got it (assume 1 min per test)

My Personal Code Follows:

Imports System

Public Module Module1
Public Sub Main()
Dim sStudentName As String
Dim iStudentMark As Double
Dim iHighMark As Integer
Dim iLowMark As Integer
Dim iAve As Integer

For List = 1 to 10

Console.WriteLine("Input A Students Name")
sStudentName = Console.ReadLine()
Console.WriteLine("This student's Mark is")
iStudentMark = Console.ReadLine()


Do Until 25<= iStudent Or iStudentMark <= 100
Console.WriteLine("Error. Invalid Percentage. Re-input.")
iStudentMark = Console.ReadLine()
Loop

iAve = iAve + iStudentMark

If iStudentMark > iHighMark Then
iHighMark = iStudentMark
Console.Write("The Highest Percentage has changed. The top score is now " + iStudentMark + " Percent from " + sStudentName)
End If

If iStudentMark < iLowMark Then
iHighMark = iStudentMark
Console.Write("The Highest Percentage has changed. The top score is now " + iStudentMark + " Percent from " + sStudentName)
End If

Next
iAve = iAve/10
Console.WriteLine(iAve)

End Sub
End Module


The Error Message(s) I get is/are:


Run-time exception (line -1): Conversion from string "The Highest
Percentage has chang" to type 'Double' is not valid.

Stack Trace:

[System.FormatException: Input string was not in a correct format.]

[System.InvalidCastException: Conversion from string "The Highest
Percentage has chang" to type 'Double' is not valid.]


I have never been the best at coding, so many thanks to those who try and solve it.

Answer

Console.ReadLine() method will return a string you cannot assign it directly to double. you need to cast them as double.

Console.WriteLine("This student's Mark is")
If Not Integer.TryParse(Console.ReadLine(), iStudentMark) Then
    Console.WriteLine("Invalid input..! failed to convert")
End If
'you can proceed with iStudentMark
Comments