C#
I have issues with the problem on the pic, when I put the code on the judge system it passed all tests but when I tried to run it on visual studio the compiler says this: System.FormatException: 'Input string was not in a correct format.' for the line: decimal[] prices = Array.ConvertAll(Console.ReadLine().Split(), decimal.Parse);
This is the whole code: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace zadacha5
{
class Program
{
static void Main(string[] args)
{
string[] products = Console.ReadLine().Split();
long[] quantities = Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
decimal[] prices = Array.ConvertAll(Console.ReadLine().Split(), decimal.Parse);
string productName = Console.ReadLine();
while (productName != "done")
{
int index = Array.IndexOf(products, productName);
Console.WriteLine("{0} costs: {1}; Available quantity: {2}", productName, prices[index], quantities[index]);
productName = Console.ReadLine();
}
}
}
}
19 Replies
What does your input look like?
Bread Juice Fruits Lemons
10 50 20 30
2.34 1.23 3.42 1.50
Bread
Juice
done
So it fails on when inputting this line
2.34 1.23 3.42 1.50
Right?yes
Issue is that
Parse
uses the current culture by defaultAdding to what Thinker said, make sure to trim your input
Also oh god please don't use
Array.ConvertAll
You might have a trailing space
This is my first time seeing it used actually 😅
And also trim input to ensure trailing spaces are removed
it still shows the same problem
@🌈 Thinker 🌈 @Kouhai
You need to change Thinker's code to use decimal instead of long
That it can't convert?
yes
Did you modify it to use decimal.Parse?
yes
Did you remove leading and trailing spaces?
Console.ReadLine().Trim()
yes
Okay, read the line and assign it to a variable
Place a breakpoint and examine the string