Index.java

Index.java

I need to write a Java GUI application called Index.java that inputs several lines of text and a search character and uses String method indexOf to determine the number of occurrences of the character in the text.
View Answers

April 6, 2010 at 12:43 PM

Hi Friend,

Try the following code:

import java.util.*;

public class CountCharacters {
public static void main(String[] args) {
System.out.println("Enter string");
Scanner input=new Scanner(System.in);
String str=input.nextLine();
String newString=str.replace(" ","");
String s = str.replaceAll((String.format("(.)(?<=(?:(?=\\1).).{0,%d}(?:(?=\\1).))", str.length())), "");
String st=s.replace(" ","");
char character[]=st.toCharArray();
System.out.println("Search Character: ");
String chh=input.next();
char ch=chh.charAt(0);
System.out.println(ch+ " occurs " + count(ch,newString)+" times.");
}
static int count(char c, String str) {
if (str == null) return 0;
int cnt = 0;
for (int i = 0;; cnt++) {
if ((i = str.indexOf(c,i)+1) == 0) break;
}
return cnt;
}
}

Thanks









Related Tutorials/Questions & Answers:
Index.java - Java Beginners
Index.java  I need to write a Java GUI application called Index.java that inputs several lines of text and a search character and uses String method indexOf to determine the number of occurrences of the character in the text
Java application - Java Beginners
Java application  Write a Java GUI application called Index.java that inputs several lines of text and a search character and uses String method indexOf to determine the number of occurrences of the character in the text
Advertisements
Upload Code error on deploying
(BaseFieldTag.java:176) at jspservlet.index.jspService(index.java:168
index - Java Beginners
index  Hi could you pls help me with this two programs they go hand in hand. Write a Java GUI application called Index.java that inputs several lines of text and a search character and uses String method indexOf to determine
Java GUI IndexOf - Java Beginners
Java GUI IndexOf  Hello and thank you for having this great site. Here is my problem. Write a Java GUI application called Index.java that inputs several lines of text and a search character and uses String method indexOf
Finding start and end index of string using Regular expression
in program  Finding_index.java are described below:- String[] text
Delete database Table through hibernate and Spring
type) { this.type = type; } } This fetch and save is called by index.java

Ads