fork download
  1. /**
  2.  * StringLab2.java
  3.  *
  4.  * Name:
  5.  * Date:
  6.  * Got help from:
  7.  *
  8.  */
  9. import javax.swing.JOptionPane;
  10.  
  11. public class StringLab2
  12. {
  13.  
  14. public static int howManyWithOverlap(String str, String word)
  15. {
  16. //This method will count how many instances of 'word' sre found in 'str'
  17. //The 'word' may overlap another 'word'
  18. //Examples:
  19. // howManyWithOverlap("asdfgasjkasiuas", "as") returns 4
  20. // howManyWithOverlap("baaaaaac", "aaa") returns 4
  21. // howManyWithOverlap("dfdfdfdfdf", "fdf") returns 4
  22. // howManyWithOverlap("you betcha", "qwerty") returns 0
  23.  
  24. //int count = 0;
  25. int length = str.length();
  26. int cntr = 0;
  27.  
  28. for(int i = 0; i <= (length-1); i++)
  29. {
  30. int death = str.indexof(word,i);
  31.  
  32. if( death != -1)
  33. {
  34. cntr++;
  35. }
  36. }
  37.  
  38.  
  39. return cntr;
  40. }
  41.  
  42.  
  43.  
  44. public static void main(String args[])
  45. {
  46. String input = JOptionPane.showInputDialog("Enter a string.");
  47. String word = JOptionPane.showInputDialog("Enter a 'word'.");
  48.  
  49. System.out.println("Original string: "+input);
  50. System.out.println("Original word: "+word);
  51. System.out.println("\nhowManyWithOverlap method:");
  52. System.out.println(" "+howManyWithOverlap(input,word));
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59. }
  60.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:11: error: class StringLab2 is public, should be declared in a file named StringLab2.java
public class StringLab2
       ^
Main.java:30: error: cannot find symbol
			int death = str.indexof(word,i);
			               ^
  symbol:   method indexof(String,int)
  location: variable str of type String
2 errors
stdout
Standard output is empty