+ 2

Receiving exceptions in this please help, the ans is correct and even the logic is correct

import java.util.Scanner; public class solver{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int t= sc.nextInt(); //t*=t; while(t>0){ int length= sc.nextInt(); int num= sc.nextInt(); int distanceCovered=0; while(num>0){ int d =sc.nextInt(); distanceCovered+= d; String direction= sc.next(); num--; /** if(direction =="c"){ continue; }else{ break; } */ } int machineLaps= distanceCovered / length ; System.out.println(t +":"+ machineLaps ); //System.out.println(distanceCovered); //System.out.println(machineLaps ); } } } https://code.sololearn.com/cCV60H9OEtJq/?ref=app

12th Nov 2022, 6:00 AM
Yoo Jas
Yoo Jas - avatar
3 Answers
+ 5
while(t > 0) { What is the stop condition for this loop? You are not updating t value so it's infinite loop ask infinite inputs. Raise exception on no input..
12th Nov 2022, 11:08 AM
Jayakrishna 🇮🇳
+ 3
Use try-catch block maybe then it will work fine
12th Nov 2022, 6:05 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 2
Jayakrishna🇮🇳 thank you so much for helping me out
12th Nov 2022, 1:15 PM
Yoo Jas
Yoo Jas - avatar