0

i am trying to solve this question https://www.hackerrank.com/challenges/pangrams and here's my code

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Pangram {

public static String alltrim(String s)
{   String s1="";
    int i,j,k=0;
    for(i=0;i<s.length();i++)
    {
        char ch=s.charAt(i);
        if(ch!=' ')
        break;
    }
    for(j=s.length()-1;j>=0;j--)
    {
        char ch=s.charAt(j);
        if(ch!=' ')
        break;
    }
    for(k=i;k<j+1;k++) 
     s1 = s1 + s.charAt(k);

 return s1;
}
public static void main(String[] args)
{
    Scanner reader = new Scanner(System.in);
    String input = reader.nextLine();String s,s1;
    s = input.toLowerCase();
    s1 = alltrim(s);
    if( check(s1) )
        System.out.println("pangram");
    else
        System.out.println("not pangram");
}
public static boolean check(String input)
{
    int [] count = new int[26];
    for( int i = 0; i < input.length(); i++ )
    {
        char ch = input.charAt(i);
        count[ch-'a']++;
    }

    for( int cnt:count )
    {
        if( cnt ==0 )
            return false;
    }
    return true;
 }
}

i always get this error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -65
      at Pangram.check(Pangram.java:46)
      at Pangram.main(Pangram.java:35)

i am trying to find out where the problem is but i coudn't ... can anyone help me?

fabian
  • 80,457
  • 12
  • 86
  • 114
coder101
  • 840
  • 2
  • 11
  • 29
  • Well have you looked at Pangram.java line 46 and worked out why you're trying to access an invalid value? Have you tried debugging? Hint: your `alltrim` method doesn't actually remove all the space. You might want use `s1 = s.replace(" ", "");`... – Jon Skeet Mar 21 '15 at 16:18
  • @JonSkeet thanks for that replace function even after using replace function it is showing a similar problem Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -43 at Pangram.check(Pangram.java:26) at Pangram.main(Pangram.java:15) ------> line 26 : count[ch-'a']++; -------> line 15 : if( check(s1) ) anything wrong with them? – coder101 Mar 21 '15 at 16:22
  • It would also help to know which value you entered to encounter the out of bound exception. – Jean-François Savard Mar 21 '15 at 16:26
  • Also, 4 of your 5 imports are not needed, you only use `java.util`. – Jean-François Savard Mar 21 '15 at 16:27
  • input : 6 aaabbb ab abc mnop xyyx xaxbbbxx @Jean-FrançoisSavard – coder101 Mar 21 '15 at 16:27
  • 1
    int i,j,k=0; Unnecessary. Do not need to declare them outside the loops as you are already declaring the required variables at the beginning of each for loop. – Jude Niroshan Mar 21 '15 at 16:27
  • 1
    @coder101: Well again, have you tried debugging? It's *vital* that you learn how to diagnose problems yourself. – Jon Skeet Mar 21 '15 at 16:30
  • yes i do agree that it is important , i am actually coding in hackerrank and i do not have ide on this lappy , so i couldn't debug @JonSkeet – coder101 Mar 21 '15 at 16:32
  • You can debug without IDE, answerer on StackOverflow always do this and they don't have magical powers. – Jean-François Savard Mar 21 '15 at 16:36
  • @coder101: So you add logging statements, or you wait until you're in a place where you *can* debug. It's disrespectful to ask others to spend time just because you can't be bothered to wait until you've got access to a debugger, or add appropriate logging. – Jon Skeet Mar 21 '15 at 19:00
  • @coder101 Any feedback on this ? – Jean-François Savard Mar 23 '15 at 17:53
  • i changed my code and reduced a lot of steps and it passed all the test cases , thanks to everyone for their responses and to your valuable time , i gotta better idea regarding out of bounds exception and also replace function came in handy – coder101 Mar 24 '15 at 20:26

3 Answers3

1

Here's one way

    public static void main(final String[] args) throws IOException {
         Scanner reader = new Scanner(System.in);
         String line = reader.nextLine();
            // Verify input argument length.

            final char[] allCharacters = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

            
                    final TreeSet<Character> charactersPresent = new TreeSet<Character>();

                    // Parse the line into letters.
                    final char[] letters = line.toLowerCase().toCharArray();

                    // Keep a set of those that are present.
                    for (final char letter : letters) {
                            charactersPresent.add(letter);
                    }

                    // Print the letters present in the alphabet but not in the input.
                    final StringBuilder missingLettersBuilder = new StringBuilder();
                    for (final char character : allCharacters) {
                            if (!charactersPresent.contains(character)) {
                                    missingLettersBuilder.append(character);
                            }
                    }

                    if (missingLettersBuilder.length() == 0) {
                            System.out.println("This is  a PANGRAM");
                    } else {
                            System.out.println("Not a PANGRAM because it doesn't have "+missingLettersBuilder.toString());
                    }
            }
Community
  • 1
  • 1
V33R
  • 909
  • 8
  • 10
1

The problem is in

count[ch-'a']++;

where ch is 6(as you entered) and you are subtracting '6'-'1' so its generating a value by subtracting the ASCII values and give

count[-43]++;
singhakash
  • 7,891
  • 6
  • 31
  • 65
1

The java.lang.ArrayIndexOutOfBoundsException: -65 happen because you subtract a(97) to a space character which is represented by 32 in the ASCII table (32-97=-65).

Which mean that the problem is in your allTrim method which does not replace all spaces as you probably expected.

If you want to replace all spaces of your string, you can use the replaceAll method of String.

Discard the s1 = alltrim(s); line and replace it by s1 = s.replaceAll(" ", "");.

Now entering the input you gave me in comments

aaabbb ab abc mnop xyyx xaxbbbxx

Won't give any exception.

As for the other exception you had (out of bound -43), it is simply because you entered

6 aaabbb ab abc mnop xyyx xaxbbbxx

6 is represented by 54 in the ASCII table, 54-97 = 43. Looking at the requirement on the link you gave us, you should only compare alphabetical letter so I suppose this is a mistake.

Note that you can easily remove all number of a String, your replace line would become :

s1 = s.replaceAll(" ", "").replaceAll("\\d", "");

Now it would not give any error even when entering

6 aaabbb ab abc mnop xyyx xaxbbbxx

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76