简体   繁体   中英

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

I have an URL link( http://www.tlu.ee/~jaagup/veeb1/loomad.txt ) with data like:

animal, mass, height

    dog, 1, 2
    cat, 2, 1
    dog, 1, 2
    cat, 2, 1
    dog, 1, 2
    cat, 2, 1
    ... 

I need to get data from it and attach it to "dog" and "cat"

this what I have for now:

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

I've found a way to find out if it is about "cat" or "dog", but could not find any example how to put only those numbers in an array. Any suggestions about it? Thanks in advance!

Maybe this will be useful.

I created a separate class to store data from each line in your file.

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 +
                '}';
    }
}

This is your program, but with minor changes - basically, it reads everything from the file, creates Animal object for each line and adds it to the list.

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

The output is like :

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

You can easily iterate over ArrayList, get anything you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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