简体   繁体   中英

Searching for elements in an array with spaces

I created a building directory and it basically shows the user the entire directory (using a string array) and if they don't want to view the entire directory they can just search for the specific business in the building. However, I cannot seem to get the search option to work because in my array there are spaces in-between.. eg:

String Floor [] = { "Ground Floor", "Vacant" };

If I remove the space in-between "Ground Floor" and make it "GroundFloor" it can then be searched, otherwise it just comes up as not found if you search "Ground Floor". What can I do to fix this?

bones of program:

import java.util.Arrays;
import java.util.Scanner;

public class Townsendtest {
public static void main(String[] args) {

//prints the Floor Number of index and he Business listed on the floor
System.out.println("Floor Number\t\tBusiness"); 

//String array of businesses on each floor starting at floor 0-24
String floor [] = { "Ground Floor", "Advanced Technologies", "HiMark Marketing", "Law offices 
of John Daniels", "PST Systems", "Century United Brokers Inc.", "Creative Resources", "Design 
Centre Associates", "Ideal Media Group", "SF Net Developers", "Shears medical Services Inc.", 
"Green Space Construction Inc.", "Cornerstone Mortgage Capital", "Allied Advantage Realty", "JAMS 
the Resolution Experts", "Law Offices of Matt Dill", "Vacant", "The Drop in Centre", "Artisan 
Interiors Consultancy", "NGS Group", "Robert  H. Greene Real Estate", "Vacant", "Vacant", "Denise 
A. Patterson Attorney at Law", "Conference Rooms 1-6" };

//for loop to print entire index to the user
for(int counter=0; counter<floor.length; counter++ ){  
System.out.println(counter + "\t\t\t" + floor[counter]);

}//end for loop


Scanner businessname = new Scanner(System.in);
String namefind;

System.out.print("Enter the name of the Business: "); 
namefind = businessname.next();

for(int i = 0; i < floor.length; i++){

if(namefind.equalsIgnoreCase(floor[i])) {
System.out.println("Business Found!");
System.exit(0);
}
}
System.out.println("Business not found, try again");


}//end main
}//end class

Instead of using

namefind = businessname.next();

Use

namefind = businessname.nextLine();

So that it takes space into account as well.

If you use a scanner, its basic next method will give you the next token, ie, something delimited by white space.

Use nextLine to get a complete line containing user input, trim leading and trailing spaces and use this in the search.

namefind = businessname.nextLine();

you can also use BufferedReader instead of Scanner for your requirement. it's more efficient way.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM