I need to write a program using a For … Next loop to accept a word as

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1681 Views - Last Post: 28 April 2012 - 09:47 PM Rate Topic: -----

#1 aguyfromhere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-April 12

I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 08:21 PM

I need to write a program using a For … Next loop to accept a word as input and determine if its letters are in alphabetical order.

This is the code I came up with but am getting the error "Index and length must refer to a location within the string.
Parameter name: length" at run time when I click the button. Any help would be appreciated as I have spent hours on this and searched all over the web and haven’t had any luck finding any examples.

Thanks!


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim word As String = CStr(TextBox1.Text)
        Dim length As Integer = word.Length - 1
        Dim track As Integer = 0
        Dim letter1 As String
        Dim letter2 As String
        For i As Integer = 0 To length Step 1
            letter1 = word.Substring(i, 1)
            letter2 = word.Substring(i + 1, 1)
            If letter1 > letter2 Then
                track = (track + 1)
            End If
        Next i
        If track = length Then
            TextBox2.Text = "The letters of this word are in alphabetical order!"
        Else : TextBox2.Text = "The letters of this word are NOT in alphabetical order! Sorry!"
        End If
    End Sub
End Class



Is This A Good Question/Topic? 1
  • +

Replies To: I need to write a program using a For … Next loop to accept a word as

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 08:38 PM

If your at the last letter then how can you get substring + 1 there are no more letters. You need a check before getting the + 1, if Not i = length Then ...

This post has been edited by _HAWK_: 28 April 2012 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,019
  • Joined: 12-June 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 08:44 PM

Look at your line 11 there.. it's taking the current position plus one.. what happens when you get to the last character and it does a plus one? Yup - that falls off the edge of the string! Perhaps capping the for-loop two places before the end (-2 not -1) might work better.. then again I am unclear how you are expecting "a > b" to work out when you try and evaluate "abc"... perhaps using the < instead?
Was This Post Helpful? 1
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 08:53 PM

Collections in .net are 0 ... (N - 1)

You don't require to use .Substring, look the indexer on a string.

Dim Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim f = Alphabet(0) ' A
Dim l = Alphabet(25) ' Z


Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:06 PM

I would make this into a Function so you can utilise early exiting, when you've found a contradiction.

  Public Function IsInAlphabeticalOrder(s As String) As Boolean
    If s Is Nothing Then Return False
    If s.Length < 2 Then Return True
    For i = 1 To s.Length - 1
      If s(i - 1) > s(i) Then Return False
    Next
    Return True
  End Function 



Or you could go loopless and use LINQ
Return s = New String(s.OrderByDescending(Function(c) c).Reverse.ToArray)


This post has been edited by AdamSpeight2008: 28 April 2012 - 09:07 PM

Was This Post Helpful? 1
  • +
  • -

#6 aguyfromhere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-April 12

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:10 PM

View PostAdamSpeight2008, on 28 April 2012 - 08:53 PM, said:

Collections in .net are 0 ... (N - 1)

You don't require to use .Substring, look the indexer on a string.

Dim Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim f = Alphabet(0) ' A
Dim l = Alphabet(25) ' Z



Thanks for trying to help me, but I’m not sure how this helps since I am using user inputted words each time…

Could you please explain?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,019
  • Joined: 12-June 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:12 PM

Strings are arrays... if you tell it what position in the array of characters you want you will get just that character..
Was This Post Helpful? 0
  • +
  • -

#8 aguyfromhere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-April 12

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:20 PM

View Postmodi123_1, on 28 April 2012 - 08:44 PM, said:

Look at your line 11 there.. it's taking the current position plus one.. what happens when you get to the last character and it does a plus one? Yup - that falls off the edge of the string! Perhaps capping the for-loop two places before the end (-2 not -1) might work better.. then again I am unclear how you are expecting "a > b" to work out when you try and evaluate "abc"... perhaps using the < instead?


Hey buddy, thank you thank you. With your help I got this code to work!

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim word As String = CStr(TextBox1.Text)
        Dim length As Integer = word.Length - 2
        Dim track As Integer = -1
        Dim letter1 As String = ""
        Dim letter2 As String = ""
        For i As Integer = 0 To length Step 1
            letter1 = word.Substring(i, 1)
            letter2 = word.Substring(i + 1, 1)
            If letter1 < letter2 Then
                track += 1
            End If
        Next i
        If track = length Then
            TextBox2.Text = "The letters of this word are in alphabetical order!"
        Else : TextBox2.Text = "The letters of this word are NOT in alphabetical order! Sorry!"
        End If
    End Sub
End Class



You rock man!
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,019
  • Joined: 12-June 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:22 PM

Do you need to write it that way? There is so much extraneous crap in there it's almost painful to me! ;)
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:24 PM

Strings can be treated as if the where ReadOnly Array of Char, because the String Class implements a Indexer for it.
You can utilise LINQ because the String Class implements IEnumerable(Of Char)
Was This Post Helpful? 0
  • +
  • -

#11 aguyfromhere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-April 12

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:24 PM

View Postmodi123_1, on 28 April 2012 - 09:22 PM, said:

Do you need to write it that way? There is so much extraneous crap in there it's almost painful to me! ;)


Haha. This is homework and the instructions say to use a For … Next loop. :nono:
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,019
  • Joined: 12-June 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:27 PM

No.. the for loop is okay.. but you have so much goofy crap in there.. like you don't need "next i".. just "next".. and by default a forloop is "step 1" so you can drop that off.. but thos are just examples.
Was This Post Helpful? 0
  • +
  • -

#13 aguyfromhere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-April 12

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:28 PM

View Postmodi123_1, on 28 April 2012 - 09:27 PM, said:

No.. the for loop is okay.. but you have so much goofy crap in there.. like you don't need "next i".. just "next".. and by default a forloop is "step 1" so you can drop that off.. but thos are just examples.


Now this.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim word As String = CStr(TextBox1.Text)
        Dim length As Integer = word.Length - 2
        Dim track As Integer = -1
        Dim letter1 As String = ""
        Dim letter2 As String = ""
        For i As Integer = 0 To length
            letter1 = word.Substring(i, 1)
            letter2 = word.Substring(i + 1, 1)
            If letter1 < letter2 Then
                track += 1
            End If
        Next
        If track = length Then
            TextBox2.Text = "The letters of this word are in alphabetical order!"
        Else : TextBox2.Text = "The letters of this word are NOT in alphabetical order! Sorry!"
        End If
    End Sub
End Class



What else can I trim in your opinion?
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:30 PM

Look at my implementation in Post# 5. This is all you need, it still uses a For ... Next loop
In the click handler.
If IsInAlphabeticalOrder(TextBox1.Text) Then
  TextBox2.Text = "The letters of this word are in alphabetical order!"
Else
 TextBox2.Text = "The letters of this word are NOT in alphabetical order! Sorry!"
End If



Even that contains redundant code.
TextBox2.Text = If(IsInAlphabeticalOrder(TextBox1.Text),
                   "The letters of this word are in alphabetical order!",
                   "The letters of this word are NOT in alphabetical order! Sorry!"



Also take the time to give your control proper contextually meaningful names.

This post has been edited by AdamSpeight2008: 28 April 2012 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#15 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,019
  • Joined: 12-June 08

Re: I need to write a program using a For … Next loop to accept a word as

Posted 28 April 2012 - 09:31 PM

Like most of it.

        Dim track As Integer = 0
        For i As Integer = 0 To TextBox1.Text.Length - 2  '-- you can reference the textbox length directly.
'-- why drop it into more strings - just use the text as is.. 
            If CStr(TextBox1.Text(i)) < CStr(TextBox1.Text(i + 1)) Then  
                track += 1
            End If
        Next
        If track = TextBox1.Text.Length - 1 Then '-- another example of using the length dynamically.
            TextBox2.Text = "The letters of this word are in alphabetical order!"
        Else 
TextBox2.Text = "The letters of this word are NOT in alphabetical order! Sorry!"
        End If


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2