Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,572 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,930 people online right now. Registration is fast and FREE... Join Now!




error reading a file

 
Reply to this topicStart new topic

error reading a file

annebar76
4 Apr, 2008 - 11:26 PM
Post #1

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 8


My Contributions
Hello,

I have this pesky homework assignment to read and manipulate a .txt file using three parallel arrays (the attached file is the file in question). It builds with no errors (i'm working in VB 2005), but when I run the program, it gives me the following error message:

Input string not in a correct format

************** Exception Text **************
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Decimal.Parse(String s, IFormatProvider provider)
at System.Convert.ToDecimal(String value)
at ch9._6.frmDJPlayList.frmDJPlayList_Load(Object sender, EventArgs e) in d:\ch9.6\ch9.6\Form1.vb:line 40
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


Here's my code thus far:

CODE

Public Class frmDJPlayList

    Public Shared _intSizeOfArray As Integer = 52
    Public Shared _strTitleOfSong(_intSizeOfArray) As String
    Public Shared _strGenreOfSong(_intSizeOfArray) As String
    Public Shared _decLengthOfSong(_intSizeOfArray) As Decimal


    Private Sub frmDJPlayList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' The frmDJPlayList load event reads the songs.txt file and fills
        ' the ComboBox object with genre types.

        ' Initialize an instance of the StreamReader object
        ' and declare variables
        Dim objReader As IO.StreamReader
        Dim strLocationAndNameOfFile As String = "c:\songs.txt"
        Dim intCount As Integer = 0
        Dim strFileErrorMessage As String = _
            "The file is not available. Restart when file is available."

        ' Verify the file exists
        If IO.File.Exists(strLocationAndNameOfFile) Then
            objReader = IO.File.OpenText(strLocationAndNameOfFile)

            ' Read file line by line until completed
            Do While objReader.Peek <> -1
                _strTitleOfSong(intCount) = objReader.ReadLine()
                _strGenreOfSong(intCount) = objReader.ReadLine()
                _decLengthOfSong(intCount) = Convert.ToDecimal(objReader.ReadLine())
                intCount += 1
            Loop

            objReader.Close()
        Else
            MessageBox.Show(strFileErrorMessage, "Error")
            Me.Close()
        End If

    End Sub



Please help!






Attached File(s)
Attached File  songs.txt ( 1.92k ) Number of downloads: 22
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Error Reading A File
4 Apr, 2008 - 11:31 PM
Post #2

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Moved to VB.NET Forums.
User is offlineProfile CardPM
+Quote Post

Ariana Lightrain
RE: Error Reading A File
5 Apr, 2008 - 12:17 AM
Post #3

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 11


My Contributions
Your error is at: _decLengthOfSong(intCount) = Convert.ToDecimal(objReader.ReadLine())

without testing it myself, I would:
CODE
        Dim temp As String = String.Empty
        Try
            temp = objReader.ReadLine()
            _decLengthOfSong(intCount) = Convert.ToDecimal(temp)
        Catch ex As Exception 'General catch, instead of specific "FormatException"
            'Look at the string "temp" to see if newlines are breaking the code
            Windows.Forms.MessageBox.Show(ex.Message & Environment.NewLine & "---" & temp & "---")
        End Try


This post has been edited by PsychoCoder: 5 Apr, 2008 - 04:28 AM
User is offlineProfile CardPM
+Quote Post

annebar76
RE: Error Reading A File
5 Apr, 2008 - 02:10 PM
Post #4

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 8


My Contributions
Thanks! That definitely helped. I've fixed the problem smile.gif
User is offlineProfile CardPM
+Quote Post

Ariana Lightrain
RE: Error Reading A File
5 Apr, 2008 - 03:15 PM
Post #5

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 11


My Contributions
Your welcome. Try..Catch is quite helpful for finding why an error occures
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:58PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month