Wednesday, 9 November 2016

Getting back to some java

Surprised I knocked this out very quickly for me, challenge 

https://www.reddit.com/r/dailyprogrammer/comments/5bn0b7/20161107_challenge_291_easy_goldilocks_bear/

package com.company;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<int[]> values = new ArrayList<>();

        String filePath = "someFile.txt";

        try {
            BufferedReader br = new BufferedReader(

            new FileReader(filePath));

            String currentLine;

            while ((currentLine = br.readLine()) != null) {
                int seatCapacity = Integer.parseInt(currentLine.

                substring(0, currentLine.indexOf(' ')));
                int temp = Integer.parseInt(currentLine.substring(currentLine.indexOf(' ') +1, currentLine.length()));
                values.add(new int[] {seatCapacity, temp});
            }

        } catch (IOException ex) {
            System.out.println();
        }

        StringBuilder suitableChairs = new StringBuilder();
        int weight =0, maxTemp =0;
        for (int i = 0; i < values.size(); i++) {
            if (i == 0) {
                weight = values.get(i)[0];

                maxTemp = values.get(i)[1];
            } else if (values.get(i)[0] >= weight && values.get(i)[1] <= maxTemp) {
                suitableChairs.append(i + " ");
            }
        }

        System.out.println(suitableChairs);
    }
}

No comments:

Post a Comment