Imports System.IO
Public Class frmRead_and_Write
Inherits System.Windows.Forms.Form
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
Dim sLine As String
sLine = Me.txtInput.Text
Dim swText As StreamWriter
If File.Exists("C:\text.txt") Then
swText = New StreamWriter("C:\text.txt", True)
Else
swText = New StreamWriter("C:\text.txt")
End If
swText.WriteLine(sLine)
swText.Flush()
swText.Close()
End Sub
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
ofdRead.DefaultExt = ".txt"
ofdRead.InitialDirectory = "C:\"
ofdRead.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*"
ofdRead.ShowDialog()
If File.Exists(ofdRead.FileName) Then
Dim srText As New StreamReader(ofdRead.FileName)
Dim strLineIn As String = String.Empty
Try
While srText.Peek >= 0
strLineIn = srText.ReadLine
Me.lblResult.Text &= strLineIn & Environment.NewLine
End While
Catch ex As Exception
MessageBox.Show(ex.Message & Environment.NewLine & strLineIn, "!! Error !!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
srText.Close()
End Try
Else
MessageBox.Show("file not found" & Environment.NewLine & "Run write file first", "!! Error !!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub btnKeywords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeywords.Click
ofdRead.DefaultExt = ".txt"
ofdRead.InitialDirectory = "C:\"
ofdRead.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*"
ofdRead.ShowDialog()
If File.Exists(ofdRead.FileName) Then
Dim srText As New StreamReader(ofdRead.FileName)
Dim strLineIn As String = String.Empty
Try
While srText.Peek >= 0
strLineIn = srText.ReadLine
Me.lblKeywords.Text &= strLineIn & Environment.NewLine
End While
Catch ex As Exception
MessageBox.Show(ex.Message & Environment.NewLine & strLineIn, "!! Error !!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
srText.Close()
End Try
Else
MessageBox.Show("file not found" & Environment.NewLine & "Run write file first", "!! Error !!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
Find Out Character OccuranceProgram should read the .txt file and find out the occurance of each c
Page 1 of 1
4 Replies - 5134 Views - Last Post: 27 September 2007 - 07:09 PM
#1
Find Out Character Occurance
Posted 25 September 2007 - 06:50 AM
My program can Read the file but don't know how can i find out the occerance of the each character, i have done following:-
Replies To: Find Out Character Occurance
#2
Re: Find Out Character Occurance
Posted 25 September 2007 - 11:18 AM
For simplicity sake, suppose you wanted to see if the character "A" occurs in your string.
You could use the following code example to iterate throught the string you read:
Does this help any?
Cheers,
Bob Porter
You could use the following code example to iterate throught the string you read:
Dim sTest As String = "A quick brown fox jumped over the fence and saw an Aardvark."
If sTest.Contains("A") Then
Debug.Print("Found at least one")
Dim MyEnumerator As System.CharEnumerator = sTest.GetEnumerator
Dim iCounter As Integer = 0
MyEnumerator.Reset()
For iLoopCounter As Integer = 0 To sTest.Length - 1
MyEnumerator.MoveNext()
If MyEnumerator.Current = "A" Then
iCounter += 1
End If
Next
Debug.Print(iCounter.ToString & " instances of 'A' were found.")
Else
Debug.Print("No instances of 'A' were found.")
End If
Does this help any?
Cheers,
Bob Porter
Thapa, on 25 Sep, 2007 - 06:50 AM, said:
My program can Read the file but don't know how can i find out the occerance of the each character, i have done following:-
#3
Re: Find Out Character Occurance
Posted 26 September 2007 - 06:28 PM
i want further improement in my program! how can i do the following
My program should read 2 text file and which have same variable with different values, For Example
"text1.txt" file content is something like this,
Ii 1
Jj 3
Kk 7
L l 5
and "text2.txt" file content is someting like this,
Ii 4
Jj 5
Kk 3
Ll 9
Note":- Variable position and value might change.
Now i need to read this two text file(I Know how to read text file) but over here i need to read it as variable and its value,
and need to find out sum of it,
i just need your guide in, How to read those words as variable and its number as its values.
If u can guide me i will really appricate.
Regards
Suraj Thapa
My program should read 2 text file and which have same variable with different values, For Example
"text1.txt" file content is something like this,
Ii 1
Jj 3
Kk 7
L l 5
and "text2.txt" file content is someting like this,
Ii 4
Jj 5
Kk 3
Ll 9
Note":- Variable position and value might change.
Now i need to read this two text file(I Know how to read text file) but over here i need to read it as variable and its value,
and need to find out sum of it,
i just need your guide in, How to read those words as variable and its number as its values.
If u can guide me i will really appricate.
Regards
Suraj Thapa
#4
Re: Find Out Character Occurance
Posted 26 September 2007 - 06:46 PM
Your previous example already was reading each line in, I am not sure what you mean by "summing" them, the values you show are not all numeric.
Can you elaborate?
Bob
Can you elaborate?
Bob
Thapa, on 26 Sep, 2007 - 06:28 PM, said:
i want further improement in my program! how can i do the following
My program should read 2 text file and which have same variable with different values, For Example
"text1.txt" file content is something like this,
Ii 1
Jj 3
Kk 7
L l 5
and "text2.txt" file content is someting like this,
Ii 4
Jj 5
Kk 3
Ll 9
Note":- Variable position and value might change.
Now i need to read this two text file(I Know how to read text file) but over here i need to read it as variable and its value,
and need to find out sum of it,
i just need your guide in, How to read those words as variable and its number as its values.
If u can guide me i will really appricate.
Regards
Suraj Thapa
My program should read 2 text file and which have same variable with different values, For Example
"text1.txt" file content is something like this,
Ii 1
Jj 3
Kk 7
L l 5
and "text2.txt" file content is someting like this,
Ii 4
Jj 5
Kk 3
Ll 9
Note":- Variable position and value might change.
Now i need to read this two text file(I Know how to read text file) but over here i need to read it as variable and its value,
and need to find out sum of it,
i just need your guide in, How to read those words as variable and its number as its values.
If u can guide me i will really appricate.
Regards
Suraj Thapa
#5
Re: Find Out Character Occurance
Posted 27 September 2007 - 07:09 PM
Ya all of the variables and values are string, i have tried few things last night and actually i got through it, below i got the code which will explain what i wanted to do, any way thanks for you concern,
*Always use code blocks
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sResult() As String
Dim sKeyWord() As String
sResult = Me.lblResult.Text.Split(CChar(vbCrLf))
sKeyWord = Me.lblKeywords.Text.Split(CChar(vbCrLf))
Dim resultVal(), keywordVal() As String
For Each s As String In sResult
resultVal = s.Split(CChar(" "))
For Each s1 As String In sKeyWord
keywordVal = s1.Split(CChar(" "))
If resultVal(0) = keywordVal(0) Then
If resultVal(0).Trim.Length > 0 And keywordVal(0).Trim.Length > 0 Then
Me.TextBox1.Text &= resultVal(0) & " " & (CDbl(resultVal(1)) + CDbl(keywordVal(1))).ToString & Environment.NewLine
Exit For
End If
End If
Next
Next
End Sub
*Always use code blocks
This post has been edited by PsychoCoder: 27 September 2007 - 07:14 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|