marketplace,\" 15Program System System Collections Generic System Linq; System.
ID: 3763704 • Letter: M
Question
marketplace," 15Program System System Collections Generic System Linq; System. Texti System. Threading.Tasks; System. 10; marketplace_flls class Program public string filelocation clusers Inatelldocumentsivisual studio 20131ProjectstMarketPlace fixestMarketPlace fixel public stringt.l Rasterlog new stringt1ee, public stringt.l Receiptlog-new stringt1e,3] public float totalspent ; public string store; public int amount- public int quantity: public string item public float wallet - 100; public string aystore; public int nuestores- : I/Total number of stores public int j-0; 31i lI Store variable for shops function II dollar value II amount of the iten you are buying //used for receipt variable static void Main(stringt) angs) //Failure: Progran main -new Progra) main.greeting); main.Shops) main.iteas) //main. shopping store(): //main.ammount ); //goto Failure; Output Show outout from Build Error List Output Search the web and Windows
Explanation / Answer
Hi There,
This kind of error is hard to find even by professors. I believe you are still student and as a student you wrote a good piece of code.
Your Error :
You declared string array of length 3 but in marketplace.txt file, in line "BooksRus 5" ; there are only two string.
So, when you split it, there will be two seperate strings : 'BooksRus' and '5'
But in picture 2,
tokens = line.split(' ');
.
.
for (int i = 0; i < Masterlog.length; i++) // this is culprit line
here, Masterlog.length is 3 (you declared it [100,3] right :p )
So, when i = 2, next line of code will try to access tokens[2] which really doesn't exist. It will throw exception which you successfully caught in catch block.
But, there you made blunder; you created an empty catch block. so it will catch the exception and do nothing afterwards. So, I believe your program is stuck at this point.
P.S. Just noticed, you used tokens[y] instead of tokens[i] ? you might got a wrong logic. Please check your programs requirements first.
Now, solution to your problem:
Its simple, instead of using Masterlog.length, use tokens.length in for loop terminating condition.
your program won't get stucked there. But for good coding practices, always catch and handle or atleast log all the errors to console by simple print statement. Its not too much to ask, is it?? :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.