1 / 2
Oct 2020

I am solving this problem and I came up with this solution but it is saying input mismatch for taking value k as an integer.
Link to the problem [https://www.spoj.com/problems/NY10A/3]

import java.util.Scanner;

public class main
{		
	    public static void main(String args[])
	    {
	    	
	    	Scanner s = new Scanner(System.in);		    	
	    	int n=s.nextInt();
	    	
	    	while(n-->0) {
	    		int k=s.nextInt();
	    		String t=s.nextLine();
	    		int ttt=0,tth=0,tht=0,thh=0,htt=0,hth=0,hht=0,hhh=0;

	    		for(int i=0;i<t.length()-2;i++) {
	    			if(t.substring(i, i+2)== "TTT") {
	    				ttt++;
	    			}
	    			else if(t.substring(i, i+2)== "TTH") {
	    				tth++;
	    			}
	    			else if(t.substring(i, i+2)== "THT") {
	    				tht++;
	    			}
	    			else if(t.substring(i, i+2)== "THH") {
	    				thh++;
	    			}
	    			else if(t.substring(i, i+2)== "HTT") {
	    				htt++;
	    			}
	    			else if(t.substring(i, i+2)== "HTH") {
	    				hth++;
	    			}
	    			else if(t.substring(i, i+2)== "HHT") {
	    				hht++;
	    			}
	    			else if(t.substring(i, i+2)== "HHH") {
	    				hhh++;
	    			}
	    		}
	    		
	    		System.out.println(k+ " " + ttt + " " + tth + " " + tht + " " + thh + " " + htt + " " + hth + " " + hht + " " + hhh);
	    		

	    	}
   		
		 }	    		
}
  • created

    Oct '20
  • last reply

    Oct '20
  • 1

    reply

  • 570

    views

  • 2

    users

  • 1

    link

When I print the length of t after reading it, it’s always 0.

I think t is being assigned the remainder of the same line after reading k. If I add another call to s.nextLine() before reading t, the error doesn’t occur.