Skip to content

Number app #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 84 additions & 3 deletions Session03/Session03/Session03Excercise02/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Diagnostics;
using System.Globalization;

namespace Session03Excercise02
{
Expand All @@ -8,13 +10,92 @@ static void Main(string[] args)
{
Console.WriteLine("Ange ett antal siffror, separerat med kommatecken.");
var input = Console.ReadLine();
var inputArray = input.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var inputArray = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
double?[] numberArray = new double?[inputArray.Length];

foreach (var number in inputArray)
//for (int i = 0; i < inputArray.Length; i++)
//{
// try
// {
// numberArray[i] = Convert.ToDouble(inputArray[i]);
// }
// catch (Exception)
// {
// numberArray[i] = 0;
// }
//}

for (int i = 0; i < inputArray.Length; i++)
{
NumberStyles numberStyle = NumberStyles.Integer | NumberStyles.Float;
IFormatProvider formatProvider = CultureInfo.InvariantCulture;

bool parsed = double.TryParse(inputArray[i], numberStyle, formatProvider, out double parsedValue);

if (parsed == true)
{
numberArray[i] = parsedValue;
}
else
{
numberArray[i] = null;
}

//numberArray[i] = GetDoubleValue(inputArray[i]);
}

//foreach (var number in numberArray)
//{
// Console.WriteLine("Värde: " + number.ToString());
//}

//int exceptionStatus = GetExceptionStatus();

}

// Metod som returnerar en siffra baserat på en sträng
static double GetDoubleValue(string input)
{
try
{
var result = int.Parse(input);

return result;
}
catch (FormatException ex) when (ex.Message.Contains("Input string"))
{
return double.MinValue;
}
catch (Exception ex)
{
return 0;
}

}

// Metod som returnerar ett heltal, saknar inparameter
static int GetExceptionStatus()
{
int exceptionResult;

try
{
Console.WriteLine("Värdet är " + number);
exceptionResult = -1;

throw new Exception("Provocerat fel");

return exceptionResult;
}
catch (Exception ex)
{
exceptionResult = ex.HResult;

return exceptionResult;
}
finally
{
exceptionResult = int.MaxValue;
}
}
}
}