none
Im confused about how to store user inputted strings to an array and display all of the entered strings at the end..... HELP plz :) RRS feed

  • Question

  • I have figured out how to let the user keep inputing strings which go into an array, which then is repeated back. (i know it may seem pointless but im trying to learn concepts)

    But what i would love to know is how they could just keep entering strings and than type something like "-99" to break the loop which then displays everything they have entered.

    Class1.cs
    [spoiler]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TESTING_ARRAYS
    {
        class Class1
        {
    
    
            public static void PrintArray(string[] foo)
            {
                for (int i = 0; i < foo.Length; i++)
                {
                    Console.Write("You entered: {0}\n\n", foo[i]);
                }
            }
    
            public static void result()
            {
                int start = 0;
                int counter = 1;
                string inputStuff = " ";
                Console.WriteLine("**enter '-99' to exit\n\n");
    
                while (start <= 1000)
                {
                    Console.Write("Input the last name #{0}{1}", counter++, ": ");
                    inputStuff = Console.ReadLine();
                    string[] weekDays = { inputStuff };
                    PrintArray(weekDays);
    
                    if (inputStuff == "-99")
                    {
                        Console.WriteLine("Loop has ended\n *press any key exit*");
                        start = 1000 + 1;
                    }//end of if
                }//end of while
            }//end of method "result"
        }//end of class
    }//end of namespace
    

    [/spoiler]

    Main class
    [spoiler]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace TESTING_ARRAYS
    {
        class Program
        {
            static void Main(string[] args)
            {
                Class1.result();
                Console.ReadKey();
            }
        }
    }

    [/spoiler]

    Basically I use the method "result" for the dirty work with while loop to allow user to keep inputing strings. which is sent to the method "printArray" to display it, and of course the main class just instantiates the result method.

    Sunday, April 15, 2012 6:13 AM

Answers

  • If anyone is having my issue I solved this  using only Arrays.however it is not expandible so you can just use large numbers like 2million, and if the user types "stop" the loop wil break, and all the entered names will be displayed in acending order.

    Here is the code.

    string[] storeNames = new string[10000];
                string s = "\n";
    
                Console.WriteLine("Enter names:\nType \"stop\" to break the loop.\n");
    
                for (int i = 0; i < storeNames.Length; i++)
                {
                    string val;
                    val = Console.ReadLine();
                    if (val == "stop")
                    {
                        break;
                    }
                    storeNames[i] = val;
                    storeNames[i] += "\n";
                    
                }
    
    
                foreach (string num in storeNames)
                {
                    s += num;
                }
    
                Console.WriteLine("\n");
                Console.WriteLine("The names in ascending order are: {0}", s);
                Console.ReadKey();

    And here is the array that uses a list (it is expandible)

                List<string> list = new List<string>();
                //list.Add("one");
                //list.Add("two");
                //list.Add("three");
                
                Console.WriteLine("Enter as many last names as you wish\n    Type: 'stop' to end the loop.\n________________");
                string input = "";
                while (input != "stop")
                {
                    input = Console.ReadLine();
                    list.Add(input);
                }
    
    
                Console.WriteLine("----------------\n\n\nHere are all of the last names you have entered in acending order:\n_________________");
                string[] array = list.ToArray();
                //for (int i = 0; i < array.Length; i++)
                foreach (string stringValues in array)
                {
                    Console.WriteLine(stringValues);       //The foreach way
                    //Console.WriteLine(array[i]); //The for way
                }
                Console.WriteLine("----------------\nPress any key to exit...");
                Console.ReadKey();

    Wednesday, April 18, 2012 2:16 AM

All replies

  • Simplest way would be to use ArrayList.

    Add: using System.Collections;

    ArrayList myList = new ArrayList();
    string input = "";
    
    do
    {
        Console.WriteLine("Enter Last Name");
        input = Console.ReadLine();
        if (input == "-99")
            break;
        else
            myList.Add(input);
    } while (true);
    
    // Here put a method to print

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Sunday, April 15, 2012 7:37 AM
  • Thank you for your help, but now I think something terrible is happening. My IDE doesnt even recognise "ArrayList"... whenever I try to use arrayList I get this error

    "Error    1    The type or namespace name 'ArrayList' could not be found (are you missing a using directive or an assembly reference?)"

    its almost as if "ArrayList" doesnt even exist in my IDE? (The intelisense doesnt even have arraylist)

    Visual Studio 2010 Professional; Has no ArrayList at all :(

    My IDE is visual studio 2010 profesional
    If you could help that would be great!

    --EDIT--
    Do I have to add something to the "Using XXX;" to be able to use ArrayList?


    Sunday, April 15, 2012 2:45 PM
  • I'd try using a List<String> instead of an ArrayList. The provide the same functionality and more, and are now flavor de jour.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Sunday, April 15, 2012 2:58 PM
  • I appreciate your help Riced, but unfortunatly my professor has limited us to only arrays :(

    Lots of things I do in that class could be done better and easier, but I must stick to the specifications of Arrays.

    However im sure that if its crunch time and I have to submit something than I would rather have a working List<string> than a broken array.So thanks!

    Anyone else want to go rounds with my question?

    Sunday, April 15, 2012 10:35 PM
  • Do you have a using System.Collections at top of program (with .Generics)? ArrayList should be defined in there. So you could use that, however, technically it is not an array. It acts like a list (i.e. grows as you add more items) whereas strictly an array has a fixed size. So a strict use of array would require knowing the number of strings to be entered before entering them.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Sunday, April 15, 2012 10:51 PM
  • [strike]Humm, you just made me realize that I could have possibly overlooked the instructions.... With this logic: "strict use of array would require knowing the number of strings to be entered before entering them." would render my project impossible. LOL thanks for saving me from an embarrassing question.[/strike]



    Monday, April 16, 2012 1:20 AM
  • If anyone is having my issue I solved this  using only Arrays.however it is not expandible so you can just use large numbers like 2million, and if the user types "stop" the loop wil break, and all the entered names will be displayed in acending order.

    Here is the code.

    string[] storeNames = new string[10000];
                string s = "\n";
    
                Console.WriteLine("Enter names:\nType \"stop\" to break the loop.\n");
    
                for (int i = 0; i < storeNames.Length; i++)
                {
                    string val;
                    val = Console.ReadLine();
                    if (val == "stop")
                    {
                        break;
                    }
                    storeNames[i] = val;
                    storeNames[i] += "\n";
                    
                }
    
    
                foreach (string num in storeNames)
                {
                    s += num;
                }
    
                Console.WriteLine("\n");
                Console.WriteLine("The names in ascending order are: {0}", s);
                Console.ReadKey();

    And here is the array that uses a list (it is expandible)

                List<string> list = new List<string>();
                //list.Add("one");
                //list.Add("two");
                //list.Add("three");
                
                Console.WriteLine("Enter as many last names as you wish\n    Type: 'stop' to end the loop.\n________________");
                string input = "";
                while (input != "stop")
                {
                    input = Console.ReadLine();
                    list.Add(input);
                }
    
    
                Console.WriteLine("----------------\n\n\nHere are all of the last names you have entered in acending order:\n_________________");
                string[] array = list.ToArray();
                //for (int i = 0; i < array.Length; i++)
                foreach (string stringValues in array)
                {
                    Console.WriteLine(stringValues);       //The foreach way
                    //Console.WriteLine(array[i]); //The for way
                }
                Console.WriteLine("----------------\nPress any key to exit...");
                Console.ReadKey();

    Wednesday, April 18, 2012 2:16 AM