Results 1 to 9 of 9

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

  1. #1
    Tangent100 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2013
    Posts
    4
    Reputation
    0

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

    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.

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,778
    Reputation
    1703
    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.

  3. #3
    Tangent100 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2013
    Posts
    4
    Reputation
    0
    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 jmcilhinney; 10-05-2013 at 9:09 AM. Reason: Added [xcode=vb] for readability

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,778
    Reputation
    1703
    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.

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,778
    Reputation
    1703
    You're also missing an If statement, which I can now see after formatting your code for you. Don't use colons like that; it just makes the code harder to read.

  6. #6
    IanRyder is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2012
    Location
    Healing, NE Lincs, UK
    Posts
    1,128
    Reputation
    743
    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

  7. #7
    Tangent100 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2013
    Posts
    4
    Reputation
    0
    Quote Originally Posted by jmcilhinney View Post
    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.

  8. #8
    IanRyder is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2012
    Location
    Healing, NE Lincs, UK
    Posts
    1,128
    Reputation
    743
    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

  9. #9
    Tangent100 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2013
    Posts
    4
    Reputation
    0
    Thanks for help from everybody!! Code is working

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •