Heres the problem that was given to me.

Write an application that inputs a String and Search a Character and uses a method String Method IndexOf to determine the number of Occurences of the character in the string.

we were told to use this book for research.
How To Program, 4th Edition

heres my program im still working on it and im not really good with programming and i try as much as i can to teach me since my Teacher only gives problems and never shows them or explains them properly enough for the not so good programmers to understand.

Hope someon can help me here.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java util.*;

public class Index extends JFrame
JLabel stringlabel, stringout, stringsearch;
JButton Count;
JTextField Stringtext,StringOutput,SSearch;

public Index()

super ("String counter");
Container Counter = getContentPane();
Counter.setLayout(new FlowLayout());
stringlabel =  JLabel("Enter a Sentence\n");
stringout = new JLabel("Result\n")
Stringtext = new JTextField(25);
sttringsearch = new JLabel("Letter To Search\n");
SSearch = new JTextField(1);
Count = new JButton("Search\n");

TextFieldHandler handler = new TextFieldHandler();

StringOutput = new JTextField(25); /* Im still thinking i might not need an output box*/
setSize (400,300);

private class TextFieldHandler implements ActionListener

public void actionPerformed (ActionEvent Index)

if (Index.getSource() == Count)

String StringEntered = Stringtext.getText();
String StringSearch = SSearch.getText();
int x =0, c =0;
X = StringEntered.indexOf(StringSearch);

while(x != -1)

x = StringEntered(StringSearch, x+1);


public static void main (Stringargs[])
Index app = new Index();


there are still some errors which im currently trying to solve, without help or just a book for reference i dont know how to solve them all.

Anyone who might know what im missing or doing wrong in the program who can help and pinpoint if possible the wrong codes and show me a correct one.

I really would appreciate any help.


and what exactly doesn't work?

to find the number of occurances using indexOf

String original = "originalString";
String oneChar = "i";

int a = original.indexOf(oneChar);
 * this will return an integer. if (for any value) this integer = -1, it means that the
 * character you're testing for is not in the original String
 * if it is not -1, it just states the first occurance of that char in the String
 * you could do something like this:
 int teller = 0;
 String toTest = original;
 while (a != -1){
    teller += 1;
    toTest = original.substring(a, toTest.length());
    a = toTest.indexOf(oneChar);
System.out.println(original + " contains " + teller + " times the char: " + oneChar);
This article has been dead for over six months. Start a new discussion instead.