Home > database >  Inserting file data in certain position into an arraylist
Inserting file data in certain position into an arraylist

Time:06-06

I'm not sure where did it go wrong as I am fairly new to Java, this is my code :

private static void showgui() {
        BufferedReader bufReader = new BufferedReader(new FileReader("bmidata.txt"));
        List<Double> scores = new ArrayList<>();

            String score2;
            while ((score2 = bufReader.readLine()) != null) { 
                String[] score1 = score2.split(","); 

                double parsed []= Double.parseDouble(score1[]);

        int maxDataPoints = 13;
        for (int i = 0; i < maxDataPoints; i  ) {
            scores.add(parsed[3]);
        }
        MainPanel mainPanel = new MainPanel(scores);
        mainPanel.setPreferredSize(new Dimension(800, 600));
        
        JFrame frame = new JFrame("Body Mass Index");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
            bufReader.close();
    }

I've compiled this and got 1 error :

PlotGraph.java:169: error: '.class' expected
                double parsed []= Double.parseDouble(score1[]);

I don't think I have to remove [] from score1 , I've tried it but it led to another error which is :

PlotGraph.java:169: error: incompatible types: String[] cannot be converted to String
                double parsed []= Double.parseDouble(score1);
                                                     ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output

Could anyone tell me an alternative way to insert the data from a txt file in the position at index 3 into an arraylist, or any way for me to fix this error?

CodePudding user response:

There isn't a "parseDouble()" method which takes String[](an Array).
Use streams to map string value to double and collect in Double Array.

Sample,

String[] scores = new String[] {"1.0", "2.0"};
Double[] parsed = Arrays.stream(scores).map(Double::parseDouble).toArray(Double[]::new);
  • Related