1 Replies - 202 Views - Last Post: 16 July 2012 - 08:26 PM Rate Topic: -----

#1 FastFox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 15-July 12

looking for a character

Posted 16 July 2012 - 07:51 PM

Hey, currently Im writing a program for a person to enter a couple of words or a sentence and then also input a particular letter to look for and counts the number of times it occurs. However I am not exactly sure how to fill in the missing pieces? Here is what I have before


public class Zletter{

    public static void main(String[] args) {

    int = TIH;
    char letter= (char)System.in.read();



    String Z1 = javax.swing.JOptionPane.showInputDialog	("Please Enter a sentence");

      for( int i = 0; i < Z1.length; i++ ) {
                  if( n1.charAT == letter ) {
                  TIH++;

                  System.out.println( "the character " + Z1 +
                 " occurred " + TIH + " times in the string" );



Is This A Good Question/Topic? 0
  • +

Replies To: looking for a character

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,736
  • Joined: 19-March 11

Re: looking for a character

Posted 16 July 2012 - 08:26 PM

To begin with, you really need to get your IO together. Mixing up console input and Swing that way is just making mud pies.

Make life simple, use a Scanner:

Scanner scan = new Scanner(System.in);
String input = scan.nextLine();


Get rid of the JOptionPane, play with the Scanner until you understand how to use it to get what you need.



The other part of your problem is completely independent from that. You don't need the input to be working to work on that, so let's look at what you have there.

	      for( int i = 0; i < Z1.length; i++ ) {
	                  if( n1.charAT == letter ) {
	                  TIH++;


Clearly you have an idea, and it's clearly a good one. I'd be willing to bet that you can express that idea in simple English and it would make sense.
Your problem is syntax - this makes sense in logic, but it doesn't make sense in Java.

I'll point out the errors and you can try to sort them out - this makes more sense than me just fixing everything, since you wouldn't learn anything that way.

Z1.length - length is a method of the String class, not a field. See the API for the String class for details.

n1.charAT - n1 does not exist in the segment of code you've shown us. This should be the name of the String that you want to search in.
- charAt is a method, it takes the index of the character you're interested in as an argument. Again, see the API for the String class.
TIH++; - Your variable names are awful. They should communicate something. Random clumps of letters and numbers don't tell you what they're supposed to mean. What is this variable supposed to contain? Call it that! Also, obey Java naming conventions. Variables take a small initial letter, and internal words are capitalized: numberOfOccurrences, for example.

Go ahead and work on this. I'll be ignoring this topic until the morning, it's yours to fix.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1