Answered Adding numbers to an Array and sorting them

WB1975

Well-known member
Joined
Apr 3, 2020
Messages
87
Programming Experience
Beginner
Just doing an exercise,

Adding numbers to an array and then sorting the list.

im trying to determine if input from a user is a number or a letter for my options, but i think im over complicating it.

Am i on the right track here or is there an easy way to do this?

The problem im having is that if i enter a letter first it works perfectly and prompts again to enter a number so:
s2
s34 asks for a number

f exits the program

but 2as 23d etc will be added to the array, seems like its getting complicated at this stage, thought I would check with you guys.

Thanks for reading


i have 2 classes
Program
Array

C#:
using System;

namespace SortArrayUp
{
    class Program
    {
        static void Main(string[] args)
        {
            Array myArrayClass = new Array();

            myArrayClass.enterNumber();
            Console.WriteLine("Thanks for using, see you soon");
        }
    }
}



C#:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Text;

namespace SortArrayUp
{
    class Array
    {
        public ArrayList theArray = new ArrayList();
        public string userInput;
        public bool finishedEnteringNumbers = false;

        public void enterNumber()
        {
            while (!finishedEnteringNumbers)
            {
                Console.WriteLine("Enter a number to add to the list or type (f) to finish:");
                userInput = Console.ReadLine();

                char firstChar = userInput[0];
                bool isNumber = Char.IsDigit(firstChar);

                if (!isNumber)
                {
                    if (firstChar == 'f')
                    {
                        if (theArray.Count <= 2)
                        {
                            Console.WriteLine("There is no numbers in the list, you need at least 3...");
                        }
                        else finishedEnteringNumbers = true;
                    }
                    else
                    {
                        Console.WriteLine("Not an integer");
                    }   
                }
                else
                {
                    string userNumber = userInput.Substring(0, userInput.Length);
                    Console.WriteLine(userNumber);

                    
                    Console.WriteLine(isNumber + " was successfully added to the list...");
                    
                }
            }
        }
    }
}
 
The primary problem is line 22-23 where you are only checking the first character.

A better approach would be to use int.TryParse(). If it returns true, then the user entered a valid integer. If it returns false what the user entered was not an integer. Only then should you check if the user entered an 'f' to stop entering values.
 
The next problem is that using the antiquated (and non-typesafe) ArrayList. ArrayList was originally created to ease Java programmers into C#, as well as, get the .NET Framework off the ground. It is not particularly efficient because it uses an internal array of objects and so you incur all of the boxing and unboxing overhead for value types, but it is very convenient when you need an "array" that grows in size. Very quickly after that Microsoft introduced generics and the List<T>. Internally, it still uses an array, but uses an array of T. The List<T> can grow dynamically like the ArrayList, but it is type safe as well as efficient because you don't incur the boxing/unboxing costs for value types.

So for your assingment, you should use List<int>. If you really must have an array, once you have populated the list, you can call ToArray() on the list to get back a copy of the internal array used by the list. But if you don't need an array, you can also index into the list using the [] operators just like you can with the ArrayList.
 
Thanks for your help, really appreciate it!!

Although basic, is this an OOP program?

Please let me know if there is anything that can be changed or made better...

Program Class

C#:
using SortListUp;
using System;

namespace SortArrayUp
{
    class Program
    {
        static void Main(string[] args)
        {
            List myList = new List();

            myList.EnterNumber();
            myList.SortAndPrintList();
            Console.WriteLine("Thanks for using, see you soon");
        }
    }
}

List Class

C#:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Text;

namespace SortListUp
{
    class List
    {
        public List<int> numberList = new List<int>();
        public int userNumberToAdd;
        public string userInput;
        public bool finishedEnteringNumbers = false;

        public void EnterNumber()
        {
            while (!finishedEnteringNumbers)
            {
                Console.WriteLine("Enter a number to add to the list or type (f) to finish:");
                userInput = Console.ReadLine();
                bool isNumeric = int.TryParse(userInput, out userNumberToAdd);
                if (isNumeric)
                {
                    numberList.Add(userNumberToAdd);
                } else
                {
                    if (userInput=="f")
                    {
                        finishedEnteringNumbers = true;
                    }
                    else
                    {
                        Console.WriteLine("That is not a valid number...");
                    }
                }
            }
        }

        public void SortAndPrintList()
        {
            numberList.Sort();
            int num = 1;
            int numTotal = 0;
            Console.WriteLine("I will now sort your list from smallest to largest for you:");
            foreach (int i in numberList)
            {
                Console.WriteLine("Number " + num + " was " + i);
                numTotal++;
                num++;
            }
            Console.WriteLine("You entered a total of " + numTotal + " numbers.");
        }
    }
}
 
A class named List should be a list. Your class should be named something more like ListManager. The EnterNumber method is poorly named and should probably be PopulateList. There should also be separate SortList and DisplayList methods.
 
A class named List should be a list. Your class should be named something more like ListManager. The EnterNumber method is poorly named and should probably be PopulateList. There should also be separate SortList and DisplayList methods.
ok will do thanks
 
Back
Top Bottom