2011-06-22 42 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