Results 1 to 12 of 12
  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Reading From File

    Java Code:
    		File f = new File("F:/" + date + ".txt");
    		if (!f.exists()) {
    			System.out.println("file not found !");
    			System.exit(1);
    		}
    		int lines = 0;
    		@SuppressWarnings("resource")
    		Scanner scan = new Scanner(f);
    		while (scan.hasNext()) {
    			System.out.println(scan.nextLine());
    			lines++;
    
    		}
    		System.out.println(lines);
    		scan.reset();
    		int sum = 0;
    		int arr[] = new int[lines];
    		for (int i = 0; i < lines; i++) {
    			arr[i] = Integer.valueOf(scan.nextLine()).intValue();
    
    			sum += arr[i];
    		}
    		System.out.println(sum);
    the date.txt file contains numbers form 1-100, I want to sum and copy them in arr[] but I get noSuchElementException ... Is it why, after counting the lines, the pointers goes to 100th line and then starts form there again??? I have reset the point though ...
    Last edited by allaudin; 07-23-2013 at 02:42 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Reading From File

    Resetting a scanner does not reset the FileInputStream used (as you could've known by reading the API documentation of the Scanner class). I'd use a List<String> for this purpose though, not a fixed size array ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Reading From File

    I was just testing .. ok I will use linke list .. now what should I do to sum those numbers ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Reading From File

    If all you want to do is sum those numbers, I shouldn't use arrays nor a List<String>; I'd just keep an int sum= 0; ready and add those numbers to it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Reading From File

    how can I reset the pointer ?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Reading From File

    Quote Originally Posted by allaudin View Post
    how can I reset the pointer ?
    What pointer?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Reading From File

    file input pointer to the start of the file ...

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Reading From File

    thanks I have done it wiht randomAccessFile ... thanks josAh ....

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Reading From File

    Quote Originally Posted by allaudin View Post
    thanks I have done it wiht randomAccessFile ... thanks josAh ....
    Why do you want to read that file more than once? You can read those numbers (and add them) while reading the file ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Reading From File

    yes I can ... I was just learning how to read it more than once ..

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Reading From File

    Well simply opening a new FileInputStream also works, although when using a Scanner that would also mean creating a new Scanner. Which functionally makes sense IMO since you want to scan a new file to get fresh data - even if it is the exact same one as the previous time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Reading From File

    What if you wanted to actually system.out.println the number of lines (along with those) with a method call? Would you use another class to call getter method code like this:

    int numberOfLines = 0;

    //Loop through the lines of text, reading each line of text and stop when a
    //null value is reached (if there is no more lines in the text file, Java
    //returns a value of null):
    while((aLine = bf.readLine()) != null){
    numberOfLines++;//increment a counter called numberOfLines
    }
    bf.close();//flush the temporary memory buffer called bf
    return numberOfLines;//return the number of lines
    }

    private String getLines(String numberOfLines){
    lines = numberOfLines;
    return "File has " + lines;
    }//end of getLines method


    /*
    * Because the getLines method has been set to private, we will need to
    * add a non-private getter method to pass the number of lines from the
    * private numLines method above.
    *
    * Because this getter method is not private, the method in the
    * FileData.java class will call for the number of lines passed into this
    * getter method, rather than directly from the private getLines method:
    */
    String giveLines(int aLines){
    lines = this.getLines();//The this Java keyword "this"
    //means "this class", rather than another class that may have the
    //same method name. It avoids any confusion.
    return lines;
    }

Similar Threads

  1. Need help file reading a txt file (Emergency)
    By 765891 in forum New To Java
    Replies: 6
    Last Post: 02-28-2013, 12:48 AM
  2. Replies: 0
    Last Post: 12-15-2012, 01:37 AM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •