Note that there are some explanatory texts on larger screens.

plurals
  1. POjava- Read data from a file for further processing
    text
    copied!<p>I'm new to java and I would like to ask you for help. I have some data stored in a txt file, each line holds three integers, separated by space. I would like to read the data from the file and then put this data in arrays for further processing, if certain conditions are met (in my case- third int is greater than 50). I read some questions about how to read the number of lines in a file or the file itself, but I can't seem to combine it all together to make it work. The latest version of the code looks like this:</p> <pre><code>public class readfile { private Scanner x; public void openFile(){ try{ x = new Scanner(new File("file.txt")); } catch (Exception e){ System.out.println("could not find file"); } } public void readFile() throws IOException{ LineNumberReader lnr = new LineNumberReader(new FileReader(new File("file.txt"))); int i = lnr.getLineNumber(); int[] table1 = new int[i]; int[] table2 = new int[i]; while(x.hasNextInt()){ int a = x.nextInt(); int b = x.nextInt(); int c = x.nextInt(); for (int j=0; j&lt; table1.length; j++) { if(c &gt; 50) { table1[j]=a; table2[j]=b; } } }System.out.printf(" %d %d", table1, table2); } public void closeFile(){ x.close(); } } </code></pre> <p>main is located in another file.</p> <pre><code>public static void main(String[] args) { readfile r = new readfile(); r.openFile(); try { r.readFile(); } catch (Exception IOException) {} //had to use this block or it wouldn't compile r.closeFile(); } </code></pre> <p>when I use %d on printf method i don't see anything, when I use %s I get some gibberish on the output like </p> <pre><code>[I@1c3cb1e1 [I@54c23942 </code></pre> <p>what should I do to make it work (ie. print pairs of a b when c is > 50)?</p> <p>Thanks in advance for any help, and sorry if this turns out to be some blatantly obvious problem, but I really ran out of ideas on how to improve this :)</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