Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

VB 2005 Formatting Array Output Space Padding

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
acerteng
Asked:
acerteng
  • 5
  • 4
  • 3
1 Solution
 
DotNetThinkerCommented:
how about this...
sArray(1).SubString(0, 20)

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
acertengAuthor Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
DotNetThinkerCommented:
Dim name as string = sArry(0) & " " & sArry(1)
name = name.SubString(0, 20)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
acertengAuthor Commented:
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
 
DotNetThinkerCommented:

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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:

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
 
acertengAuthor Commented:
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
 
DotNetThinkerCommented:
Dim fullName As String = ""
0
 
acertengAuthor Commented:
Idle_Mind that worked...sorry my earlier post was in reference to DotNetThinker

Thank you both very much!!!!!
0
 
DotNetThinkerCommented:
sorry, i forgot to delete line 9
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now