why this loop infinite loop ??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why this loop infinite loop ???

case "TARTUP": intial = new File("intialInformation.txt"); info = new Scanner(intial); coursesNUM = info.nextInt(); // 3 sectionsNUM = info.nextInt(); // 3 studentsNUM = info.nextInt(); // 17 coursesLine = info.next(); System.out.print("The courses for this semester are:"); for (int i = 0; i < coursesNUM; i++) { System.out.print(" " +coursesLine); } System.out.println("\n----------------------------------------------------------\n"); https://code.sololearn.com/c7t5OaStWdoA/?ref=app

4th Oct 2020, 10:38 AM
SH S
SH S - avatar
7 Answers
0
It shouldn't be infinite. Did you make sure that your coursesNUM is coming through as 3 just before the for-loop starts? I would add this line and confirm it says coursesNUM = 3. coursesLine = info.next(); System.out.println("coursesNUM = " + coursesNUM); /**** ADD THIS ***/ System.out.print("The courses for this semester are:"); I strongly suspect your coursesNUM is far larger than 3 or the problem is outside of your snippet. One unrelated weird thing about your code is that your loop will repeatedly print the same coursesLine. I don't know how your initialInformation.txt file is formatted but you probably want information on multiple different courses. In other words, you'll do something like coursesLine = info.next() in a loop instead of before the loop.
4th Oct 2020, 2:24 PM
Josh Greig
Josh Greig - avatar
0
Josh Greig i am sure it is 3 and i add the full code you can see it
4th Oct 2020, 2:32 PM
SH S
SH S - avatar
0
SH S, your full compile-ready code can't start with break; Also, can you share all of your txt files so I can reproduce the issue? If you share your email address here, I'll email you and you can reply with a zip of all your code and files.
4th Oct 2020, 3:05 PM
Josh Greig
Josh Greig - avatar
0
Josh Greig shai00as@gmail.com
4th Oct 2020, 3:17 PM
SH S
SH S - avatar
0
I emailed you and tried to add you on gmail's chat.
4th Oct 2020, 3:21 PM
Josh Greig
Josh Greig - avatar
0
You're correct that coursesNUM is reading as 3. You're incorrect in thinking the pasted for-loop is infinite. You should fix how your loop prints the same coursesLine 3 times like below, though: System.out.print("The courses for this semester are:"); for (int i = 0; i < coursesNUM; i++) { coursesLine = info.next(); /**************** MOVE HERE **/ System.out.print(" " +coursesLine); } Your infinite loop is the while-loop that reads commands. You read 1 command before entering your command-reading loop. Since you never read another command, your loop never ends. You should read a new command with each iteration. Here is a snippet from the corrected code in MainProgram.java. Scanner input = new Scanner(commandfile); while (input.hasNext()) { String Command = input.next(); /********** MOVE HERE ***/ switch (Command) { I emailed the updated MainProgram.java also to save time implementing the edits.
4th Oct 2020, 3:40 PM
Josh Greig
Josh Greig - avatar
0
Josh Greig Now I understand, thank you so much.
4th Oct 2020, 5:08 PM
SH S
SH S - avatar