Solved

VB 2005 Formatting Array Output Space Padding

Posted on 2007-11-19
12
176 Views
Last Modified: 2013-12-26
I am using VB 2005 Express. I have extracted data from one file and put it into another file. (.dat) My problem is the program that is reading the file needs it to be formatted in a specific way to be able to read it.

the first array is 13 spaces total, the second array is 8 spaces total, the third array is 20 spaces total and the last is 4 spaces total. Total meaning with data that array needs to be that amount of spaces.

My problem is the third array, this is where names are inputted and they can vary in length but the space can only accommodate 20 characters&spaces (total). All of the other inputs are pretty much constant.

Here is and example of what the output should look like:

123456      7890    johnjacob jinglehime1234
123456      7890   John Doe                  1234                    

Hopefully that gives you an idea of what I am dealing with. Notice that in the first row the name was cut off...that's okay. In my current code the output looks like this and my program won't read it:

123456 7890 johnjacob jinglehimerschmidt1234
123456 7890 John Doe1234


Imports System.IO
 

Module Mod1

   Sub Main()

      Dim fIn As StreamReader

      Dim fOut As StreamWriter

      Dim sArry() As String
 

'This reads file and creates/overwrites new .dat

      fIn = New StreamReader(New FileStream("c:\testfile1.dat", fileMode.Open))

      fOut = New StreamWriter(New FileStream("c:\testfile2.dat", FileMode.Create))
 

'Parse text

      Do Until (fIn.EndOfStream)

        sArry = Split(fIn.ReadLine, ",")

        If UBound(sArry) = 8 Then _

fOut.WriteLine(sArry(4) & " " & sArry(2) & " " & sArry(1) & " " & sArry(0) & "TEXT")

      Loop

      fIn.Close()

      fOut.Close()

   End Sub
 

End Module

Open in new window

0
Comment
Question by:acerteng
  • 5
  • 4
  • 3
12 Comments
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20313838
how about this...
sArray(1).SubString(0, 20)

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20313874
Use String.PadRight():
http://msdn2.microsoft.com/en-us/library/34d75d7s.aspx

fOut.WriteLine(sArry(4).PadRight(13) & sArry(2).PadRight(8) & sArry(1).PadRight(20) & sArry(0).PadRight(4) & "TEXT")

Open in new window

0
 

Author Comment

by:acerteng
ID: 20314889
Idle_Mind so far so good...thank you for the PadRight...I knew there had to be something out there.  I cannot get sArry(0) space sArry(1) to combine then do PadRight(20).

It counts the space only including sArry(1)...hopefully that makes sense

Maybe this will help sArry(0) fName sArry(1) is lName.  Need to concatenate those two with a space between them to total 20 spaces...cutting off anything over 20 from sArry(1)

fName lName fill in space totaling 20

Thank you for your input.

Not sure if this (concatenating) is a separate incident or can be part of original question...I can put in another question if I need to and accept your solution.  Let me know.







0
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20314945
Dim name as string = sArry(0) & " " & sArry(1)
name = name.SubString(0, 20)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20314954
Gotcha...so you need to concat, pad, and then trunc.

I think something like this is what you are after:

    Dim fullName As String = sArry(0) & " " & sArry(1)
    fullName = fullName.PadRight(20).Substring(0, 20)

    fOut.WriteLine(sArry(4).PadRight(13) & sArry(2).PadRight(8) & fullName & "TEXT")
0
 

Author Comment

by:acerteng
ID: 20315185
Okay my limited coding knowledge reached it limits last week.  I tried putting the Dim in a couple of places.  Since the sArry() is not known until the first file is read I get an error message.  

I tried to put the Dim in a couple of other places to no avail.  Where in the code should I put the Dim?
Imports System.IO
 

Module Mod1

    Sub Main()

        Dim fIn As StreamReader

        Dim fOut As StreamWriter

        Dim sArry() As String

        Dim fullName As String = sArry(0) & " " & sArry(1)

        fullName = fullName.PadRight(20).Substring(0, 20)
 
 

        fIn = New StreamReader(New FileStream("c:\testfile1.dat", FileMode.Open))

        fOut = New StreamWriter(New FileStream("c:\testfile2.dat", FileMode.Create))

        Do Until (fIn.EndOfStream)

            sArry = Split(fIn.ReadLine, ",")

            If UBound(sArry) = 8 Then _

            fOut.WriteLine(sArry(4).PadRight(12) & sArry(2).PadRight(8) & fullName & "TEXT")

        Loop

        fIn.Close()

        fOut.Close()

    End Sub
 

End Module

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20315213

Imports System.IO

 

Module Mod1

    Sub Main()

        Dim fIn As StreamReader

        Dim fOut As StreamWriter

        Dim sArry() As String

        Dim fullName As String

        fullName = fullName.PadRight(20).Substring(0, 20)

 

 

        fIn = New StreamReader(New FileStream("c:\testfile1.dat", FileMode.Open))

        fOut = New StreamWriter(New FileStream("c:\testfile2.dat", FileMode.Create))

        Do Until (fIn.EndOfStream)

            sArry = Split(fIn.ReadLine, ",")

            If UBound(sArry) = 8 Then

                fullName = sArry(0) & " " & sArry(1)

                fullName = fullName.SubString(0, 20)

                fOut.WriteLine(sArry(4).PadRight(12) & sArry(2).PadRight(8) & fullName & "TEXT")

            EndIf

        Loop

        fIn.Close()

        fOut.Close()

    End Sub

 

End Module

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 20315246

Imports System.IO

Module Mod1

    Sub Main()

        Dim fIn As StreamReader

        Dim fOut As StreamWriter

        Dim sArry() As String

        Dim fullName As String 

 

        fIn = New StreamReader(New FileStream("c:\testfile1.dat", FileMode.Open))

        fOut = New StreamWriter(New FileStream("c:\testfile2.dat", FileMode.Create))

        Do Until (fIn.EndOfStream)

            sArry = Split(fIn.ReadLine, ",")

            If UBound(sArry) = 8 Then 

                fullName = (sArry(0) & " " & sArry(1)).PadRight(20).Substring(0, 20)

                fOut.WriteLine(sArry(4).PadRight(12) & sArry(2).PadRight(8) & fullName & "TEXT")

            End If

        Loop

        fIn.Close()

        fOut.Close()

    End Sub

End Module

Open in new window

0
 

Author Comment

by:acerteng
ID: 20315250
Copied and pasted still comes up with

Warning      1      Variable 'fullName' is used before it has been assigned a value. A null reference exception could result at runtime.
0
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20315267
Dim fullName As String = ""
0
 

Author Comment

by:acerteng
ID: 20315274
Idle_Mind that worked...sorry my earlier post was in reference to DotNetThinker

Thank you both very much!!!!!
0
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20315281
sorry, i forgot to delete line 9
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now