-
Jul 20th, 2014, 01:55 AM
#1
Thread Starter
New Member
Visual Studio 2013 Visual basic Easy program
So I am new to all of this I tried making a program that I need as soon as possible (Hope someone replies quick)
So here is my code
Imports System
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim file As System.IO.StreamReader
file =
My.Computer.FileSystem.OpenTextFileReader("C:\Users\XXX\Desktop\stuff.txt")
Dim file2 As System.IO.StreamWriter
file2 = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\XXX\Desktop\stuff2.txt", True)
Dim line As String
line = file.ReadLine()
Dim words As String() = line.Split()
Dim index As Integer = 0
For Each word As String In words
index += 1
If String.Compare(word, "The", True) = 0 Then Exit For
Next word
If index < words.Length Then
file2.WriteLine(words(index))
file2.Close()
Else
file2.WriteLine("")
file2.Close()
End If
End Sub
End Class
So What the program was supposed to do was read a certain txt file and then if it locates a word preceded by The it should place this word in the second file So it does that but the problem is I want it to Write them in the other file 1 after one On each line
But when I test it it only prints the first line's word
I hope you understand this and please reply Thanks
-
Jul 20th, 2014, 03:33 AM
#2
Re: Visual Studio 2013 Visual basic Easy program
you are only reading one line with this:
line = file.ReadLine()
so you only process the first line of the file. your way of searching for the word "the" is suboptimal (i.e. split and loop through the words). use .contains instead or learn regexp but maybe keep this for later.
-
Jul 20th, 2014, 01:26 PM
#3
Thread Starter
New Member
Re: Visual Studio 2013 Visual basic Easy program
SO is there any way to do this either with another bit of code or by improving this?
-
Jul 20th, 2014, 01:26 PM
#4
Thread Starter
New Member
Re: Visual Studio 2013 Visual basic Easy program
Ok I will see what I can do then
-
Jul 20th, 2014, 01:35 PM
#5
Thread Starter
New Member
Re: Visual Studio 2013 Visual basic Easy program
Could you give me an example?
-
Jul 20th, 2014, 02:02 PM
#6
Addicted Member
Re: Visual Studio 2013 Visual basic Easy program
Here is a basic tutorial. I recommend you read and do some exercises until you fully understand at least the first 4 chapters.
http://www.homeandlearn.co.uk/NET/vbNet.html
-
Jul 20th, 2014, 02:09 PM
#7
Re: Visual Studio 2013 Visual basic Easy program
Welcome to the forum. In future please provide a descriptive thread title. Right now your thread issue is meaningless to members. Next always post code inside of code or highlight tags. This can be written manually or via the tool menu interface. Finally only post code that we need to see. You have simply chucked us your whole class and said "work this out for your self".
Break this into steps, your worrying about the end when the start is written poorly. This is step One. No splitting.
vb Code:
Imports System.IO Public Class Form1 Private ReadOnly FileLocation As String = "C:\Users\XXX\Desktop\stuff.txt" Private Sub Button1_Click(sender As Object, e As EventArgs) Dim lines As String() = File.ReadAllLines(Me.FileLocation) End Sub End Class
-
Jul 20th, 2014, 02:12 PM
#8
Thread Starter
New Member
Re: Visual Studio 2013 Visual basic Easy program
Thank you I will fix everything
-
Jul 20th, 2014, 02:14 PM
#9
Thread Starter
New Member
Re: Visual Studio 2013 Visual basic Easy program
Thanks
-
Jul 20th, 2014, 02:34 PM
#10
Thread Starter
New Member
Re: Visual Studio 2013 Visual basic Easy program
How should it look like now?
-
Jul 20th, 2014, 03:59 PM
#11
Re: Visual Studio 2013 Visual basic Easy program
Originally Posted by alaaemad15
How should it look like now?
how should what look now?
-
Jul 20th, 2014, 04:39 PM
#12
Thread Starter
New Member
Re: Visual Studio 2013 Visual basic Easy program
Originally Posted by ident
how should what look now?
How should the program look like now?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|