Java 拆分要输入到映射中的数组
我正在尝试拆分一个名称数组,例如“Joe Bloggs”。Joe和Bloggs是分开的,然后可以在以名字为键的地图中使用。这是我到目前为止所拥有的Java 拆分要输入到映射中的数组,java,arrays,string,loops,maps,Java,Arrays,String,Loops,Maps,我正在尝试拆分一个名称数组,例如“Joe Bloggs”。Joe和Bloggs是分开的,然后可以在以名字为键的地图中使用。这是我到目前为止所拥有的 public static void main(String[] args) { Map<String, String> snames = new HashMap<String, String>(); int index = -1; String[] names = new String[8];
public static void main(String[] args) {
Map<String, String> snames = new HashMap<String, String>();
int index = -1;
String[] names = new String[8];
Scanner s = new Scanner( System.in );
for( int i = 0; i <= names.length; i++ ){
System.out.println( "Enter student name:" );
names[ i ] = s.next();
}
System.out.println("Please type in the student index you want to view between 0 and 7");
index = s.nextInt();
while (index >7 || index <0){
System.out.println("Please type in a valid index value");
index = s.nextInt();
}
System.out.println("The student with the index of " + index + " is " + names[index]);
}
}
publicstaticvoidmain(字符串[]args){
Map snames=newhashmap();
int指数=-1;
字符串[]名称=新字符串[8];
扫描仪s=新扫描仪(System.in);
对于(inti=0;i7 | | index不太确定您要的是什么
for (String x : names){
String[] temp = x.split(" "); // split "Joe Bloggs" to ["Joe", "Bloggs"]
if (temp.length == 2){
snames.put(temp[0], temp[1]); // Mapping "Joe" to "Bloggs"
}
}
这就是你想要的吗?你自己试试,不要指望有人为你写。我想要的是想法,而不是期待有人为我写。如果你有两个名字相同的人:乔·布洛格斯和乔·史密斯会怎么样。当你将第一个名字添加到地图中时,第二个会覆盖第一个名字。