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
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...");
}
}
}
}
}