James M
2012-02-21 20:41:25 UTC
import java.util.Scanner;
public class StringSearch {
/**
* @param args
*/
public static int i = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the string: ");
String userstring = keyboard.nextLine();
userstring = userstring.toLowerCase();
System.out.println("Enter the character to find: ");
String userchar = keyboard.nextLine();
int begin = 0;
int[] output = new int[30];
for (int i = 0; i <= output.length; i++) {
output[i] = linearSearch(userstring, begin, userchar);
begin = output[i] + 1;
}
if (output[0] == -1)
System.out.println("The character " + userchar
+ "was not found");
else
System.out.println("The character " + userchar
+ " was found at indices " + output);
// output = linearSearch(userstring, begin, userchar);
}
public static int linearSearch(String search, int startVal, String lookout) {
// int[] charRay = new int[search.length()];
int[] index = new int[30];
for (i = startVal; i <= search.length(); i++) {
index[i] = search.indexOf(lookout, startVal);
// return index;
}
return index[i];
/*
* Old linearSearch loop, reference if needed for (int i = startVal; i <
* search.length(); i++){ if (search.equals(startVal)) return i; }
* return -1;
*/
}
}
So basically, I'm trying to call the method in a loop so that additional indices will be reached. For example, in the string "mom", it would call the method once, find the m at index 0, run again, find the m at index 2, then terminate. I'm not sure exactly what's wrong, and for the most part it's just printing addresses. Any and all help is wonderful. Please try to run/compile the code and make sure it works, this is a late project :/
Thank you for reading/helping.