Results 1 to 5 of 5
  1. #1
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Question taking input from a txt file and creating an array from tha taken integers

    hey guys so my input file looks something like this
    1
    2
    3
    4
    5
    public static void main(String[] args) throws FileNotFoundException {
    File data = new File("data.txt");// open file
    Scanner scanner = new Scanner(data);
    int index = 0;
    int z = 0;
    while (scanner.hasNextLine()) {
    z++;
    scanner.nextInt();
    }
    int[] numbers = new int[z];


    /**ok so the code above i just use it to get the size of my array but the problem seems to be in the next part. I don't know why the values of the array are not being updated. Can someone help with why or at least hint. Thanks.
    */

    {

    while (scanner.hasNextLine())
    {
    int x = scanner.nextInt();
    numbers[index] = x;
    index++;
    }
    }
    }
    Last edited by romero4742; 03-15-2012 at 08:48 PM. Reason: Solved

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default Re: taking input from a txt file and creating an array from tha taken integers

    Yes the problem is that the iterator over you file reach the end after the first while statement, so when the code doesn't enter the second while because scanner.hasNextLine() is false.
    You should reset the Scanner so that it rereads the file from the beginning
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default Re: taking input from a txt file and creating an array from tha taken integers

    just a notice, you have an extra useless {} over the second while loop
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  4. #4
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: taking input from a txt file and creating an array from tha taken integers

    You're right haha thanks :)
    btw Reps+

  5. #5
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default Re: taking input from a txt file and creating an array from tha taken integers

    My pleasure :) thanks :)
    Click on REP and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Creating 2D array from all user input
    By peek_a_boo in forum New To Java
    Replies: 1
    Last Post: 12-08-2011, 09:16 PM
  2. Taking input from use
    By leepikamukharji in forum New To Java
    Replies: 1
    Last Post: 11-08-2011, 03:33 PM
  3. file input: array of integers
    By hannes in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 04:44 PM
  4. Taking input from Properties File
    By bugger in forum New To Java
    Replies: 4
    Last Post: 05-07-2008, 10:30 AM
  5. Taking input from console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 05:47 PM

Tags for this Thread

Posting Permissions

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