Old 01-30-2012, 04:07 AM   #1 (permalink)
Status: Junior Member
Posts: 24

Default Searching string for letter? HELP!?

I'm trying to do the following:
-user inputs a phrase
-user inputs a letter they want to search for
-the output returns "the character (whatever they put) was found at indices 2 and 4" or whatever, or that it was not found

It is returning for the first index but is printing multiple lines. What do I do? D:

Here's my code:

import java.util.Scanner;
public class StringSearch {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner keyboard = new Scanner(;
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 = linearSearch(userstring, begin, userchar); //3rd parameter will be userchar


public static int[] linearSearch(String search, int startVal, String lookout){
//int[] charRay = new int[search.length()];

int[] index = new int[15];

for (int i = startVal; i <= search.length(); i++){
index[i] = search.indexOf(lookout);
if (index[0] == -1)
System.out.println("The character " + lookout + " was not found");
System.out.println("The character " + lookout + " was found with indices " + index[i]);


//delete 2nd loop, use indexOf(lookout) and return index

return index;
jamesm is offline   Reply With Quote



Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
searching a string in string array using binary search in c#? Unknown_User Programming 0 08-22-2012 04:37 PM
php code that compares the first letter of a string array value to a letter? Alex PHP Code/ PHP Codes 1 05-14-2012 10:37 AM
searching for a string in a file? softelgin19 Coding 1 03-02-2012 06:08 AM
Searching a string Array in java? DjinnEternal Programming 0 12-12-2011 08:06 AM
Write a program when user enter the string lower letter will change to upper letter AffiffGrisold Programming Languages 0 09-12-2011 10:06 AM