Frequency count

Sorry for posting again, but more people would see my post if it is new rather than old.

I am to write an application program to prompt the user for text and displays the frequency that each number appears in the text.

- I need to use a method to convery the entire text into lower-case prior to counting the letters.

- If the frequency count is 0, do not display it.

I am able to start to program and all, able to get the prompt and stuff, but how would I convert the letters of text into lower-case?

[536 byte] By [Toneza] at [2007-11-27 0:04:04]
# 1
check out the java.lang.String class and it's methods...wait for it...it's coming...public String toLowerCase()
MaxxDmga at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 2
> Sorry for posting again, but more people would see my> post if it is new rather than old. Why do you think many people are necessary to solve that issue?The API alone can to the trick.String#toLowerCase()
Wildcard82a at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 3
Okay... how do I store this into an array? Someone will input some text using a JOptionPane dialog. How would I store the string into an array without knowing how many characters there are?
Toneza at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 4

Are you going to be storing each character of the string into an array?

If so, then you can use the .length(); method for the string. This will return the size of the string.

//Pseudo Code

char[] arr = new char[stringName.length()];

or

char[] arr = stringName.toCharArray();

API is your friend !

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

lethalwirea at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 5

import javax.swing.*;

public String toLowerCase(str);

public static void main(String[] args) {

String str;

char[] amount;

str = JOptionPane.showInputDialog("Type some words!");

amount = new char[str.length()];

I am unsure where to put the toLowerCase method. And how do I count the amount of each letter in the string?

The output shoudl look like:

"This is a test for the TextLetterCount application"

a: 3

c: 2

e: 5

f: 1

h: 2

i: 4

l: 2

n: 2

o: 3

p: 2

r: 2

s: 3

t: 10

u: 1

x: 1

Toneza at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 6
well, I know that I have to loop it looking for each individual letter but don't know how. And once it does start looking, how would it keep track of the amount of particular letters? This is just confusing...
Toneza at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 7
can anybody help me with declaring this array and storing the amount of each letter within the string? please
Toneza at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 8

If you want to count the number of letters in a string then you can use an int array of length 26 (one element per letter). Then iterate over the String and get each letter one at a time. For each letter increment the matching element in the array.

How will you know which element in the array? The letter a has an ascii value of 97 and the letter z has the value of 122. Guess what 97 -97 equals. Guess what 122 - 97 equals.

floundera at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 9

In order to add each value to the array you'd need

Pseudo:

int i = 0;

// This stores all the values from string into the array.

while (i < lengthOfString ) {

amount[i] = str.charAt(i).toLowerCase();

++i;

}

// I think you'll need to cut out the spaces too though. So you might need a //delimeter(something that ignores the spaces)

// Try googling regular expressions in java

lethalwirea at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 10
NO NO NO.You don't need an array of the letters. They are already stored in the String. Breaking them down into an array is pointless when you can use charAt method.
floundera at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 11

oh my god. this is too confusing... I don't understand what this means.... I can't even seem to work out the algorithm for this... I don't know how long the string is going to be. I don't know how to add the toLowerCase method into my code without it giving an error.

> You don't need an array of the letters. They are

> already stored in the String. Breaking them down into

> an array is pointless when you can use charAt method.

What do you mean by this? How would I go about using the charAt method?

The API's are useless to me because I don;t understand them at all... sorry if this drives people away from helping me, but I am totally lost...

I know you are trying to help me without actually giving me the actual code, and I thank you for it. This is just hard for me to understand, java is no easy thing to understand

Toneza at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 12

Yes.. you're right flounder. (Like always) :)

The way I would do it is by creating a Map.

Then

for(var = 0; var < string.length; var++)

if(myMap.containsKey(string.charAt(var))

myMap.put(string.charAt(i), myMap.get(string.charAt(i)) + 1);

else

myMap.put(string.charAt(i), 1);

println(myMap);

This is something else you may consider. It seems to work fine.

lethalwirea at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 13

declare int array of length 26

change string to lowercase

loop over string {

get current char

if(current char is a letter) {

increment array that corresponds to current char

}

}

loop over array {

print letter

print array element

new line

}

To give you an idea of what I was refering to with ascii values.

System.out.println((char)97);

System.out.println((int)'a');

floundera at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 14

how would i write this? I am still unure how to convert string to lowercase. when I try to use public String toLowerCase ,,, it tells me "illegal start of expression"

import javax.swing.*;

public class Frequency{

public static void main(Stirng [] args){

String str;

int[] arr = new int[26];

str = JOptionPane.showInputDialog("Type words!");

public String toLowerCase(str);

for(int i = 0; i <26; i++){

if(

thats all i got... i don't know what to put into the if statement. The toLowerCase if done wrong cuz there is error.

How do I tell if the current char is a letter? and then increment the array that corresponds to the current letter?

Toneza at 2007-7-11 15:58:41 > top of Java-index,Java Essentials,New To Java...
# 15
toLowerCase is a method of the String class. How do you call a method? The same way has you have always called a method. Also it returns a String so you have to make an assignment.
floundera at 2007-7-21 19:37:20 > top of Java-index,Java Essentials,New To Java...
# 16

okay.. import javax.swing.*;

public class Frequency {

public static void main(String[] args) {

int[] amount = new int[26];

}

public String toLowerCase(){

String str;

str = JOptionPane.showInputDialog("Type some words!");

return str;

}

}

this right so far?

Toneza at 2007-7-21 19:37:20 > top of Java-index,Java Essentials,New To Java...
# 17

I just dont understand this.

>declare int array of length 26

>change string to lowercase

>loop over string

get current char

if (current char is a letter){

increment array that corresponds to current char

}

}

>loop oover array {

print letter

print array element

new line

}

I dont understand this. I've done the array length of 26, and the string to lowercase. Loop over the string.. I would use a for loop right?

for(char i = 0; i<26; i++){

if( .....

I still don't know what to put into the if statement. and with a char, does it not default a null instead of a 0? So what would I put into the for loop?

Toneza at 2007-7-21 19:37:20 > top of Java-index,Java Essentials,New To Java...
# 18

After looking at the algorithm... it says to get the current char. a for loop does get the current char.

for(char i=0; i<26; i++){

Now looking at the if statement. it says |> if (current char is a letter){

There is nothing in my code that would be able to tell if there is a letter or not. How would I do this? and how would I increment array that corresponds to current char? please please help

Toneza at 2007-7-21 19:37:20 > top of Java-index,Java Essentials,New To Java...
# 19

Tonez I think you really need to step back and take another look at the Java basics. Try looking at [url=http://java.sun.com/docs/books/tutorial/java/concepts/class.html]classes and methods[/url], and [url=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html]variables[/url].

Djaunla at 2007-7-21 19:37:20 > top of Java-index,Java Essentials,New To Java...