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")
Counter.add(stringlabel);
Stringtext = new JTextField(25);
Stringtext.setEditable(true);
sttringsearch = new JLabel("Letter To Search\n");
Counter.add(stringsearch);
SSearch = new JTextField(1);
SSearch.setEditable(true);
Count = new JButton("Search\n");

TextFieldHandler handler = new TextFieldHandler();
Stringtext.addActionListener(handler);
Count.addActionListener(handler);
Counter.add(Stringtext);
Counter.add(StringOutput);
Counter.add(SSearch);

StringOutput = new JTextField(25); /* Im still thinking i might not need an output box*/
StringOutput.setEditable(false);
Counter.add(Count);
setSize (400,300);
show();
}

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)
{

c++;
x = StringEntered(StringSearch, x+1);
}

}
}
}

public static void main (Stringargs[])
{
Index app = new Index();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE_);
}

}

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.

thanks.

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.