java - 搜索数组中带有空格的元素

标签 java arrays string spaces

我创建了一个建筑目录,它基本上向用户显示了整个目录(使用字符串数组),如果他们不想查看整个目录,他们可以只搜索建筑中的特定业务。但是,我似乎无法使搜索选项起作用,因为在我的数组中,中间有空格......例如:

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/

相关文章:

c# - 如何循环比较两个文本文件中的数百万个值?

c++ - 动态分配的字符串

java - 发布视频时总是提示视频格式不受支持

java - 为什么我的 Kafka Consumer 在第一次运行时消费消息很快,但在以后的运行中速度会大大降低?

javascript - 在javascript中将复选框的属性值插入数组

c - 为什么我没有从该函数中获得任何值(value)?

c - 简单字符串匹配期间比较的字符数

java - Hibernate中如何进行保存点和回滚?

java - 不同OS环境下对JUnit测试的不同期望

javascript - 使用 fetch API 从数组获取数据时出现问题