arrays - Reading txt file from working directory in Java (not working) -


i have read couple of posts of how read txt file int[] , through multiple ways possible haven't succeeded in any.

import java.io.*; import java.util.scanner;  public class sequencesquare { private int[] arr;  public sequencesquare()  {     int count = 0;     file filename = new file("sequence.txt"); // filename opening     try     {         scanner input = new scanner(filename);         while (input.hasnextline()) //while there line read         {             if(input.hasnextint()) //if line has int             {                 count++; // increment count                  input.nextint(); //move next integer             }         }          arr = new int[count]; // create arr sufficient size         input.close(); // close scanning file          scanner newinput = new scanner(filename);          for(int = 0; < arr.length; i++)         {             while (newinput.hasnextline()) // same above             {                 if(newinput.hasnextint()) // same above                 {                    arr[i] = newinput.nextint(); //store int scanned arr                  }             }         }         newinput.close();     }     catch(filenotfoundexception f)     {         f.printstacktrace();     } } "sequence.txt" 1 2 5 9 29 30 7 9 111 59 106 130 -2 

so when default constructor called suppose open "sequence.txt" , read integers formatted int array.in txt file numbers formatted integer per line such 4\n 5\n etc. when iterate through array "arr" seems have no contents. have implemented number of test functions test whether has been filled(not listed) arr not return anything. please help. please tell me happening. i'd rather know explanation happening rather answer both do. know can use arraylists , lists carry out same functions want array. if want see whole class can post other code unnecessary works

this how can using array. if list simpler.

import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception;  public class filereading {     public static final string file_path = "d:\\testing.txt";     public static void main(string[] args) {                  try {             int[] newarr = readfile(file_path);             //testing new array             for(int i=0;i<newarr.length;i++){                 system.out.println(newarr[i]);             }          } catch (ioexception e) {             e.printstacktrace();         }      }      public static int[] readfile(string pathtofile) throws numberformatexception, ioexception{         string scurrentline;         int[] arr = new int[countlines(pathtofile)];         bufferedreader br = new bufferedreader(new filereader(pathtofile));         int i=0;         while ((scurrentline = br.readline()) != null) {                             arr[i] = integer.parseint(scurrentline);             i++;         }         br.close();         return arr;     }      public static int countlines(string pathtofile) throws numberformatexception, ioexception{         bufferedreader br = new bufferedreader(new filereader(pathtofile));         int count =0;         while ((br.readline()) != null) {             count++;         }         br.close();         return count;     } } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -