Help me to find problem in my code

problem : HTMLTAGS Problem - CodeChef
my code : CodeChef: Practical coding for everyone

for my inputs it is always giving me output as Error .

s doesn’t seem to be being read in correctly: it might be the same problem as in C++, where after you read in t, the value of sc.nextLine() will just be the rest of the line containing t i.e. the empty string.

I don’t know enough about Java’s IO to be sure, though.

Edit:

This seems to work:

/* 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
	{
	    Scanner sc = new Scanner(System.in);
		int t= sc.nextInt();
        sc.nextLine(); // Skip the remainder of the line containing T.
		for(int i=0 ; i<t ;i++ )
		{
		    String s = sc.nextLine();
		    boolean b=false;
		    int n= s.length();
		    if(n>3)
		    {
		        b= ( s.charAt(0) == '<' && s.charAt(1) == '/' && s.charAt(n-1) == '>' );
		        for( int j=2 ; j< n-1 ;j++)
		        {
		            if(!(Character.isLowerCase(s.charAt(j)) || Character.isDigit(s.charAt(j))))
		              b=false;
		        }
		    }
		    if(b)
		       System.out.println("Success");
		    else
		       System.out.println("ERROR");
		}
		sc.close();
	}
}

Or maybe, the logic was incorrect here.

He wrote

for(int j = 0; j < n - 1; j++)

instead.

And btw, instead of nextLine(), he can use the next() method of Scanner Class everywhere.

Or both :slight_smile:

1 Like