Thursday, 15 July 2010

java - Comparing charAt(x) to String array = letter [y] -


I am trying to write a piece of code that produces a letter frequency using arrays. I stuck a little bit about how to compare a letter to an alphabet in a string in a string. My original pseudo code is as follows.

  import java.util.Scanner; Public class test {public static zero main (string [] args) {scanner sc = new scanner (System.in); System.out.print ("Please enter a sentence:"); String str = sc.nextLine (); String [] Come = {"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", "Zed"}; Float [] freq = new float [25]; Int x, a = 0, b = 0, strCount = 0; String straight 1; Str1 = str.replaceAll ("", ""); For (x = 0; x & lt; str1.length (); x ++) {strCount ++; } System.out.println ("The number of characters in the string is:" + strCount); Println ();  

And now I'm stuck to how to compare str1 to sir array. I have tried the following, but there is a problem in comparison to this.

  while (b & lt; strCount) {while (a & lt; let.length) {if (let [a] == Str1.charAt (b)) {freq [a] = Freak [A] ++; } If (let [a]! = Str1.charAt (B)) {a = a ++; }} B = B ++; }  

Any help would be greatly appreciated

thanks.

OK, I see many other issues but the one you are asking is quite simple.

  if (let [a] .charAt (0) == str1.charAt (b)) // < - A letter {freq [A] ++; } Else {a ++; }  

In addition, strCount = str1.length (); There is no need for any loop.


No comments:

Post a Comment