3 Replies - 193 Views - Last Post: 14 May 2012 - 08:15 PM Rate Topic: -----

#1 solidtuber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-March 12

Need assistant on finishing off a code.

Posted 14 May 2012 - 06:56 PM

Hey I need help finishing off my code. I've done most of the code just need help doing a few things.

First is printing out percentage as well with the counted letters

[codeimport java.util.*;

public class CountArrayLetters {
public static void main(String[] args) {

//this is get results
char[] chars = userEnters();

System.out.println();
System.out.println("Occurrences of each letter are:");
System.out.println("-----------------------------------------------------------");
PrintArray(countLetters(chars));
}

public static char[] userEnters() {
Scanner input = new Scanner(System.in);
System.out.println("Enter line of text: ");
String inputX = input.nextLine();
char[] chars = inputX.toCharArray();

return chars;
}

public static int[] countLetters(char[] input) {
int[] counts = new int[26];
for (int i = 0; i < input.length; i++) {
counts[input[i] - 'a']++;
}
return counts;
}

public static void PrintArray(int[] counts) {
for (int i = 0; i < counts.length; i++) {
if ((i + 1) % 10 == 0)

System.out.println( "\n" + counts[i] + " " + (char) ('a' + i ));
else
System.out.print( "\n" + counts[i] + " " + (char) ('a' + i) + " ");

}




}
}][/code]

import java.util.*;

public class CountArrayLetters {
    public static void main(String[] args) {
        
        //this is get results
        char[] chars = userEnters();
        
        System.out.println();
        System.out.println("Occurrences of each letter are:");
        System.out.println("-----------------------------------------------------------");
        PrintArray(countLetters(chars));
    }

    public static char[] userEnters() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter line of text: ");
        String inputX = input.nextLine();
        char[] chars = inputX.toCharArray();

        return chars;
    }

    public static int[] countLetters(char[] input) {
        int[] counts = new int[26];
        for (int i = 0; i < input.length; i++) {
            counts[input[i] - 'a']++;
        }
        return counts;
    }

    public static void PrintArray(int[] counts) {
        for (int i = 0; i < counts.length; i++) {
            if ((i + 1) % 10 == 0)
               
            System.out.println( "\n" +    counts[i] + "    " + (char) ('a' + i ));
            else
                System.out.print( "\n" +    counts[i] + "    " + (char) ('a' + i) + "   ");

        }




    }
}


My bad, i also need help when the user inputs data, i need to make it so the code will account for spacebar keystrokes. Get java.lang.array.indexoutofboundsexeception: -65 when i use spacebars

Is This A Good Question/Topic? 0
  • +

Replies To: Need assistant on finishing off a code.

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: Need assistant on finishing off a code.

Posted 14 May 2012 - 06:58 PM

You want your code to anticipate the spaces and not count them off?
Why not use the String method trim to get rid of the whitespaces then feed them into your arrays?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,361
  • Joined: 02-June 10

Re: Need assistant on finishing off a code.

Posted 14 May 2012 - 07:08 PM

IndexOutOfBounds - You have a list of say 10 items and try to access item number 12 or -2. Those values are out of the bounds of the collection.
Put a breakpoint at the offending lines and double check all the values: Sizze of the collection, starting index, length of substring, that sort of thing. Don't forget arrays are 0 indexed, so element 1 is index 0, element 2 is index 1 and so on.


What does "I need help" and "I'm having trouble" mean?
  • You are getting an error? What does this error mean?
  • Your code isn't doing what you expect it to?
  • you want someone to design your algorithm for you because you can't think of a way it should work?


I don't want you to write my code, just give me ideas on how to solve my problem. - Read this article

You've only described the job requirement, not anything about what you are actually doing to accomplish it.
Was This Post Helpful? 1
  • +
  • -

#4 solidtuber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-March 12

Re: Need assistant on finishing off a code.

Posted 14 May 2012 - 08:15 PM

View PostfromTheSprawl, on 15 May 2012 - 09:58 AM, said:

You want your code to anticipate the spaces and not count them off?
Why not use the String method trim to get rid of the whitespaces then feed them into your arrays?

I'll try that, now just prints the percentages of occurrence of each letter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1