繁体   English   中英

从txt读取数据并将其转换为数组(java)

[英]Read data from txt and convert it into array(java)

我有一个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
    ... 

我需要从中获取数据并将其附加到“狗”和“猫”

这是我现在拥有的:

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();
    }

我找到了一种方法来找出它是关于“猫”还是“狗”的,但是找不到如何将那些数字仅放在数组中的任何示例。 有什么建议吗? 提前致谢!

也许这会很有用。

我创建了一个单独的类来存储文件中每一行的数据。

public class Animal {
    private String species; // cat or dog
    private int weight; // mass
    private int size; // height

    // getters and setters

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "species='" + species + '\'' +
                ", weight=" + weight +
                ", size=" + size +
                '}';
    }
}

这是您的程序,但有少量更改-基本上,它从文件中读取所有内容,为每一行创建Animal对象并将其添加到列表中。

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();
        }
    }
}

输出如下:

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}
....

您可以轻松地遍历ArrayList,获取所需的任何东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM