Page 3 of 3 FirstFirst 123
Results 41 to 50 of 50
  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Reading From File Question??

    Your problem is an argument for the use of ArrayLists. No sizing problems with them.

  2. #42
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    I wish I could use them, but professor said no ArrayList. I could have just trimmed the ArrayList

  3. #43
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Reading From File Question??

    Did you try using a partially filled array? Many methods can handle that. You provide the starting index and length or ending index.

  4. #44
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Reading From File Question??

    Quote Originally Posted by kraigballa View Post
    I wish I could use them, but professor said no ArrayList. I could have just trimmed the ArrayList
    Have You consider StringBuilder class?
    StringBuilder is sequence of characters and it resize automatically.
    You could insert into StringBuilder complete numbers and separate them with spaces.
    After that, You could use split method, in order to create an array.
    Say:
    Java Code:
    String[] stringArray = strBuilder.toString().split(" ");

  5. #45
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Reading From File Question??

    @diamonddragon
    I think you are making it uselessly complicated.

  6. #46
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Reading From File Question??

    Well, Yes, it's better to use concat method, in order to get string with complete numbers separated by spaces.
    Than simpy use:
    Java Code:
    string += Integer.parseInt(temp) + " ";
    ...
    String[] stringArray = string.split(" ");

  7. #47
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Reading From File Question??

    How does your code relate to the OPs problem of getting an array full of ints?

  8. #48
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Reading From File Question??

    This:
    Java Code:
    scoresArray[i] = Integer.parseInt(temp);
    if replace with this:

    Java Code:
    string += Integer.parseInt(temp) + " ";
    will produce say: "20 33 12 33 1 54 67 "

    After:
    Java Code:
    String[] stringArray = string.split(" ");
    You will have array full of strings, such Integer.parseInt(string[i]) is of type int.

    And usind that, there is no need for this code:
    Java Code:
                while(inFile.hasNext()) {
                    counter++;
                    inFile.next();
                }
    Last edited by diamonddragon; 01-25-2012 at 08:27 PM.

  9. #49
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Reading From File Question??

    Using an ArrayList solves the problem. His restriction is an artificial one.

  10. #50
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Reading From File Question??

    Of course, this is just another easy way to solve problem, having in mind artificial restriction.
    Because this aproach don't need creating of new array each time int value if found, since array is produced in one step.
    Last edited by diamonddragon; 01-25-2012 at 08:40 PM.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Question about fast reading files in Java.
    By Martino in forum Suggestions & Feedback
    Replies: 0
    Last Post: 05-09-2011, 03:34 PM
  2. URL Reading question
    By g123456 in forum New To Java
    Replies: 5
    Last Post: 03-17-2010, 03:07 AM
  3. Replies: 11
    Last Post: 11-03-2009, 05:32 PM
  4. [SOLVED] File reading question
    By wiz0r in forum New To Java
    Replies: 5
    Last Post: 04-19-2009, 01:21 AM
  5. Question abt.reading xml file using java
    By gvi in forum Advanced Java
    Replies: 6
    Last Post: 11-08-2007, 05:48 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
  •