We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Character Array Help

I need to fill an array according to the order of number in a string of text a user provides(a telephone number). For example if the number 555-5142 is dialed, the first 3 numbers are five, so char firstLetter, secondLetter, and thirdLetter would be filled with 'J','K,'L'.
I've looked at how to do this, and haven't found any clear answers. Here's some code on what I'm doing. Basically it's supposed to generate words from a phone number. I'm needing to convert the textbox text(holds phone number) and assign those values as being first, second third, etc, that way my program can print when it is supposed to. I've had a really hard time getting this far, but maybe one of you can take a look. Thanks in advance.
` char[] twoLetters = { 'A', 'B', 'C' };
char[] threeLetters = { 'D', 'E', 'F' };
char[] fourLetters = { 'G', 'H', 'I' };
char[] fiveLetters = { 'J', 'K', 'L' };
char[] sixLetters = { 'M', 'N', 'O' };
char[] sevenLetters = { 'P', 'R', 'S' };
char[] eightLetters = { 'T', 'U', 'V' };
char[] nineLetters = { 'W', 'X', 'Y', 'Z' };

    char[] firstLetter = { ' ', ' ', ' ' };  
    char[] secondLetter = { ' ', ' ', ' ' };
    char[] thirdLetter = { ' ', ' ', ' ' };
    char[] fourthLetter = { ' ', ' ', ' ' };
    char[] fifthLetter = { ' ', ' ', ' ' };
    char[] sixthLetter = { ' ', ' ', ' ' };
    char[] seventhLetter = { ' ', ' ', ' ' };


    int[] numbers = new int[8] { 2, 3, 4, 5, 6, 7, 8, 9 };
    string userInput;

    private void TwoButton_Click(object sender, EventArgs e)
    {
        NumberTextBox.Text += numbers[0];
        userInput += numbers[0];     
    }
    `private void Generateutton_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    for (int l = 0; l < 3; l++)
                    {
                        for (int m = 0; m < 3; m++)
                        {
                            for (int n = 0; n < 3; n++)
                            {
                                for (int o = 0; o < 3; o++)
                                {
                                    using (StreamWriter writer = new StreamWriter("F:\\log.txt", true))
                                    {

                                        writer.Write(firstLetter[i]);
                                        writer.Write(" ");
                                        writer.Write(secondLetter[j]);
                                        writer.Write(" ");
                                        writer.Write(thirdLetter[k]);
                                        writer.Write(" ");
                                        writer.Write(fourthLetter[l]);
                                        writer.Write(" ");
                                        writer.Write(fifthLetter[m]);
                                        writer.Write(" ");
                                        writer.Write(sixthLetter[n]);
                                        writer.Write(" ");
                                        writer.Write(seventhLetter[o]);
                                        writer.WriteLine();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

}

3
Contributors
10
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
11
Views
Question
Answered
kiail
Light Poster
40 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Are you just supposed to make "words" from phone numbers?
...or is there a particular group of steps you're supposed to take?

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

yes, i'm supposed to make words from phone numbers, as far as particular group of steps i'm supposed to take, that's what I'm here. Here is what I have so far: (it currently doesn't register properly the order in which I click a button - i tried moving my write statements to each button click, but that started messing up the pattern in which it was written to file(very badly).

`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SimpeNumberConvertToWord
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    char[] twoLetters = { 'A', 'B', 'C' };
    char[] threeLetters = { 'D', 'E', 'F' };
    char[] fourLetters = { 'G', 'H', 'I' };
    char[] fiveLetters = { 'J', 'K', 'L' };
    char[] sixLetters = { 'M', 'N', 'O' };
    char[] sevenLetters = { 'P', 'R', 'S' };
    char[] eightLetters = { 'T', 'U', 'V' };
    char[] nineLetters = { 'W', 'X', 'Y', 'Z' };


    int[] numbers = new int[8] { 2, 3, 4, 5, 6, 7, 8, 9 };
    string userInput;

    private void TwoButton_Click(object sender, EventArgs e)
    {
        NumberTextBox.Text += numbers[0];
        userInput += numbers[0];
        char[] array = NumberTextBox.Text.ToCharArray();

    }

    private void ThreeButton_Click(object sender, EventArgs e)
    {
        NumberTextBox.Text += numbers[1];
        userInput += numbers[1];
    }

    private void FourButton_Click(object sender, EventArgs e)
    {
        NumberTextBox.Text += numbers[2];
        userInput += numbers[2];
    } 

    private void FiveButton_Click(object sender, EventArgs e)
    {
        NumberTextBox.Text += numbers[3];
        userInput += numbers[3];
    }

    private void SixButton_Click(object sender, EventArgs e)
    {
        NumberTextBox.Text += numbers[4];
        userInput += numbers[4];
    }

    private void SevenButton_Click(object sender, EventArgs e)
    {
        NumberTextBox.Text += numbers[5];
        userInput += numbers[5];
    }

    private void EightButton_Click(object sender, EventArgs e)
    {
        NumberTextBox.Text += numbers[6];
        userInput += numbers[6];
    }

    private void NineButton_Click(object sender, EventArgs e)
    {
        NumberTextBox.Text += numbers[7];
        userInput += numbers[7];
    }


    private void Generateutton_Click(object sender, EventArgs e)
    {  
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    for (int l = 0; l < 3; l++)
                    {
                        for (int m = 0; m < 3; m++)
                        {
                            for (int n = 0; n < 3; n++)
                            {
                                for (int o = 0; o < 3; o++)
                                {
                                    for (int p = 0; p < 3; p++)
                                    {
                                        using (StreamWriter writer = new StreamWriter("F:\\log.txt", true))
                                        {

                                            writer.Write(threeLetters[j]);
                                            writer.Write(" ");
                                            writer.Write(fourLetters[k]);
                                            writer.Write(" ");
                                            writer.Write(fiveLetters[l]);
                                            writer.Write(" ");
                                            writer.Write(sixLetters[m]);
                                            writer.Write(" ");
                                            writer.Write(sevenLetters[n]);
                                            writer.Write(" ");
                                            writer.Write(eightLetters[o]);
                                            writer.WriteLine();

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

}
`

kiail
Light Poster
40 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

oh it can only be a max of 7 numbers entered, but that's pretty easy to fix

kiail
Light Poster
40 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

You'd be better off putting the chars into a jagged array and just retrieving the whole (inner) array based on the offset of the digit pressed.

  private static char[][] arr_arr_chrPhonePad = new char[10][]
  {  // jagged array
     new char[]{}, //0
     new char[]{}, //1
     new char[]{'a', 'b', 'c'}, //2
     new char[]{'d', 'e', 'f'}, //3
     new char[]{'g', 'h', 'i'}, //4
     new char[]{'j', 'k', 'l'}, //5
     new char[]{'m', 'n', 'o'}, //6
     new char[]{'p', 'q', 'r', 's'}, //7
     new char[]{'t', 'u', 'v'}, //8
     new char[]{'w', 'x', 'y', 'z'}, //9
  };

So

 arr_arr_chrPressed[3]

will return 'd','e','f'.

And

 new String(arr_arr_chrPhonePad[3])

will make a string "def"

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

I have tried to get it to reference to each number properly when writing, but some how I've got it writing Chinese or something. I do not think this is what I want lol. I'm trying to access the element in my write statements, and I think it's messed up when I'm converting value. It turns it to chinese when converted to string at the value line. If it's the way it is now it leaves an indexoutofrangeexception. However, I think you have set me a little closer honestly to my goal. I just need to figure out why I can't access the elements properly.
`private static char[][] PhonePad = new char[10][]
{ // jagged array
new char[]{}, //0
new char[]{}, //1
new char[]{'a', 'b', 'c'}, //2
new char[]{'d', 'e', 'f'}, //3
new char[]{'g', 'h', 'i'}, //4
new char[]{'j', 'k', 'l'}, //5
new char[]{'m', 'n', 'o'}, //6
new char[]{'p', 'q', 'r', 's'}, //7
new char[]{'t', 'u', 'v'}, //8
new char[]{'w', 'x', 'y', 'z'}, //9
};

    int[] numbers = new int[8] { 2, 3, 4, 5, 6, 7, 8, 9 };
    string userInput;
    `
`private void Generateutton_Click(object sender, EventArgs e)
    {


        int[] values = new int[NumberTextBox.Text.Length];

        for (int x = 0; x < NumberTextBox.Text.Length; x++)
        {
            values[x] = Convert.ToInt32(NumberTextBox.Text[x]);
        }



        for (int i = 0; i <= 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    for (int l = 0; l < 3; l++)
                    {
                        for (int m = 0; m < 3; m++)
                        {
                            for (int n = 0; n < 3; n++)
                            {
                                for (int o = 0; o < 3; o++)
                                {
                                        try
                                        {
                                            using (StreamWriter writer = new StreamWriter("F:\\log.txt", true))
                                            {

                                                writer.Write(PhonePad[Convert.ToInt32(values[0])][i]);
                                                writer.Write(" ");
                                                writer.Write(PhonePad[Convert.ToInt32(values[1])][j]);
                                                writer.Write(" ");
                                                writer.Write(PhonePad[Convert.ToInt32(values[2])][k]);
                                                writer.Write(" ");
                                                writer.Write(PhonePad[Convert.ToInt32(values[3])][l]);
                                                writer.Write(" ");
                                                writer.Write(PhonePad[Convert.ToInt32(values[4])][m]);
                                                writer.Write(" ");
                                                writer.Write(PhonePad[Convert.ToInt32(values[5])][n]);
                                                writer.Write(" ");
                                                writer.Write(PhonePad[Convert.ToInt32(values[6])][o]);
                                                writer.WriteLine();
                                            }
                                        }
                                        catch (IndexOutOfRangeException IOEx)
                                        {
                                            MessageBox.Show("something bad happened");
                                        }

                                    }
                            }`
kiail
Light Poster
40 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

okay, it's not in chinese any more =) I got worried.

kiail
Light Poster
40 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Why does everyone think new is the way to create static arrays? What a waste of resources!

Use a 2D array, row=number dialed, col=letters:

char letters[10][4] = { {'-','-','-','-'},  //0
                        {'-','-','-','-'},  //1
                        {'A','B','C','-'},  //2
                        {'D','E','F','-'},  //3
                        {'G','H','I','-'},  //4
                        {'J','K','L','-'},  //5
                        {'M','N','O','-'},  //6
                        {'P','R','S','-'},  //7
                        {'T','U','V','-'},  //8
                        {'W','X','Y','Z'}}; //9
WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

I'd love to know how to do it the right way, how would I implement that? I tried using it as is, renaming it, etc, and it just gave me errors.

kiail
Light Poster
40 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Thines, that pretty much worked, I have my program producing words from numbers now thanks to you. Now I'm off to figure how to put the letter z in, it freaks out and messes up my nice neat order when I try to use z. Thanks a million Thines.

kiail
Light Poster
40 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by thines01 and WaltP

You're right @WaltP. It was unnecessary to call new on that structure.

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3220 seconds using 2.77MB