C
C#7mo ago
js

invalid index error which i dont understand

string temp = selectionItems[selection-1].ToString();
string name = temp.Substring(0, temp.IndexOf(","));
string toppings = temp.Substring(temp.IndexOf(","), temp.IndexOf("£"));
string[] toppingsArray = toppings.Split(", ");
List<string> toppingsList = toppingsArray.ToList();
toppingsList.RemoveAt(0);
Console.WriteLine(selectionItems[selection-1]);
Console.WriteLine(temp.IndexOf("£"));
Console.WriteLine(temp.Length);
string stringPrice = temp.Substring(temp.IndexOf("£"), temp.Length-1);
Console.WriteLine(stringPrice);
string pricetemp = temp.Substring(temp.IndexOf("£", temp.Length-1));
pricetemp = pricetemp.Remove(pricetemp.IndexOf("£"));
pricetemp = pricetemp.Remove(pricetemp.IndexOf("."));
int intPrice = int.Parse(pricetemp);
Pizza pizza = new Pizza(name, toppingsList, intPrice, stringPrice);
string temp = selectionItems[selection-1].ToString();
string name = temp.Substring(0, temp.IndexOf(","));
string toppings = temp.Substring(temp.IndexOf(","), temp.IndexOf("£"));
string[] toppingsArray = toppings.Split(", ");
List<string> toppingsList = toppingsArray.ToList();
toppingsList.RemoveAt(0);
Console.WriteLine(selectionItems[selection-1]);
Console.WriteLine(temp.IndexOf("£"));
Console.WriteLine(temp.Length);
string stringPrice = temp.Substring(temp.IndexOf("£"), temp.Length-1);
Console.WriteLine(stringPrice);
string pricetemp = temp.Substring(temp.IndexOf("£", temp.Length-1));
pricetemp = pricetemp.Remove(pricetemp.IndexOf("£"));
pricetemp = pricetemp.Remove(pricetemp.IndexOf("."));
int intPrice = int.Parse(pricetemp);
Pizza pizza = new Pizza(name, toppingsList, intPrice, stringPrice);
No description
4 Replies
js
jsOP7mo ago
line for error is string toppings = temp.Substring(temp.IndexOf(","), temp.IndexOf("£")); its saying my indexes for the substring are invalid this is the temp variable Margherita, tomato sauce, cheese £5.00 im trying to take a substring from temp.IndexOf(",") = 33 temp.IndexOf("£") = 38
js
jsOP7mo ago
selectionItems is a list where each of these have been added as separate items
No description
Kringe
Kringe7mo ago
IndexOf returns the first occurrence of a char in your case '','' for this 10 and for "£" 33 substring takes the startindex and length so when your string is 38chars long it results into a OutOfRangeException Idk what you exactly want you want all the toppings or only the last one
zzz
zzz7mo ago
yh i thought it was 1st value index to 2nd value index and substring is characters between only just realised it was starting index and length lol
Want results from more Discord servers?
Add your server