Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Where the hell is the error coming from. . . ?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Where the hell is the error coming from. . . ?

    PHP Code:
    import java.util.Scanner;
    public class 
    CharOccurence {
      public static 
    void main(String[] arg){
        
    Scanner input = new Scanner(System.in);
        
        
    System.out.println("Enter a sentence to count the occurence of each letter");
        
    String words=input.nextLine();
        
        
    int[] count letterOccurence(words.toLowerCase());
        
        for(
    int i=0;i<words.length();i++){
          if(
    count[words.charAt(i) - 'a'] == 1)
              
    System.out.println(words.charAt(i)+ " occurence: "count[words.charAt(i) - 'a']+ " time");
          else
              
    System.out.println(words.charAt(i)+ " occurence: "count[words.charAt(i) - 'a']+ " times");
        }
        
        
     }
        
        
        public static 
    int[] letterOccurence(String sentence){
            
    int[] count = new int[26];
            
            for(
    int i=0;i<sentence.length();i++){
                if(
    Character.isLetter(sentence.charAt(i)))
                    
    count[sentence.charAt(i) - 'a']++;
            }
            
          return 
    count;
        }
     

    Last edited by Yunus Einsteinium; July 13th, 2012 at 11:17 AM.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Where the hell is the error coming from. . . ?

    man, Einsteinium, you commit a basic error and wonder why you're killed the poison is
    int[] count = letterOccurence(words.toLowerCase());
    for(int i=0;i<words.length();i++){
    ..
    }
    to detox
    int[] count = letterOccurence(words.toLowerCase());
    for(int i=0;i<count.length;i++){
    ..
    }

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Where the hell is the error coming from. . . ?

    man, when i do that another error pops out instructing me to change it back to length()

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Where the hell is the error coming from. . . ?

    Quote Originally Posted by Yunus Einsteinium View Post
    PHP Code:
    import java.util.Scanner;
    public class 
    CharOccurence {
      public static 
    void main(String[] arg){
        
    Scanner input = new Scanner(System.in);
        
        
    System.out.println("Enter a sentence to count the occurence of each letter");
        
    String words=input.nextLine();
        
        
    int[] count letterOccurence(words.toLowerCase());
        
        for(
    int i=0;i<words.length();i++){
          if(
    count[words.charAt(i) - 'a'] == 1)
              
    System.out.println(words.charAt(i)+ " occurence: "count[words.charAt(i) - 'a']+ " time");
          else
              
    System.out.println(words.charAt(i)+ " occurence: "count[words.charAt(i) - 'a']+ " times");
        }
        
        
     }
        
        
        public static 
    int[] letterOccurence(String sentence){
            
    int[] count = new int[26];
            
            for(
    int i=0;i<sentence.length();i++){
                if(
    Character.isLetter(sentence.charAt(i)))
                    
    count[sentence.charAt(i) - 'a']++;
            }
            
          return 
    count;
        }
     

    Hello Yunus Einsteinium!
    For future posts, it would help if you explained what are you trying to do with your code and posted the full error messages and the code that produces them.
    I tried your code and it causes an ArrayIndexOutOfBoundsException. This happens when you enter a sentence that has characters which are NOT letters.
    You can avoid that by checking every character (in the for loop) and see whether it is a letter or not.
    Hope this helps.

  5. The Following User Says Thank You to andreas90 For This Useful Post:

    Yunus Einsteinium (July 13th, 2012)

  6. #5
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Where the hell is the error coming from. . . ?

    Einsteinium, your 2nd poison vial is:
     
            for(int i=0;i<sentence.length();i++){
                if(Character.isLetter(sentence.charAt(i)))
                    count[sentence.charAt(i) - 'a']++;
            }
    Where? the term sentence.charAt(i) - 'a' can be bigger than 26 or a negative value if sentence.charAt(i) is an uppercase character A, B,...
    to detox:

            String tmp = sentence.toLowerCase();
            for(int i=0;i<sentence.length();i++){
                if(Character.isLetter(sentence.charAt(i)))
                    count[tmp.charAt(i) - 'a']++;
            }

  7. #6
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Where the hell is the error coming from. . . ?

    and the 3rd poison bottle you have is:
     
                Scanner input = new Scanner(System.in);
     
                System.out.println("Enter a sentence to count the occurence of each letter");
                String words=input.nextLine();
    	    int[] count = letterOccurence(words.toLowerCase());
     
    	    for(int i=0;i<count.length;i++){
    	      if(count[words.charAt(i) - 'a'] == 1)
    		  System.out.println(words.charAt(i)+ " occurence: "+ count[words.charAt(i) - 'a']+ " time");
    	      else
    		  System.out.println(words.charAt(i)+ " occurence: "+ count[words.charAt(i) - 'a']+ " times");
    	    }
    What happens if words == null ?
    to detox
     
        Scanner input = new Scanner(System.in);
     
        System.out.println("Enter a sentence to count the occurence of each letter");
        String words=input.nextLine();
        if (words != null) {
    	    int[] count = letterOccurence(words.toLowerCase());
     
    	    for(int i=0;i<count.length;i++){
    	      if(count[words.charAt(i) - 'a'] == 1)
    		  System.out.println(words.charAt(i)+ " occurence: "+ count[words.charAt(i) - 'a']+ " time");
    	      else
    		  System.out.println(words.charAt(i)+ " occurence: "+ count[words.charAt(i) - 'a']+ " times");
    	    }
     
        }

Similar Threads

  1. Not sure where the error is coming from
    By Duke_fann in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 23rd, 2011, 04:21 AM
  2. JFrames not coming up?
    By scooty199 in forum AWT / Java Swing
    Replies: 13
    Last Post: October 30th, 2010, 02:54 AM
  3. Replies: 1
    Last Post: October 16th, 2010, 03:32 PM
  4. [SOLVED] SQLServer Hell
    By xyrer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 21st, 2010, 04:46 PM
  5. Hell world :)
    By Kassino in forum Member Introductions
    Replies: 2
    Last Post: May 10th, 2010, 08:48 AM