2011-06-22 40 views
-1

下面是輸入文件字符串操作VB.net

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

基本上我需要檢查是否有第二行的任何單詞「USD」,並把 字是或在第一行號。 預期低於輸出

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

是我的代碼需要一些調整

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

回答

0

編輯,包括保存到文件,並都在屏幕上。

是否要將最終輸出打印到屏幕或保存到另一個文本文件? 這裏是它會出現在屏幕上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() 
+0

Ahmad-San將保存到另一個文本文件。 – user801207

+0

更新了代碼以包含兩種方法。你可以註釋掉你不想使用的那個 – Ahmad

+0

Ahmad-San,最後一行沒有寫? – user801207

0

有一個明確定義的輸入格式,並使用分裂()。

輸入:

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

方法:

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

012的樣子,我所需要的單詞是或否的描述,如DELL筆記本,宏基筆記本不能用價格的行中 – user801207

+0

哎呀。我的不好,編輯了代碼。 – o12

+0

012,文字「HP NOTEBOOK」沒有寫在您的代碼 – user801207