Home > OS >  How to group every [0][1][2] of an Array to create a new object?
How to group every [0][1][2] of an Array to create a new object?

Time:12-03

I have an array and I want to assign values in group of 3s Name, Id, Population, Name, Id, Population, Name, Id, Population etc. Is there a way to do that? This is what I have

while (scanner.hasNext()) { `
    scanner.useDelimiter(",");`
    list.add(scanner.nextLine());}`
    for(int i=0; i<list.size(); i  ){
    String n = list.get(i);
    System.out.println("Hopefully going thru "   n);} //for me to check
String ar =list.toString();
Object [] a = ar.split(",");// splitting the array for each string
for(int h=0;h<a.length;h =3) { // for [0]  =  3 is Name
for(int j=1;j<a.length; j =3) { // for [1]  = 3 is Id
for(int k=2; k<a.length;k =3) { //for[2] = is Population
String name = a[h].toString();
String id = a[j].toString();
String population = a[k].toString();
System.out.println("name is "  name);// this is just to check correct values
System.out.println("id is "  id);// this is just to check correct values
System.out.println("population is "  population);// this is just to check correct values
CityRow cityRow = new CityRow(name,id,population); //?? I want every set of [0][1][2] to create a new object` 

CodePudding user response:

I don‘t think that ar has the correct data and I don‘t understand why you don’t work with list directly, but assuming that ar has the correct data, it should be possible to use:

for(int = 0; i < ar.length ; ) {
  var cityRow = new CityRow(
    ar[i  ],
    ar[i  ],
    ar[i  ]
  );
  // remember to add cityRow to an
  // appropriate list
}

CodePudding user response:

You use Scanner so no need to split an array. You can read each separate value one-by-one directly from it.

public class Main {

    public static void main(String... args) {
        Scanner scan = new Scanner(System.in);
        scan.useDelimiter("\\n|,");

        System.out.print("Total groups: ");
        int total = scan.nextInt();
        List<City> cities = readCities(scan, total);
        printCities(cities);
    }

    private static List<City> readCities(Scanner scan, int total) {
        List<City> cities = new ArrayList<>(total);
        System.out.println("Enter each city on a new line. Each line should be: <id>,<name>,<population>");

        for (int i = 0; i < total; i  ) {
            String id = scan.next();
            String name = scan.next();
            int population = scan.nextInt();
            cities.add(new City(id, name, population));
        }

        return cities;
    }

    private static void printCities(List<City> cities) {
        System.out.println();
        System.out.format("There are total %d cities.\n", cities.size());

        int i = 1;

        for (City city : cities) {
            System.out.format("City №%d: id=%s, name=%s, population=%d\n", i  , city.id, city.name, city.population);
        }
    }

    static class City {

        private final String id;
        private final String name;
        private final int population;

        public City(String id, String name, int population) {
            this.id = id;
            this.name = name;
            this.population = population;
        }
    }
}
  • Related