我有一个URL链接(http://www.tlu.ee/~jaagup/veeb1/loomad.txt),其数据如下:
animal, mass, height
dog, 1, 2
cat, 2, 1
dog, 1, 2
cat, 2, 1
dog, 1, 2
cat, 2, 1
...
我需要从中获取数据并将其附加到“dog”和“cat”
这就是我现在所拥有的:
import java.net.*;
import java.io.*;
public class andmed {
public static void main(String[] args) throws IOException {
try{
String aadress="http://www.tlu.ee/~jaagup/veeb1/loomad.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(new URL(aadress).openStream()));
String rida= br.readLine();
System.out.println("Tulbad: " + rida);
rida = br.readLine();
int[] kassid = new int[rida.split(",").length];
int kogus = 0;
while(rida!=null){
if (rida.startsWith("kass")){
String[] m = rida.split(",");
for (int i = 0; i < m.length; i++) {
m[i] += Double.parseDouble(m[i]);
kogus =+ 1;
}
}
kogus++;
//System.out.println(m[1]);
rida=br.readLine();
}
br.close();
} catch(Exception ex){
System.out.println("Probleem: "+ ex);
ex.printStackTrace();
}
我找到了一种方法来查明它是关于“猫”还是“狗”,但找不到任何示例如何只将这些数字放入数组中。关于它的任何建议? 提前致谢!
答案 0 :(得分:0)
也许这会有用。
我创建了一个单独的类来存储文件中每行的数据。
public static void main(String[] args) {
try{
String url="http://www.tlu.ee/~jaagup/veeb1/loomad.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
String line= br.readLine();
System.out.println("Tulbad: " + line);
// almost the array :)
List<Animal> animals = new ArrayList<>();
// read to the end of file
while ((line = br.readLine()) != null) {
final String[] temp = line.split(",");
Animal animal = new Animal();
animal.setSpecies(temp[0]);
animal.setWeight(Integer.valueOf(temp[1]));
animal.setSize(Integer.valueOf(temp[2]));
animals.add(animal);
}
br.close();
// display the list
animals.forEach(System.out::println);
} catch(Exception ex){
System.out.println("Probleem: "+ ex);
ex.printStackTrace();
}
}
}
这是你的程序,但稍作修改 - 基本上,它从文件中读取所有内容,为每一行创建Animal对象并将其添加到列表中。
Tulbad: liik,mass,korgus
Animal{species='kass', weight=1000, size=25}
Animal{species='kass', weight=1200, size=28}
Animal{species='kass', weight=1800, size=30}
Animal{species='kass', weight=2000, size=35}
Animal{species='kass', weight=1000, size=27}
....
输出如下:
=IFERROR(IF(MATCH(A1,Sheet1!A:A,0),"A",0),0)
您可以轻松地遍历ArrayList,获得您需要的任何内容。