我创建了一个建筑目录,它基本上向用户显示了整个目录(使用字符串数组),如果他们不想查看整个目录,他们可以只搜索建筑中的特定业务。但是,我似乎无法使搜索选项起作用,因为在我的数组中,中间有空格......例如:
String Floor [] = { "底层", "空置"};
如果我删除“Ground Floor”之间的空间并将其设置为“GroundFloor”,则可以搜索它,否则如果您搜索“Ground Floor”,它就会显示为未找到。我可以做什么来解决这个问题?
程序的骨架:
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
最佳答案
而不是使用
namefind = businessname.next();
使用
namefind = businessname.nextLine();
这样它也考虑了空间。
关于java - 搜索数组中带有空格的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27587522/