Home > Software engineering >  Is there any way to solve the following problem of incorporating string tokenizer in Buffered Reader
Is there any way to solve the following problem of incorporating string tokenizer in Buffered Reader

Time:06-11

Problem Statement: Chef wants to appear in a competitive exam. To take the exam, there are following requirements: Minimum age limit is X (i.e. Age should be greater than or equal to XX). Age should be strictly less than Y. Chef's current Age is A. Find whether he is currently eligible to take the exam or not.

Output format: For each test case, output YES if Chef is eligible to give the exam, NO otherwise. You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).

Constraints:

  • 1≤T≤1000
  • 20≤X<Y≤40
  • 10≤A≤50

Below is my code for age limit problem in codechef:

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

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int i,x,y,a,n = Integer.parseInt(br.readLine());
          for (i = 0; i<n ;i  )
                    {
                        StringTokenizer st = new StringTokenizer(br.readLine());
                        x =Integer.parseInt(st.toString());
                        y =Integer.parseInt(st.nextToken(" ").toString());
                        a =Integer.parseInt(st.nextToken(" ").toString());
                        if (20<= x && x<y && y<= 40 && 10 <=a && a<= 50)
                            {   
                                if (a >= x && a < y)
                                    System.out.println ("Yes");
                                else 
                                    System.out.println ("No");
                            }
                        
                    } 
            }
    }

Can anybody help me with why is there a problem in x =Integer.parseInt(st.toString());?

CodePudding user response:

Based on your question it looks like you are entering numbers with white space as delimiter.

st.toString() will give you toString() representation of StringTokenizer. If you want to read first token into x, you can simply do

x =Integer.parseInt(st.nextToken());

Optional Tip, for reading y & a, you can omit using st.nextToken(" ") & can directly use st.nextToken(), provided you don't have different delimiters in your input but only white space.

  •  Tags:  
  • java
  • Related