John
John

Reputation: 13

How to access a list

So ive been working on reading from a list into an array and using the contents to make a calculations.

I have the array parts =

[Mildred, Bush, 45, 65, 45, 67, 65, Fred, Snooks, 23, 43, 54, 23, 76, Morvern, Callar, 65, 45, 34, 87, 76, Colin, Powell, 34, 54, 99, 67, 87, Tony, Blair, 67, 76, 54, 22, 12, Peter, Gregor, 99, 99, 99, 99, 99]

The assumption here is that every 7 values is one line of the text file. So what i want to do is take the name, change the numbers into integers, do calculations then output them.

The code i tried using is:

for(int i=0; i < 42;i=i+7) {
    float avg;
    float total;
    for (int x=2; x < 7;x++) {
        String number = parts[x];
        int num = Integer.parseInt(number);
        total = total + num;
    }
    avg = total/6;
    System.out.print(parts[i+1] + "," + parts[i+2] + + "Final Score is avg");
}

but i get hit with an error

The type of the expression must be an array type but it resolved to List<String>

So i was wondering how I would go about solving this

Updated try:

String fileName = "Details.txt";

    String wfilename = "output.txt";

    // This will reference one line at a time
    String line = null;

    String temp;

    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = new FileReader(fileName);

        FileWriter fileWriter = new FileWriter(wfilename);


        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        List<String> parts = new ArrayList<>();
        String [] temp2;

        while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
            temp = line;
            temp2 = line.split(" ");

            for(int i=0; i<7; i++){
                parts.add(temp2[i]);
           }
            //System.out.println(line);
            bufferedWriter.write(line + "\n");
        }   
        System.out.print(parts);

        //conver to an array of strings
        String[] partsArray = parts.toArray();

        //code to do take values from array, do calculations and format to output
        for(int i=0; i < 42;i=i+7) {
            float avg;
            float total = 0;
            for (int x=2; x < 7;x++) {
                String number = partsArray[x];
                int num = Integer.parseInt(number);
                total = total + num;
            }
            avg = total/6;
            System.out.print(partsArray[i+1] + "," + partsArray[i+2] + "Final Score is" + avg);
        }

        // Always close files.
        bufferedReader.close();   
        bufferedWriter.close();
    }
    catch(FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");                  

    }

Upvotes: 0

Views: 51

Answers (2)

vashzak
vashzak

Reputation: 1

The .toArray() should do the trick.

Ex: String[] partsArr = parts.toArray();

Some reading material: here

Upvotes: -1

corsiKa
corsiKa

Reputation: 82589

You clearly have a list. You can either turn it into an array, like this

String[] partsArray = parts.toArray(new String[parts.size()]);

or begin accessing it like a list, like so

String number = parts.get(x);

Upvotes: 3

Related Questions