Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Don't worry about whether it's running on Mac or Windows. Java takes care of all the business for you. :)</p> <p>You could do something like the following. It's just a quick solution so you might want to do some changes perhaps. It reads from a file named "input.txt" right now.</p> <pre><code>import java.io.*; public class ParseLines { public static void main(String[] args) { try { FileInputStream fs = new FileInputStream("input.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(new DataInputStream(fs))); String line; double collect[] = {0.0, 0.0, 0.0, 0.0}; int lines = 0; while ((line = reader.readLine()) != null) { String[] parts = line.split(","); if (parts.length == 5) { int avg = 0; boolean skip = false; for (int i = 1; i &lt; 5; i++) { String part = parts[i].trim(); try { int num = Integer.valueOf(part); avg += num; collect[i - 1] += (double) num; } catch (NumberFormatException nexp) { System.err.println("Input '" + part + "' not a number on line: " + line); skip = true; break; } } if (skip) continue; avg /= 4; lines++; System.out.println("Last name: " + parts[0].split(" ")[1] + ", Avg.: " + avg); } else { System.err.println("Ignored line: " + line); } } for (int i = 0; i &lt; 4; i++) { collect[i] /= (double) lines; System.out.println("Average of column " + (i + 1) + ": " + collect[i]); } reader.close(); } catch (Exception e){ System.err.println("Error: " + e.getMessage()); } } } </code></pre> <p><strong>Edit:</strong> Altered the code to average the first, second, third and fourth column.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload