|
-
Dec 1st, 2011, 03:11 PM
#1
Thread Starter
New Member
Please Help with Visual Studios 2010 Assignment
I got this assignment due tonight and I suck at Visual Studios 2010. Can someone who has ease with this please help. I'm supposed to design this program in ConsoleApplication about Phone Numbers:
You are writing a program and you need to store telephone numbers. You'd like to be user-friendly, and let the users enter their telephone number in whatever format they like with parentheses and dashes, so long as they enter 10 digits.
Assume that they won't enter any other characters. The user will only enter ( ) - spaces and numbers.
So, these would all be valid inputs,
6121234567
(612)123-4567
612 123-4567
612-123-4567
(612) 1234567
Write a program which validates a phone number entered. In your validation, you'll need to remove all the extra characters ( ) - and spaces from the user's input. And, you'll need to check that there are 10 digits.
If the phone number doesn't have 10 digits, ask the user to re-enter their phone number.
You'll most likely want to put the validation into a function.
Once they have entered a valid phone number, remove all the extra characters and store the phone number in an Integer variable, and display it.
You've used the function Val to convert Strings to numbers before. What does that function do if the argument isn't a number? How does this code behave? How could you use the Val function in your program? There is a way of validating the user's input has 10 digits using the Val function - there is an equally correct way without using the Val function.
So, if your user enters,
(612)- 123 4567
Your program would display
Thanks for your input
6121234567
This is the first part and the second part is Part 2: Camel Case method name generator. For example: convertStringToInteger(), printAllEmployeeData(), getUserInput().
Write a program which asks the user what a method will do. The user will enter a string which looks like this,
Convert string to integer
or
Print all employee data
Your program will take that input, and convert it to a method name in camel case. So, it will convert
Convert string to integer ----> convertStringToInteger
Print all employee data ----> printAllEmployeeData
Display the camel case method name to the user.
There's more than one way to do this. Hints:
Part 1: There's some variations of how to do this, but here's one way.
You want to change the first letter of each word to uppercase, except the first word. How can the computer know what parts of the string represent a word? What character separates words in a string?
You'll be capitalizing the first letter of each word. The string you are processing can have any number of words. What type of programing structure is appropriate to do the same task for a number of items?
One way to solve this problem is to have your program search the string for a space. What function can you use to do that? (Check the Visual Basic String Library Functions and Example Program document in the content section) Then, it will use the Substring function to cut the string into three parts - the string before and the space, the character after the space, and the rest of the string. For example, you would divide this string
"Write the data to a file"
to
"Write"
"t"
"he data to a file"
Note the space after the first word isn't included in any of the string. You can use ToUpper on the string containing the T. Then, join the strings back together to get
WriteThe data to a file
Now you need to think of a way to repeat this process. What structure repeats code? What condition do you want to check to determine when you are done processing the string? Hint: the Contains function may be helpful.
So, you should end up with a string that looks like this.
WriteTheDataToAFile
And now you can use Substring and ToLower to take the first character, make it lowercase, and rejoin it to the rest of the string.
Thank you very much for your help. Ima keep working on it but if someone stumbles upon this and can help me out it would greatly be appreciated. All i would need is the code from the visual basics screen. Thank you once again. 
-
Dec 1st, 2011, 04:18 PM
#2
Thread Starter
New Member
Re: Please Help with Visual Studios 2010 Assignment
I'm almost done with the first part but just not sure how to validate that the input from user is 10 digits. I think you use a boolean structure of true or false and might be able to do it in pseudocode but not sure in VB. This is what I got so far.....
Module Module1
Sub Main()
Dim number As String
Dim newNumber As String
Dim lengthOfNumber As Integer
Console.WriteLine("Type in your phone number")
number = Console.ReadLine
newNumber = number.Replace("(", "")
newNumber = newNumber.Replace(")", "")
newNumber = newNumber.Replace("-", "")
newNumber = newNumber.Replace(" ", "")
lengthOfNumber = newNumber.Length()
Console.WriteLine(newNumber)
Console.WriteLine(lengthOfNumber)
Console.ReadKey()
End Sub
End Module
-
Dec 1st, 2011, 04:28 PM
#3
Re: Please Help with Visual Studios 2010 Assignment
The code from the visual basic screen? Isn't that pretty much everything other than the form designs themselves?
Folks here are happy to help, even with homework, but we want to see some effort on your part first. Show us some code and we'll help you out.
My usual boring signature: Nothing
 
-
Dec 1st, 2011, 07:08 PM
#4
Thread Starter
New Member
Re: Please Help with Visual Studios 2010 Assignment
I got everything done on the first part. Just not sure how to run it as a function module to validate the input from user.
Last edited by alvarjaime; Dec 1st, 2011 at 07:31 PM.
-
Dec 1st, 2011, 07:20 PM
#5
Thread Starter
New Member
Re: Please Help with Visual Studios 2010 Assignment
Actually this is the code I put into visual basics. I'm stuck here on how to proceed:
Module Module1
Sub Main()
Dim number As String
Dim newNumber As String
Dim lengthOfNumber As Integer
Console.WriteLine("Type in your phone number")
number = Console.ReadLine
newNumber = number.Replace("(", "")
newNumber = newNumber.Replace(")", "")
newNumber = newNumber.Replace("-", "")
newNumber = newNumber.Replace(" ", "")
lengthOfNumber = newNumber.Length()
If lengthOfNumber < 10 Or lengthOfNumber > 10 Then
Console.WriteLine("Please type in a 10 digit number only")
number = Console.ReadLine
Return
End If
Console.WriteLine(newNumber)
Console.ReadKey()
End Sub
Last edited by alvarjaime; Dec 1st, 2011 at 07:28 PM.
-
Dec 1st, 2011, 09:44 PM
#6
Lively Member
Re: Please Help with Visual Studios 2010 Assignment
You should just loop through each character in the newNumber string, and check if the character is a number. If it is a number then count it. When the loop is done check if the count = 10.
vb Code:
'this will keep a count of the numbers Dim count As Integer = 0 'this will loop through each char in newNumber For Each C As Char In newNumber 'this will check if the char is numeric. If Val(C) = True Then 'if C is a number then add to the count count += 1 End If Next 'end of loop 'when the loop is done, this will check if the count = 10 If count = 10 Then MsgBox("Thanks for your input! " & newNumber) Else MsgBox("Please type in a 10 digit number only") End If
Instead of using Val(C) to check if the character is a number you can also use isNumeric(C).
-edit-
sorry I made a mistake in the above code. Change Val(C) to isNumeric(C).
Last edited by MotoX646; Dec 2nd, 2011 at 12:42 AM.
-
Dec 1st, 2011, 09:57 PM
#7
Lively Member
Re: Please Help with Visual Studios 2010 Assignment
Or, if you don't want to do a loop, you can just do this:
vb Code:
'this will check if newNumber string is a number, and convert it to a double. 'then it will convert the double to a string and return the length of the string. 'if the length of the string is 10 then you have 10 numbers. If Val(newNumber).ToString.Length = 10 Then MsgBox("Thanks for your input! " & newNumber) Else MsgBox("Please type in a 10 digit number only") End If
As for the second part of your assignment... Let me see your attempt and I will help you out after that.
Last edited by MotoX646; Dec 1st, 2011 at 10:03 PM.
-
Dec 1st, 2011, 10:44 PM
#8
Thread Starter
New Member
Re: Please Help with Visual Studios 2010 Assignment
THANX A BUNCH MAN!!!!!.... really appreciate your reply. I mean, I still can't just paste it because there are errors when I run the program but i think I can manage it a little better. I started a bit on the second part but got stuck on how to use the trim function to separate into 3 strings and using a loop to step thru all the input to capitalize using the toUpper function and then joining the strings. I got this so far but Ima try and work on the first part again now that I got some more info. Thanx.
Last edited by alvarjaime; Dec 1st, 2011 at 11:37 PM.
-
Dec 1st, 2011, 11:35 PM
#9
Thread Starter
New Member
Re: Please Help with Visual Studios 2010 Assignment
For some reason I couldn't use your code: I did run this code and ALMOST got everything. One thing is at the end when it displays the final number it goes on an infinite loop for some reason. I think I can fix that with console.ReadKey() but then it will still infinite loop. This is the code I got so far for the first part:
Module Module1
Sub Main()
Dim number As String
Dim newNumber As String
Dim lengthOfNumber As Integer
Console.WriteLine("Type in your phone number")
number = Console.ReadLine
While Val(number) = False
newNumber = number.Replace("(", "")
newNumber = newNumber.Replace(")", "")
newNumber = newNumber.Replace("-", "")
newNumber = newNumber.Replace(" ", "")
lengthOfNumber = newNumber.Length()
If lengthOfNumber < 10 Or lengthOfNumber > 10 Then
Console.WriteLine("Please type in a 10 digit number only")
number = Console.ReadLine()
Return
Else
Console.WriteLine("Thank you for your input your number is " & newNumber)
End If
End While
End Sub
End Module
-
Dec 1st, 2011, 11:55 PM
#10
Lively Member
Re: Please Help with Visual Studios 2010 Assignment
Oops, sorry about the errors. I just realized you are making a console application so you can't use message boxes. In my code you will have to replace "msgbox" with "Console.WriteLine", then it should work.
There are a couple ways to do the second part of your assignment. I will teach you one way...
First we will get the input string. Here you will use Console.ReadLine to read the input, but I will just do this:
vb Code:
Dim inputstring As String = "Print all employee data"
Then we need to split the input string into words. The best way to do that is use the String.Split method, and we will split the string by spaces " " .
vb Code:
Dim inputSplit As String() = inputstring.Split(" ")
inputSplit is now a collection of strings. inputSplit(0) is "Print". inputSplit(1) is "all". inputSplit(2) is "employee". inputSplit(3) is "data".
We can now loop through each word/string inside of inputSplit like this:
vb Code:
For Each word As String In inputSplit 'do stuff Next
Now that we can loop through each word, we need to take each word and capitalize the first character, then store it in a new string.
To get the first character of a string you can use String.Substring(0,1) and that will start at character 0, and take 1 character. So that means "employee".Substring(0,1) = "e". Then you can just call String.ToUpper and capitalize it.
To get all characters after the first character you can use String.Substring(1) and that will start at character 1 and take everything after it. So that means "employee".Substring(1) = "mployee".
So.... "employee".Substring(0,1).toUpper & "employee".Substring(1) = "Employee" . We can now add something similar to the loop.
vb Code:
Dim newString as string = "" For Each word As String In inputSplit newString += word.Substring(0,1).toUpper & word.Substring(1) Next
When the loop finishes, newString will be "PrintAllEmployeeData"
Now you just have to take the first character of newString and make it lower case, and you are finished. I'm sure you can figure out how to do that now.
-
Dec 2nd, 2011, 12:10 AM
#11
Lively Member
Re: Please Help with Visual Studios 2010 Assignment
OOPS! Sorry I'm a bit tired, I made a mistake in my first loop example...
Val(C) returns a number (double) and not a boolean (true/false). So the If statement is wrong. Only isNumeric(C) returns a boolean.
This is wrong:
vb Code:
If Val(C) = True Then End if
This is right:
vb Code:
If isNumeric(C) = True Then End If
It's best to use my second example... So your first assignment would work like this:
vb Code:
Module Module1 Sub Main() Dim number As String Dim newNumber As String Dim isNumber As Boolean = False Console.WriteLine("Type in your phone number") While isNumber = False number = Console.ReadLine newNumber = number.Replace("(", "") newNumber = newNumber.Replace(")", "") newNumber = newNumber.Replace("-", "") newNumber = newNumber.Replace(" ", "") If Val(newNumber).ToString.Length = 10 Then Console.WriteLine("Thanks for your input! " & newNumber) isNumber = True Else Console.WriteLine("Please type in a 10 digit number only") End If End While End Sub End Module
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|