Question:
Sequential search of a string in Java?!?!?
James M
2012-02-21 20:41:25 UTC
I'm doing a sequential search in Java for a string. My code is very close, I think, and I'm pretty sure I have the right code, but it's throwing me errors. I'm working in Eclipse. This is my code:

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.
Five answers:
Casper
2012-02-21 21:08:50 UTC
Try to throw IndexOutOfBoundException. right now i am not having any compiler.i am telling you this because java generates exceptions while using indices (not all the time, but most of the time its related to strings)
leung
2016-11-11 13:10:11 UTC
Sequential Search Java
Motorhead
2012-02-21 22:05:09 UTC
There is only one string, so you only need to traverse it once.



public class StringSearch {



public static void main(String[] args) {





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: ");

char userChar = keyBoard.nextLine().charAt(0);



int indicies[50];

char nextChar;

int cnt = 0;

int len = userstring.length;

for (int i = 0; i < len; i++)

{

nextChar = userstring.charAt(i);

if( nextChar == userChar)

{

cnt++;

indices[i] = 1;

}

else

{

indices[i] = 0;

}

}



if (cnt == 0)

{

System.out.println("The character " + userchar + " was not found");

}

else

{

System.out.print("The character " + userchar + " was found at indices " );

for (i = 0; i < len; i++)

{

System.out.format(" %3d, ", i);

}

System.out.println( ".");

}



}



}





Sorry, but my Java is extremely rusty, so there will be mistakes.

But you were doing things much to complicated.
anonymous
2016-02-26 01:35:39 UTC
searching for Arrays jave we have to open google webside and download her name.
berny
2012-02-21 21:15:52 UTC
http://www.youtube.com/watch?v=vW53w7me4AE&list=PL27BCE863B6A864E3&index=1&feature=plpp_video


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...