Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 2nd, 2003, 12:08 AM
Authorized User
 
Join Date: Jul 2003
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nvillare
Default HELP, I am having trouble figuring out how to..

I am learning VB.net and am working from a book. I am having trouble figuring out one of their review exercises.

I have to write a program that inputs one number consisting of five digits from the user. The program then separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user typed 12345 it would show
1 2 3 4 5

This is to use the command window for input and output. It says to assume the user will enter only 5 digits, and to use both divison and modulus operators.

Can someone please give me a push into the right direction!! The book doesn't explain how to do that.

Your help is greatly appreciated!!


Thanks!

nvillare
__________________
Thanks!

N
 
Old October 2nd, 2003, 06:29 AM
Registered User
 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

try this

        Dim i As Integer
        Dim strInput As String = ""
        Dim charArray As Char()
        Dim strOutput As String = ""

        strInput = "12345"

        charArray = strInput.ToCharArray()

        For i = 0 To charArray.Length - 1
            'strOutput += charArray(i) & " "
            strOutput = strOutput & charArray(i) & " "
        Next
        MessageBox.Show(strOutput)

 
Old October 2nd, 2003, 08:33 AM
Authorized User
 
Join Date: Jul 2003
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nvillare
Default

Thanks for the help. The only thing is, I have to use division and mod, to figure it out. I have no idea on that.

Thanks!

nvillare
 
Old October 2nd, 2003, 08:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

Here it is in VB6. You will have to convert to VB.NET.
I used the mod & the interger division operator.

    Dim lngNumber As Long
    Dim strOutput As String

    ' Initialize values
    lngNumber = 88888
    strOutput = ""

    ' Loop until done.
    ' We will be removing the least significant digit
    ' at the end of each iteration.
    Do While lngNumber > 0

        ' Use Mod 10 to get the last digit, convert that
        ' to a string, add it and the spaces to the FRONT
        ' of the string so far.
        strOutput = CStr(lngNumber Mod 10) & _
                    " " & _
                    strOutput

        ' Remove the last digit.
        ' Note the \ truncates the fraction.
        lngNumber = lngNumber \ 10
    Loop

    Debug.Print strOutput



John R Lick
[email protected]
 
Old October 2nd, 2003, 09:01 AM
Authorized User
 
Join Date: Jul 2003
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nvillare
Default

Thanks, but why use mod 10? I am trying to understand how it works.

Thanks!

nvillare
 
Old October 2nd, 2003, 09:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

X Mod Y gives the remainder when you divide X by Y. 21 / 10 is 2 remainder 1, so 21 Mod 10 = 1. Any number mod 10 will give you the last digit.

You use mod’s often in daily life. For example if it is 10:45 AM, and you want to know what time it will be in 30 minutes, you use Mod 60. (45 + 30) Mod 60 = 75 Mod 60 = 15, so it would be 11:15 AM.

Hours use Mod 12 (or mod 24 for a 24 hour clock). 11:00 + 2 hours is 1:00... (11 + 2) Mod 12 = 13 Mod 12 = 1.


John R Lick
[email protected]
 
Old March 13th, 2005, 08:09 PM
Registered User
 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have been learning Java from a self tech book and I ran into the same exercise.

This is how I solved it. Just look at the algorisims and ignore the Java code.

Code:
// Chapter 2 :: Self Exercise 2.30

// Write an application that inputs one number consisting of five digits 
// from the user, separates the numbers unto its individual digits and prints 
// the digits separated from one another by three spaces each.

// For example, if the user types in the number 42339, the program should print
// 4   2   3   3   9

import java.util.Scanner;

public class Ch2_Ex30
{
    // main method begins execution of Java application
    public static void main( String args[] )
    {
        // create Scanner to obtain input from command window
        Scanner input = new Scanner( System.in );

        int userInput; // User input... Number to be separated.

        System.out.print( "Enter a five digit integer: " ); // prompt user.
        userInput = input.nextInt(); // read five digit integer from user's keys.

        int first  =  (userInput / 10000); // first digit
        int second =  (userInput % 10000) / 1000; // second digit
        int third  =  (userInput % 1000) / 100; // thrid digit
        int fourth =  (userInput % 100) / 10; // fourth digit
        int fifth  =  (userInput % 10); // fifth digit

        System.out.printf( "%d   %d   %d   %d   %d", first, second, third,
         fourth, fifth ); // display integer separated into individual digits.
    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed in figuring out some Frame Code John Parker Access VBA 8 January 2nd, 2008 12:55 PM
If Else Trouble Cibressus Beginning PHP 0 May 1st, 2007 02:04 PM
Figuring out which values don't match ninel SQL Server 2000 7 May 12th, 2006 01:26 AM
Please i have had a Trouble hurted Wrox Book Feedback 1 June 28th, 2004 02:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.