![]() |
![]() |
#1 (permalink) |
Status: Junior Member
Posts: 24
|
![]()
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.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 = 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"); else System.out.println("The character " + lookout + " was found with indices " + index[i]); } //delete 2nd loop, use indexOf(lookout) and return index return index; } } |
![]() |
![]() |
![]() |
Bookmarks |
Thread Tools | |
Display Modes | |
|
![]() |
||||
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 05: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 11:37 AM |
searching for a string in a file? | softelgin19 | Coding | 1 | 03-02-2012 07:08 AM |
Searching a string Array in java? | DjinnEternal | Programming | 0 | 12-12-2011 09:06 AM |
Write a program when user enter the string lower letter will change to upper letter | AffiffGrisold | Programming Languages | 0 | 09-12-2011 11:06 AM |