Help me in solving HCAGMAM1 problem

My issue

I am getting a run time error as follows, please help:)

Exception in thread “main” java.util.InputMismatchException: For input string: “111100110101100000101100011111”
at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Codechef.main(Main.java:17)

My code

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		int t;
		Scanner sc=new Scanner(System.in);
		t=sc.nextInt();
		for(int i=0;i<t;i++){
		    int x=sc.nextInt();
		    int y=sc.nextInt();
		    String s=sc.nextLine();
		    int curr_streak=0,long_streak=0,work_days=0,salary;
		    for(int j=0;j<s.length();j++){
		        if(s.charAt(j)=='1'){
		            curr_streak++;
		            work_days++;
		        }
		        else{
		            if(curr_streak>long_streak) long_streak=curr_streak;
		            curr_streak=0;
		        }
		    }
		    salary=(x*work_days)+(y*long_streak);
		    System.out.println(salary);
		}
	}
}

Problem Link: HCAGMAM1 Problem - CodeChef

@admiral_c
plzz refer the following java code for better understanding.

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-->0){
		    int x = sc.nextInt();
		    int y = sc.nextInt();
		    String s = sc.next();
		    int count=0,sum=0;
		    int streak=0;
		    int max=0;
		    for(int i=0;i<30;i++){
		        if(s.charAt(i)=='1'){count++;streak++;}
		        else streak=0;
		        if(streak>max){max=streak;}
		    }
		    sum=(max*y) +(count*x);
		    System.out.println(sum);
		}
	
	}
}
1 Like