Solved

VB 2005 Formatting Array Output Space Padding

Posted on 2007-11-19
12
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 11

Expert Comment

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

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
 
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 86

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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