Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim contain(3) As Integer
Dim b, c, d, tem As Integer
' DECLARING FOR AN INPUT DATA OR STRING
Dim INPUT As String
INPUT = InputBox("Enter Your String")
ListBox1.Items.Add(INPUT)
' be will be - 1
For b = 0 To INPUT.ToString - 1
'be will be + 1
For c = b + 1 To INPUT.ToString - 1
If contain(B)/> < contain(c) Then
tem = contain(B)/>
contain(B)/> = contain(c)
contain(c) = tem
End If
Next
Next
For d = 0 To INPUT - 1
ListBox2.Items.Add(contain(d).ToString)
Next
End Sub
End Class
15 Replies - 1285 Views - Last Post: 17 March 2013 - 08:14 AM
#1
sorting
Posted 16 March 2013 - 01:14 AM
can anyone can give me idea .my problem is my sorting is not working ., i want is i have to listbox2 ,,. in listbox1 will display the input and in the listbox2 it will display the sorting .. but my problem is in listbox2 is empty(no display) .. . please help me
Replies To: sorting
#2
Re: sorting
Posted 16 March 2013 - 05:16 AM
Hello jayveeeeeee,
Do you just want to enter words into the inputbox one at a time and have them put in Listbox1 and sorted in Listbox2? If so you should know that an InputBox only had 1 string returned which would be your INPUT string. This means that your listboxes are only going to have 1 line in each listbox because it only gets run once when the form loads. Unless you are entering more like a sentence in the inputbox and want to split it up into separate words and list the words into the listboxes. There seems to be a few lines with errors in them and i can`t really tell what your goal is here? Please explain a little more and i can help you figure it out.
Do you just want to enter words into the inputbox one at a time and have them put in Listbox1 and sorted in Listbox2? If so you should know that an InputBox only had 1 string returned which would be your INPUT string. This means that your listboxes are only going to have 1 line in each listbox because it only gets run once when the form loads. Unless you are entering more like a sentence in the inputbox and want to split it up into separate words and list the words into the listboxes. There seems to be a few lines with errors in them and i can`t really tell what your goal is here? Please explain a little more and i can help you figure it out.
#3
Re: sorting
Posted 16 March 2013 - 05:53 AM
my main goal is i have to sort a sentence ..can i use sort in label ??
just gave me idea please and help me .this is only my last problem , i finish it the a , b part , my last is this C...
Write a program that reads several lines of text and prints a table indicating the number of occurrences of each different word in the text. The first version of your program should include the words in the table in the same order in which they appear in the text. For example, the lines To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer contain the words "to" three times, the word "be" two times, the word "or" once, etc. A more interesting (and useful) printout should then be attempted in which the words are sorted alphabetically.
just gave me idea please and help me .this is only my last problem , i finish it the a , b part , my last is this C...
#4
Re: sorting
Posted 16 March 2013 - 06:47 AM
Here is a quick example of how i would do it. If you have to count how many times the word is in the sentence then RegEx is a vary handy way to do it. Then just make listbox2 a sorted listbox. This may give you some ideas to use in your program.
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Get the input sentence
Dim INPUT As String = InputBox("Enter Sentence")
'Split the sentence into words
Dim splitstring() As String = INPUT.Split(CChar(" "))
'make listbox 2 display words sorted in alphabetical order
ListBox2.Sorted = True
For Each word As String In splitstring
'Count how many times the word is in the sentence
Dim wordcount As Integer = Regex.Matches(INPUT, word).Count
'Display word and word count in regular listbox
ListBox1.Items.Add(word & " - " & wordcount.ToString)
'Display word and word count in sorted listbox
ListBox2.Items.Add(word & " - " & wordcount.ToString)
Next
End Sub
End Class
#5
Re: sorting
Posted 16 March 2013 - 10:23 AM
Hey,
I just noticed that the Import line has (<b></b>) in it. I don`t know why the editor does that sometimes but, it should look like this
Imports System.Text.RegularExpressions
I just noticed that the Import line has (<b></b>) in it. I don`t know why the editor does that sometimes but, it should look like this
Imports System.Text.RegularExpressions
#6
Re: sorting
Posted 16 March 2013 - 07:19 PM
wait i will try it ..can i ask what is the purpose of Imports System.Text.RegularExp ????what is can do ? sorry if i ask you because we will defense this project ,
#7
Re: sorting
Posted 16 March 2013 - 07:32 PM
jayveeeeeee, on 16 March 2013 - 09:19 PM, said:
wait i will try it ..can i ask what is the purpose of Imports System.Text.RegularExp ????what is can do ? sorry if i ask you because we will defense this project ,
It is for importing RegularExpressions Namespace which is needed to use the ( Regex.Matches(INPUT, word).Count ) command to get the count of how many times the word exists in the INPUT sentence. Here is a link to msdn if you would like to find out a little more about it.
#8
Re: sorting
Posted 16 March 2013 - 09:17 PM
Hey jayveeeeeee,
I figured i would try to explain the (Imports) a little more. You do not have to use the Imports statement but, that would mean that you would have to use the full name in the RegEx code like shown here
Now suppose you had a few lines of code that used RegEx you would have to add (System.Text.RegularExpressions) to the beginning of each line that used the RegEx code. After a few lines of code it adds a lot more typing and code to your program. So by using the Imports statement you do not have to add (System.Text.RegularExpressions) to each line. You can just type
It basically it can save a lot of typing and space in your program code.
I figured i would try to explain the (Imports) a little more. You do not have to use the Imports statement but, that would mean that you would have to use the full name in the RegEx code like shown here
Dim wordcount As Integer = System.Text.RegularExpressions.Regex.Matches(INPUT, word).Count
Now suppose you had a few lines of code that used RegEx you would have to add (System.Text.RegularExpressions) to the beginning of each line that used the RegEx code. After a few lines of code it adds a lot more typing and code to your program. So by using the Imports statement you do not have to add (System.Text.RegularExpressions) to each line. You can just type
Dim wordcount As Integer = Regex.Matches(INPUT, word).Count
It basically it can save a lot of typing and space in your program code.
#9
Re: sorting
Posted 17 March 2013 - 12:39 AM
if u dont mind can u explain this im not quiet that i understand this ...
and this one to please ... what can this do ... what is function ..
[code ] Dim wordcount As Integer = Regex.Matches(INPUT, word).Count [ /code ]
Dim splitstring() As String = INPUT.Split(CChar(" "))
and this one to please ... what can this do ... what is function ..
[code ] Dim wordcount As Integer = Regex.Matches(INPUT, word).Count [ /code ]
#10
Re: sorting
Posted 17 March 2013 - 05:11 AM
Hi jayveeeeeee,
This declares a String Array named (splitstring)
The other half takes your (INPUT) string which is the sentence you entered and splits the string on each empty space between the words and assigns each word to one element of the array
For example if we entered "Today is a great day" then this is what the INPUT split into the splitstring array elements would be
splitstring(0) = "Today"
splitstring(1) = "is"
splitstring(2) = "a"
splitstring(3) = "great"
splitstring(4) = "day"
Now the For Each loop, loops threw each element in (splitstring). The first loop assigns word = splitstring(0), the second loop assigns word = splitstring(1), the third loop assigns word = splitstring(2) until it has looped threw all the elements of the splitstring array.
Here is the msdn Link on arrays. It may help you to understand arrays a little more. Here is the Link to the String.Split Method. It will explain a little more about the method.
This declares a String Array named (splitstring)
Dim splitstring() As String
The other half takes your (INPUT) string which is the sentence you entered and splits the string on each empty space between the words and assigns each word to one element of the array
= INPUT.Split(CChar(" "))
For example if we entered "Today is a great day" then this is what the INPUT split into the splitstring array elements would be
splitstring(0) = "Today"
splitstring(1) = "is"
splitstring(2) = "a"
splitstring(3) = "great"
splitstring(4) = "day"
Now the For Each loop, loops threw each element in (splitstring). The first loop assigns word = splitstring(0), the second loop assigns word = splitstring(1), the third loop assigns word = splitstring(2) until it has looped threw all the elements of the splitstring array.
For Each word As String In splitstring
Here is the msdn Link on arrays. It may help you to understand arrays a little more. Here is the Link to the String.Split Method. It will explain a little more about the method.
#11
Re: sorting
Posted 17 March 2013 - 05:20 AM
ok thanks i understand now ..thanks you very much ..
#12
Re: sorting
Posted 17 March 2013 - 05:34 AM
No problem. I know it was not exactly how you wanted to do it but, i couldn`t think of a better way of doing it without making a long drawn out repetitive bunch of code. Even if you don`t use it you have at least learned a few things to use later on in other programs.
#13
Re: sorting
Posted 17 March 2013 - 06:55 AM
can i ask one more please because this is my first time to use the (for each ),..
For Each word As String In splitstring why the code is not give error we did not declare the variable word ..is it automatic when we use for each or not ??????sorry for the other question i just to learn more on programming especially vb.net
For Each word As String In splitstring why the code is not give error we did not declare the variable word ..is it automatic when we use for each or not ??????sorry for the other question i just to learn more on programming especially vb.net
#14
Re: sorting
Posted 17 March 2013 - 07:19 AM
jayveeeeeee, on 17 March 2013 - 07:55 AM, said:
can i ask one more please because this is my first time to use the (for each ),..
For Each word As String In splitstring why the code is not give error we did not declare the variable word ..is it automatic when we use for each or not ??????sorry for the other question i just to learn more on programming especially vb.net
For Each word As String In splitstring why the code is not give error we did not declare the variable word ..is it automatic when we use for each or not ??????sorry for the other question i just to learn more on programming especially vb.net
Yes, word, in this case is declared by the As String. Any For loop can be done this way. In the regular For loop, we do not usually have to declare the loop counter because it's assumed by the compiler to be an Integer, even if we have set Option Strict On.
If we want to use a different variable type in the loop, we have to declare it, which can be done with a Dim, or with a declaration as part of the For loop, so For x As Int64 will not throw an exception or prevent a compile, and will allow you to work with very large numbers.
It's the same in a For Each, except that instead of a specific type, the compiler takes the type from the type of variable specified by the In. So, if you wanted change the type, you can specify it in the same way as the exampe I just gave. So an Array of Byte could be scanned as For Each x As Int16 in byteArray, which tells the compiler that you know it's a Byte, but you want each one converted to Int16.
This post has been edited by lar3ry: 17 March 2013 - 07:21 AM
#15
Re: sorting
Posted 17 March 2013 - 07:42 AM
jayveeeeeee, on 17 March 2013 - 08:55 AM, said:
can i ask one more please because this is my first time to use the (for each ),..
For Each word As String In splitstring why the code is not give error we did not declare the variable word ..is it automatic when we use for each or not ??????sorry for the other question i just to learn more on programming especially vb.net
For Each word As String In splitstring why the code is not give error we did not declare the variable word ..is it automatic when we use for each or not ??????sorry for the other question i just to learn more on programming especially vb.net
I am not sure what you mean by (why the code is not give error we did not declare the variable word) but, the (For Each word As String) is declaring (word) as a string variable just like if we wrote (Dim Word As String). If we used a For loop like this we would need to declare the variable separately.
Dim word As String = ""
For x = 0 To splitstring.Length - 1
word = splitstring(x)
'Then do the rest of the code to count words and list them
Next
|
|

New Topic/Question
Reply



MultiQuote





|