Converting string to char type data, then defining whether a letter or a number?

Tangent100

New member
Joined
Oct 4, 2013
Messages
4
Programming Experience
Beginner
I need to create a program in VB net 2010 that converts a string that a person inserts into a char type and then defines whether it is an upper case, lower case or a number... multiwingspan

This is my 'idontknowwhatiamdoing' code, I got to the point of converting it but I don't even have an idea of how to check whether it has converted or not...

Dim str As String

Console.WriteLine("Input a character")
Str = Console.ReadLine()

Dim chars As Char() = Str.ToCharArray()

Any help is appreciated, especially the easy-to-understand one, thank you.
 
There's no need to check whether it converted because it can't possibly not. After executing that code you will have a Char array, no two ways about it. To process each Char in that array, you can use a For Each loop. The things is, you can use a For Each loop on the original String anyway.

Either way, when processing a single Char, the Char structure has several methods that can help you out. Specific to your question, the IsDigit, IsLetter, IsUpper and IsLower methods.
 
Okay, so I got this now but it doesn't work I don't know why... shows that it is unaccessible due to security after underlying IsLower, IsLower and IsDigit, where have I gone wrong? Thanks!
Sub Main()
        Dim str As String

        Console.WriteLine("Input a character")
        str = Console.ReadLine()

        Dim OneCharacter As Char() = str

        If IsLetter(OneCharacter) Then
            If IsLower(OneCharacter) Then
                Console.WriteLine("Lowercase letter")
            Else
                Console.WriteLine("Uppercase letter")
            End If
        Else : IsDigit(OneCharacter)
            Console.WriteLine("Decimal digit")
        End If
        Console.ReadLine()
    End Sub
 
Last edited by a moderator:
Well, first of all, how can you assign a String to a Char array variable? Is a String a Char array? In your previous code you were calling ToCharArray so why did you just arbitrarily stop doing that?

I said that you need to use a For Each loop either over that Char array or over the String itself. I don't see any For Each loop.

As I said in my previous post, those methods are members of the Char structure. WriteLine and ReadLine are members of the Console class and look how you're calling them.
 
Hi,

Ah, I got beaten to it again but, the last part of what I was about to post was:-

Finally, The best bit of advice I can give you is to turn Option Strict On. This will help you to identify and correct all Type Conversion errors as you encounter them within your coding. To change this in your current project go to Project->Properties on the IDE's Menu and change the options on the Compile Tab and for all future projects you can do this by going to Tools->Options on the IDE's Menu and changing the VBDefaults.

Hope that helps.

Cheers,

Ian
 
Well, first of all, how can you assign a String to a Char array variable? Is a String a Char array? In your previous code you were calling ToCharArray so why did you just arbitrarily stop doing that?

I said that you need to use a For Each loop either over that Char array or over the String itself. I don't see any For Each loop.

As I said in my previous post, those methods are members of the Char structure. WriteLine and ReadLine are members of the Console class and look how you're calling them.

The thing is that it is Nested If exercise so I am trying to use nested if, I haven't learned to do For Each loop yet so I am trying to make the code with things I have learned. Not that I am lazy, I just thought there is a way to do it without using For Each loop or any other stuff which I haven't learned yet.

I guess I'll have to learn more about char structure to understand your third point, this is all new to me. :chargrined:
 
Hi,

Based on what has already been said then what you need to do is to first of all make sure that only one character has been entered into your console application. If that is True then you can convert that one character of the entered string into a Char value and then do whatever comparisons you need with your If statements. If the string is NOT a Single character then you can display some error message. Here is a quick snippet from your own code:-

If str.Length = 1 Then
  Dim oneCharacter As Char = str(0)
  'you can now do all your If Statements based on the single character that has been entered
Else
  Console.WriteLine("You must only enter one character at a time")
End If


Hope that helps.

Cheers,

Ian
 
Back
Top