Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to store all lines in 2D array, you can use something like that:</p> <pre><code>public static void main(String[] args) throws Exception { FileInputStream inputStream = new FileInputStream("file.txt"); InputStreamReader streamReader =new InputStreamReader(inputStream,"UTF-8"); BufferedReader in = new BufferedReader(streamReader); int increment = 10; int size = 0; // number of lines String[][] all = new String[increment][]; for(String line; (line = in.readLine()) != null;) { String[] data = line.split(","); all[size++] = data; if(all.length == size) { // Increment capacity String[][] tmp = new String[all.length + increment][]; for(int i = 0; i &lt; data.length; i++) { tmp[i] = all[i]; } all = tmp; } } // Trim to size String[][] tmp = new String[size][]; for(int i = 0; i &lt; size; i++) { tmp[i] = all[i]; } all = tmp; </code></pre> <p>Sorting: If your teacher not say nothing about <code>java.util.Comparator</code>:</p> <pre><code> Arrays.sort(all, new Comparator&lt;String[]&gt;() { @Override public int compare(String[] a, String[] b) { // By year? int yearA = Integer.parseInt(a[0]); int yearB = Integer.parseInt(b[0]); return yearA - yearB; } }); </code></pre> <p>Although it may not be very useful sort, because the structure does not allow a quick search.</p> <p>Query: And finally the user input:</p> <pre><code> String input = null; Scanner sc = new Scanner(System.in); do { System.out.print("Input the year: "); String year = sc.nextLine(); for(int i = 0; i &lt; size; i++) { if (all[i][0].equals(year)) { // out data } } } while (input == null || input.isEmpty()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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