c# - My text file will not read into the array -
this has been answered. relatively new c# , experiencing problem. program keeps throwing error per try catch. have looked on , changed things, nothing seems work. error being thrown on line 49 (where while loop reading text file takes place). have idea issue?? know there may other issues min/max/average, can figure out. need me fix reading of file array.
the error code is: input string not in correct format. @ system.number.stringtonumber using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.io;
namespace total_sales_bbrantley { public partial class form1 : form { public form1() { initializecomponent(); } private void btncalc_click(object sender, eventargs e) { try { // declare variable hold amount of sales // declare constant array size const int size = 100; // create array sales double[] allsales = new double[size]; // declare variable holding total value of sales double total = 0.0; // declare variable hold average double average; // declare variable hold highest value double highest = double.minvalue; // declare variable hold lowest value double lowest = double.maxvalue; int count = 0; // declare streamreader variable. streamreader readfile; // open file , streamreader object using relative path readfile = file.opentext("sales.txt"); while (!readfile.endofstream && count < allsales.length) { error error error: [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ allsales[count] = int.parse(readfile.readline()); ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] // increment count count++; } // close file readfile.close(); lstsales.items.add("the file contains " + count + " items:"); (int index = 0; index < count; index++) { lstsales.items.add(allsales[index]); } // display total double sum = allsales.sum(); lbltotal.text = sum.tostring(); total += sum; average = total / allsales.length; lblaverage.text = average.tostring(); (int index = 1; index < allsales.length; index++) { if (allsales[index] > highest) { highest = allsales[index]; } lblhighest.text = highest.tostring(); } (int index = 1; index < allsales.length; index++) { if (allsales[index] < lowest) { lowest = allsales[index]; } lbllowest.text = lowest.tostring(); } } catch (exception ex) { // display error message on bad input file messagebox.show(string.concat("error calculating sales. ", ex.message, "\r\n", ex.stacktrace)); } } private void btnexit_click(object sender, eventargs e) { //closes application this.close(); } }
}
i think issue using int.parse instead of double.parse.
Comments
Post a Comment