Java Substring

  • + 6 comments

    Java solution

    import java.util.Scanner;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Read input */
            Scanner scan = new Scanner(System.in);
            String s  = scan.next();
            int start = scan.nextInt();
            int end   = scan.nextInt();
            scan.close();
            
            System.out.println(s.substring(start, end));
        }
    }
    

    From my HackerRank Java solutions.

    • + 3 comments

      you did not add any of the constraints mentioned in the problem statement. if this code worked then the test cases are very poor.

      • + 1 comment

        Hi. This code passes all HackerRank tests. I think you may be misunderstanding what the constraints are for. You have to make your code work on any values it is given that fall within the constraints. That does not mean you have to specifically add the constraints to the problem.

        Hope this helps.

        HackerRank solutions.

        • [deleted]
          + 0 comments

          No RodneyShag, The algorithm must efficiently works in unexpected conditions. I mean is we can't check just run our codes on correct values. We MUST check the incorrect values and handle them.

      • + 0 comments

        no man ...all hackerrank testcases test your code flexibility and are not at all poor...if you dont beleive me download test cases and analyze it..if you did something wrong in your code..it will help a lot to debug your code

      • [deleted]
        + 1 comment

        I agree with you. I checked the constraints cases and my code failed on two tests but if I don't check constraints the code will be passed all test cases. This challenge is suck 🤪

        • + 0 comments

          Agree with you

    • + 1 comment

      What is the purpose of scan.close()?

      • + 0 comments
        Scanner scan = new Scanner(System.in);
        

        opens a Scanner to read from System.in. This takes resources. scan.close() closes these resources when we are done with them, which is good practice.

        HackerRank solutions.

    • + 0 comments
         System.out.println(S.substring(start, end));
      
    • + 0 comments

      can anybody plz let me known that why we have used scan.next and not scan.nextline in the above code

    • + 0 comments

      All the above codes including yours also are throwing exception