开发者

string manipulation VB.net

Below is the input file

     DELL  NOTEBOOK
     1000  USD      
     ACER  NOTEBOOK
     HP    NOTEBOOK
     APPLE MOBILE
     900   USD
     HTC   MOBILE
     800   USD

Basically I need to check if there any word "USD" on the second line and put the word Yes or No. on the first line. Expected Output

     DELL  NOTEBOOK   YES
     1000  USD      
     ACER  NOTEBOOK   NO
     HP    NOTEBOOK   NO
     APPLE MOBILE     YES
     900   USD
     HTC   MOBILE     YES
     800   USD

below is my code that need some tweak

      Sub Main()
      Dim fh As StreamReader
      fh = new StreamReader("list.txt")
      dim currency as string
      dim bCurrency as boolean
      Dim s As String = fh.ReadLine()
      While not s Is Nothing
     开发者_如何学C       currency = s.substring(5,3)
            if currency = "USD" then
              bCurrency  = True
            else
                if bCurrency  = true then
                    Console.WriteLine(s & "     Yes")
                    bCurrency  = False
                else
                    Console.WriteLine(s & "     No")
                end if             
            end if

            s = fh.ReadLine
      End While
      fh.Close()
    End Sub


Edited to include Saving to file and on screen both.

Do you want the final output to be printed to the screen, or saved into another text file? Here is the way it would appear on screen AS WELL AS SAVED to OUTPUT.TXT

 Dim tmpLine as String
 Dim FirstLine as Boolean = True

    Dim fh As StreamReader
    Dim fout as StreamWriter
    fh = New StreamReader("list.txt")
    fout = New StreamWriter("output.txt")

    Dim line As String = fh.ReadLine()
    Dim lineData As String() = Nothing

    While Not line Is Nothing
        lineData = line.Split(" ")

       If FirstLine=False Then
            If lineData(1).Equals("USD") Then
                 Console.WriteLine(tmpLine & " Yes")
                 fout.WriteLine(tmpLine & " Yes")
            Else
                 Console.WriteLine(tmpLine & " No") 
                 fout.WriteLine(tmpLine & " No")
            End If
        Else
             FirstLine = False
        End If

        tmpLine = line
        line = fh.ReadLine
    End While
    fh.Close()

        If lineData(1).Equals("USD") Then
             Console.WriteLine(tmpLine & " Yes")
             fout.WriteLine(tmpLine & " Yes")
        Else
             Console.WriteLine(tmpLine & " No") 
             fout.WriteLine(tmpLine & " No")
        End If
    fout.Close()


Have a well defined input format and use split().

Input:

DELL NOTEBOOK
1000 USD
ACER NOTEBOOK
HP NOTEBOOK
APPLE MOBILE
900 USD
HTC MOBILE
800 USD

Method:

    Dim fh As StreamReader
    fh = New StreamReader("list.txt")
    Dim line As String = fh.ReadLine()
    Dim nextLine As String = fh.ReadLine()

    While line IsNot Nothing
        If nextLine IsNot Nothing Then
            Dim lineData As String() = nextLine.Split(" ")
            If lineData(1).Equals("USD") Then
                Console.WriteLine(line & " Yes")
                Console.WriteLine(nextLine)
            Else
                Console.WriteLine(line & " No")
                Console.WriteLine(nextLine & " No")
            End If
            line = fh.ReadLine
            nextLine = fh.ReadLine
        Else
            Console.WriteLine(line & " No")
            line = fh.ReadLine
        End If
    End While
    fh.Close()
    End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜