Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
Like Tree3Likes

Thread: Any help would be appreciated (reading from file, sorting, arrays)!

  1. #21
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Problem is that you've statically sized the int array to 10. Are you sure there's only going to be 10 elements?

    My approach would be to use an ArrayList (simply adding to an ArrayList increses its size). When done create an array from the ArrayList.

    Using the Arrays class will allow you to sort the int array.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    So your text file is in fact this:

    Java Code:
    {6, 2, 3, 4, 5, 1, 9, 7, 8}
    Is this a file that you've created or one given by your instructor? If it's one you've created, I'd try to simplify it and change it to something like this:

    Java Code:
    6 2 3 4 5 1 9 7 8
    If it's a file given to you by your instructor, you'll want to read in your String and then manipulate it. I would get rid of the "{", "," and "}" Strings by using String's replace(...) or replaceAll(...) methods.

    After simplifying the String, it will be much easier for you to use the data it holds.

    Remember that after every change in your code, compile, run, test to check for syntax and logic errors. Sprinkle your code heavily with System.out.println(...) statements to test what your critical variables are holding (you'll remove these println statements from the final bug-proof product).

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Reading a file into 5 arrays
    By mossberg in forum New To Java
    Replies: 2
    Last Post: 10-30-2011, 09:07 PM
  2. Sorting arrays
    By Lex in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 02:19 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Sorting Two Arrays
    By Faye Rett in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 01:00 AM
  5. Arrays and File Reading
    By DaFlake in forum New To Java
    Replies: 3
    Last Post: 08-12-2009, 05:28 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
  •