how to read a number from a text file

SarahWard

Banned
I need to read a text file which holds an integer number (in this case '6').

How can I read this value into a variable?

Everything I try keeps coming up with that annoying and pointless error message "numberformatexception". :(

I have tried using a textreader in readline mode but without luck....


dim TR as TextReader
dim xsize as string
dim sdcard_folder as string: sdcard_folder = "testfolder"

TR.Initialize(File.OpenInput(File.DirRootExternal, sdcard_folder & "xsize.txt"))
xsize = TR.ReadLine
If IsNumber(xsize) = False Then 'DIDN'T READ THE VALUE
TR.Close


When running this code the value of '6', found in the text file, should be read into the string variable 'size'. But it isn't and the if-then test for a number fails. Why isn't the number read as the first line of a text file?

(I have simplified this code for ease of reading)
 
Last edited:

klaus

Expert
Licensed User
Longtime User
I'm afraid that the problem is for those who want to share files between Antdroid, B4PPC and Windows.
I had encountered the same problem as Sarah trying to read files generated with B4PPC.
That would mean that we must remove the first character from the first line when we read a B4PPC file in B4Android and we must add the 3 BOM characters when we write to a file in B4Android to make the file readable in B4PPC, if we don't want to manipulate these files when we move from one system to the other.

Best regards.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
(Basic4ppc) When you write a file you can use cASCII if UTF8 is not needed.
Yes, I will do it in one of my projects because there are only numbers in the file. Unfortunately with texts with special characters like é, ü etc. it won't work.

Best regards.
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
I think you left out the FORWARD SLASH

Sep 13 2011
02:25 Hours

Hi

The code appears to work for me. Give it a shot!

Ray Tesluk

Sub ButtonWrite_Click
Dim TW As TextWriter
Dim xsize As String: xsize = "6"
Dim sdcard_folder As String: sdcard_folder = "testfolder"
If File.IsDirectory(File.DirRootExternal,sdcard_folder) = True Then
Try
TW.Initialize(File.OpenOutput(File.DirRootExternal & "/" & sdcard_folder,"xsize.txt",False))
TW.Write(xsize)
TW.Close
Msgbox(xsize,"xsize Value Written")
Catch
Msgbox(LastException,"Write Error")
End Try
Else
Try
File.MakeDir(File.DirRootExternal,sdcard_folder)
Msgbox(sdcard_folder & " Created",File.DirRootExternal)
Catch
Msgbox(LastException,"Create Error")
End Try
End If
End Sub
Sub ButtonRead_Click
Dim TR As TextReader
Dim xsize As String: xsize = "6"
Dim sdcard_folder As String: sdcard_folder = "testfolder"

Try
TR.Initialize(File.OpenInput(File.DirRootExternal, sdcard_folder & "/" & "xsize.txt"))
xsize = TR.ReadLine
TR.Close
Catch
Msgbox(LastException,"Read Error")
End Try

Try
If IsNumber(xsize) = False Then 'DIDN'T READ THE VALUE
Msgbox(xsize,"Not A Number")
Else
Msgbox(xsize,"IS A Number")
End If
Catch
Msgbox(LastException,"IsNumber Error")
End Try
End Sub
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
Added EditText1 View To Test Numeric/Non-Numeric Values

Sub ButtonWrite_Click
Dim TW As TextWriter
Dim xsize As String: xsize = "6"
Dim sdcard_folder As String: sdcard_folder = "testfolder"

If EditText1.Text <> "" Then
xsize = EditText1.Text
End If

If File.IsDirectory(File.DirRootExternal,sdcard_folder) = True Then
Try
TW.Initialize(File.OpenOutput(File.DirRootExternal & "/" & sdcard_folder,"xsize.txt",False))
TW.Write(xsize)
TW.Close
Msgbox(xsize,"xsize Value Written")
Catch
Msgbox(LastException,"Write Error")
End Try
Else
Try
File.MakeDir(File.DirRootExternal,sdcard_folder)
Msgbox(sdcard_folder & " Created",File.DirRootExternal)
Catch
Msgbox(LastException,"Create Error")
End Try
End If
End Sub


Sub ButtonRead_Click
Dim TR As TextReader
Dim xsize As String
Dim sdcard_folder As String: sdcard_folder = "testfolder"

Try
TR.Initialize(File.OpenInput(File.DirRootExternal, sdcard_folder & "/" & "xsize.txt"))
xsize = TR.ReadLine
TR.Close
Catch
Msgbox(LastException,"Read Error")
End Try

Try
If IsNumber(xsize) = False Then 'DIDN'T READ THE VALUE
Msgbox(xsize,"Not A Number")
Else
Msgbox(xsize,"IS A Number")
End If
Catch
Msgbox(LastException,"IsNumber Error")
End Try
End Sub
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
From 1st Post

This line of code

TR.Initialize(File.OpenInput(File.DirRootExternal, sdcard_folder & "xsize.txt"))

should be

TR.Initialize(File.OpenInput(File.DirRootExternal, sdcard_folder & "/xsize.txt"))

Note the '/'

or could be

TR.Initialize(File.OpenInput(File.DirRootExternal, sdcard_folder & "/" & "xsize.txt"))

rtesluk
 
Upvote 0

SarahWard

Banned
This is not a problem in Basic4android. Android doesn't handle the BOM marking like .Net. You should save the files without BOM.
Re-saving 2500 text files without bom is pretty much out of the question without a batch process in notepad++. :(

So this is a limitation with Android itself? Well it makes program conversion a real problem when using text files in other languages.
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Re-saving 2500 text files without bom is pretty much out of the question without a batch process in notepad++. :(
Sed will be useful here. Something like:
sed '1s/^\(.\)\{3\}//g' <filename>
should remove the first 3 bytes. Havent tried this though!

EDIT:
Tried it. It works.
Put all your txt files in one directory and run the following cmd line:
sed -i '1s/^\(.\)\{3\}//g' *

It should convert all your text files and remove the 1st three bytes in one go!
 
Last edited:
Upvote 0

rtesluk

Member
Licensed User
Longtime User
Forward Slash

This line in your code, I believe is incorrect; namely,

TR.Initialize(File.OpenInput(File.DirRootExternal, sdcard_folder & "xsize.txt"))

I wonder if you changed it to

TR.Initialize(File.OpenInput(File.DirRootExternal, sdcard_folder & "/xsize.txt"))

Note the FORWARD SLASH "/xsize.txt"

Ray Tesluk
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
Try this test app

Sep 29 2011
13:25 Hours

I just created a test app that might resolve your problem as I see it.

Regards,

Ray Tesluk
 

Attachments

  • TC20110929.zip
    4.4 KB · Views: 199
Upvote 0
Top