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 BeginnersIndex.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 BeginnersJava 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
index - Java Beginnersindex 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 BeginnersJava 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