locked
I NEED HELP!!!! PLEASE RRS feed

  • Question

  • How do i go about skipping spaces in my Visual Basic 2005 Express Edition console application output? lets say i type the number" 12345 " and want it to display " 1   2   3   4   5 " skipping 3X spaces in between each letter. I already am using Modulus to single out each integer,

     

    Tuesday, September 12, 2006 10:11 PM

Answers

  • The following will work

            Dim s As String = Console.ReadLine
            For Each c As Char In s
                Console.Write(c & "   ")
            Next

    You say you typed in a number - the fact that its a number you dont show any code as to how you enter the line.  I would assume something like console.readline, in which case its just a string.

     

    Tuesday, September 12, 2006 10:43 PM
  • Why is this not great code. 

    1. You have no exception handling

    2. You are depending upon and implied conversion to integer

    In your code try entering a value which is not convertible to an integer - say a value xyz

    The following is includes some rudimentary exception handling for situations when the value is not numeric.

    The items highlight will generate the required output originally asked for.

     I could ask why you are using modulus to get the characters when you can simply turn this value into a string using the tostring method and iterate through the characters.   This way it is just string processing and not dependent upon base 10.

     

    Module Module1

        Sub Main()

            'variables used in the calculation

            Dim number As Integer

            Dim n1 As Integer

            Dim n2 As Integer

            Dim n3 As Integer

            Dim n4 As Integer

            Dim n5 As Integer

            Dim endnumber As Integer

            'prompt for and read the number from the user
            Try

                Console.Write("Please enter the integer: ")

                Dim InputString As String = Console.ReadLine()

                '//Verify Input is actually Numeric
                If IsNumeric(InputString) Then
                    number = CType(InputString, Integer) '//Cast To Integer
                    n1 = ((number / 10000) Mod 10)
                    n2 = ((number / 1000) Mod 10)
                    n3 = ((number / 100) Mod 10)
                    n4 = ((number / 10) Mod 10)
                    n5 = ((number / 1) Mod 10)

                    endnumber = (n1) & (n2) & (n3) & (n4) & (n5)

                    '//Build Required String from Number
                    Dim sFinalString As String = ""
                    For Each c As Char In endnumber.ToString
                        sFinalString = sFinalString & c & "   "
                    Next

                    '//Output String
                    Console.WriteLine("The end number is " & sFinalString) ' display the ending number
                Else
                    Console.WriteLine("Didn't enter a integer")
                End If
            Catch ex As Exception
                Console.WriteLine("An exception occured" & vbCrLf & ex.Message)
            End Try
           
        End Sub ' Main

    End Module ' Separation

    Wednesday, September 13, 2006 1:40 AM

All replies

  • The following will work

            Dim s As String = Console.ReadLine
            For Each c As Char In s
                Console.Write(c & "   ")
            Next

    You say you typed in a number - the fact that its a number you dont show any code as to how you enter the line.  I would assume something like console.readline, in which case its just a string.

     

    Tuesday, September 12, 2006 10:43 PM
  • THIS IS WHAT I CURRENTLY HAVE...

    Module Separation

    Sub Main()

    'variables used in the calculation

    Dim number As Integer

    Dim n1 As Integer

    Dim n2 As Integer

    Dim n3 As Integer

    Dim n4 As Integer

    Dim n5 As Integer

    Dim endnumber As Integer

    'prompt for and read the number from the user

    Console.Write("Please enter the integer: ")

    number = Console.ReadLine()

    n1 = ((number / 10000) Mod 10)

    n2 = ((number / 1000) Mod 10)

    n3 = ((number / 100) Mod 10)

    n4 = ((number / 10) Mod 10)

    n5 = ((number / 1) Mod 10)

    endnumber = (n1) & (n2) & (n3) & (n4) & (n5)

    Console.WriteLine("The end number is " & endnumber) ' display the ending number

    End Sub ' Main

    End Module ' Separation

    Tuesday, September 12, 2006 11:40 PM
  • Why is this not great code. 

    1. You have no exception handling

    2. You are depending upon and implied conversion to integer

    In your code try entering a value which is not convertible to an integer - say a value xyz

    The following is includes some rudimentary exception handling for situations when the value is not numeric.

    The items highlight will generate the required output originally asked for.

     I could ask why you are using modulus to get the characters when you can simply turn this value into a string using the tostring method and iterate through the characters.   This way it is just string processing and not dependent upon base 10.

     

    Module Module1

        Sub Main()

            'variables used in the calculation

            Dim number As Integer

            Dim n1 As Integer

            Dim n2 As Integer

            Dim n3 As Integer

            Dim n4 As Integer

            Dim n5 As Integer

            Dim endnumber As Integer

            'prompt for and read the number from the user
            Try

                Console.Write("Please enter the integer: ")

                Dim InputString As String = Console.ReadLine()

                '//Verify Input is actually Numeric
                If IsNumeric(InputString) Then
                    number = CType(InputString, Integer) '//Cast To Integer
                    n1 = ((number / 10000) Mod 10)
                    n2 = ((number / 1000) Mod 10)
                    n3 = ((number / 100) Mod 10)
                    n4 = ((number / 10) Mod 10)
                    n5 = ((number / 1) Mod 10)

                    endnumber = (n1) & (n2) & (n3) & (n4) & (n5)

                    '//Build Required String from Number
                    Dim sFinalString As String = ""
                    For Each c As Char In endnumber.ToString
                        sFinalString = sFinalString & c & "   "
                    Next

                    '//Output String
                    Console.WriteLine("The end number is " & sFinalString) ' display the ending number
                Else
                    Console.WriteLine("Didn't enter a integer")
                End If
            Catch ex As Exception
                Console.WriteLine("An exception occured" & vbCrLf & ex.Message)
            End Try
           
        End Sub ' Main

    End Module ' Separation

    Wednesday, September 13, 2006 1:40 AM
  • thanks for the answer, just started taking VB so wasnt really sure how to approach it. thanks

    Wednesday, September 13, 2006 8:00 PM
  • Is it just me or is this the most useless application ever? If I read this right you ask the user for an integer (which you assume, I think, is 5 digits long) rip it up one digit at a time and reassemble them. I'm not actually just complaining but there are much better ways to accomplish this.
    Thursday, September 14, 2006 2:41 PM
  • Yes it is useless.  Except maybe as a learning exercise!  Which I suspect is the purpose of this question.

     

    Here is my 2 cents on the best algorithm:

     

     

    Dim Input As String

    Dim MyBuffer As System.Text.StringBuilder

    Try

    Console.WriteLine("Please input a number")

    Input = Console.ReadLine

    If IsNumeric(Input) Then

    MyBuffer = New System.Text.StringBuilder

    'this puts no realistic limit on the length of the number

    For Icount As Integer = 0 To Input.Length - 1

    MyBuffer.Append(Input.Substring(Icount, 1) & " ")

    Next

    Console.WriteLine(MyBuffer.ToString)

    Else

    Console.WriteLine("Input not a valid number")

    End If

    Catch ex As Exception

    Console.WriteLine(ex.Message)

    End Try

     

    Ibrahim Malluf

    Thursday, September 14, 2006 7:20 PM
  • Oh, I get that it's an excercise. It's just seems like an ineffective one. It's not tied too well to anything of substance or value (which is fine on it's own) but it's not really teaching an efficient, elegant, way to address a pattern of problem and even were we to find the "best way" to address it it's still the best Patterns and Practices for doing squat.

    I guess I'm venting on the educational system at this point. It just looks like a lot of code to do little and teach even less.

       Sub Main()
          Console.Write("Please enter the integer: ")

          Dim number As Integer
          If Integer.TryParse(Console.ReadLine(), number) Then
             Console.WriteLine("The end number is " & number.ToString("#   #   #   #   #").Trim())
          Else
             Console.WriteLine("Didn't enter a integer.")
          End If
       End Sub

    There, see that teaches tools, TryParse and format strings. It fails in the same ways the original did in that it does not consider numbers larger than 99999. It's concise, efficient, easily changed. It's early bound and doesn't suffer the rounding errors that the original code did, though use of the \ operator rather than the / operator for integer division would have done as well.

    Sorry if I haven't been very helpful.

    Saturday, September 16, 2006 7:11 AM