英文:
Java copy elements in column from String array to new int array
问题
以下是翻译好的部分:
我只是需要一点关于某事的帮助,似乎无法正确解决。
我有以下代码,它显示了CSV文件中的值。这部分代码是正常工作的。
while(file.hasNext()){
String data = file.nextLine();
String[] values = data.split(",");
for(String index : values){
System.out.printf("%s \t", index);
}
System.out.println();
}
这段代码显示:
1 John Smith Engineering
2 Jim Jones Cooking
我想从 values[0] 中获取值(即ID - 1,2),并将它们复制到一个新的整数数组中,以便我可以将其传递给其他方法来执行搜索等操作。任何帮助将不胜感激。
谢谢!
英文:
just after a bit of help with something that I cant seem to get right.
I have the following code which displays the values in a csv file. This is working.
while(file.hasNext()){
String data = file.nextLine();
String[] values = data.split(",");
for(String index : values){
System.out.printf("%s \t", index);
}
System.out.println();
}
The code displays:
1 John Smith Engineering
2 Jim Jones Cooking
I want to take the values just from values[0] (which are the ID's - 1,2) and copy them into a new int array so I can pass this to other methods to perform searches and whatnot. Any help would be greatly appreciated.
Thanks!
答案1
得分: 0
// 仅翻译代码部分
将 values[0] 保存到一个单独的列表中,并将其转换为数组。如果您知道 id 的数量,可以直接将 id 保存到一个数组中。
List<String> ids = new ArrayList<>();
while (file.hasNext()) {
String data = file.nextLine();
String[] values = data.split(",");
if (values.length >= 1) {
ids.add(values[0]);
for (String index : values) {
System.out.printf("%s \t", index);
}
}
System.out.println();
}
String[] idsArray = new String[ids.size()];
ids = ids.toArray(idsArray);
// 使用 idsArray
英文:
Just save values[0] into a seperate list and convert it to an array. If you know the number of ids you can directly save your ids into an array.
List<String> ids = new ArrayList<>();
while (file.hasNext()) {
String data = file.nextLine();
String[] values = data.split(",");
if (values.length >= 1) {
ids.add(values[0]);
for (String index : values) {
System.out.printf("%s \t", index);
}
}
System.out.println();
}
String[] idsArray = new Integer[ids.size()];
ids = ids.toArray(idsArray);
// use idsArray
答案2
得分: 0
不太确定我完全理解你的问题,但你可以尝试这样做:
String[] str_array = {...};
int[] int_array = {...};
int_array[index] = Integer.parseInt(str_array[index]);
这样你就可以将一个字符串添加到整数数组中,注意处理NumberFormatException。
编辑:
ArrayList<Integer> ids = new ArrayList<>(); // IDs: 1, 43, 23...
Scanner scanner = new Scanner(System.in); // 从控制台读取
int input = scanner.nextInt();
if (ids.contains(input)) {
// true
} else {
// false
}
英文:
Not quite sure I understand your question fully, but you can try this:
String[] str_array = {...};
int[] int_array = {...};
int_array[index] = Integer.parseInt(str_array[index]);
This way you can add a String to an int array, watch out for NumberFormatExceptions.
Edit:
ArrayList<Integer> ids = new ArrayList<>(); // IDs: 1, 43, 23...
Scanner scanner = new Scanner(System.in); // Reading from console
int input = scanner.nextInt();
if (ids.contains(input)) {
// true
} else {
// false
}
答案3
得分: 0
可以尝试这样做:)
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
while(file.hasNext()){
String data = file.nextLine();
String[] values = data.split(",");
for(int key=0;key<values.size();key++){
if (!map.containsKey(key)){
map.put(key, new ArrayList<String>());
}
map.get(key).add(values[key]);
}
}
Integer[] items = new Integer[map.get(0).size()];
for (i = 0; i < map.get(0).size() ; i++) {
items[i]=Integer.valueOf(map.get(0).get(i));
}
英文:
you can try this:)
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
while(file.hasNext()){
String data = file.nextLine();
String[] values = data.split(",");
for(int key=0;key<values.size();key++){
if (!map.containsKey(key)){
map.put(key, new ArrayList<String>());
}
map.get(key).add(values[key]);
}
}
Integer[] items = new Integer[map.get(0).size()];
for (i = 0; i < map.get(0).size() ; i++) {
items[i]=Integer.valueOf(map.get(0).get(i));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论