Results 1 to 10 of 10
  1. #1
    nickerhardt is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default Read txt file into arraylist

    Hey

    I have a txt file that looks like this:
    at:@
    too:2

    I am triyng to get those words into an arraylist but i cannot get it to work.
    I am doing it through an arraylist. In the arraylist the words are supposed to be in this order:
    at
    @
    too
    2

    and so on.

    This is what I have so far:
    Scanner slang=new Scanner(new File(slangdict));

    slang.useDelimiter(":");
    //Tæller til oprettelse af array

    while(slang.hasNext()) {

    System.out.println(slang.next());

    counter++;

    }

    //System.out.println(counter);

    String[] slanglist=new String[counter];



    int count=0;

    while(slang.hasNext())

    {

    slanglist[count]=slang.next();

    count++;

    }

    for(int i=0;i<slanglist.length;i++) {

    System.out.println(slanglist[i]); }

    The problem is, that when I print my arraylist on the scrren it just prints null.

    What have I done wrong?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    I'd say the second while loop is never executed.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    nickerhardt is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    I cannot see why the second while loop shouldn't be executed?
    By the way - when I say say the arraylist prints null to the screen, it does so numerous times, like this:

    null
    null
    null
    null

    and so on.

    Have you got any other solutions?

    They will be highly appreciated?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    You loop through the Scanner using next() and hasNext()...when it exists that loop the Scanner has reached the end of the file, so what do you think the next call to hasNext() (in the second loop) will return?

  5. #5
    nickerhardt is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    All right, I guess it will return "false" and the while loop wont't be executed. But how do I "restart" the scanner/hasNext\next" so it will read from the start of the file?
    I guess thats the way to start the second while loop.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Why not simply store everything in the first loop?
    Using an ArrayList rather than a straight array?
    In fact, if this is supposed to be mapping the left hand side to the right then you're probably looking at a Map.

    Unless this is an exercise, of course.

  7. #7
    nickerhardt is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Actually, I am supposed to put it into a hashmap, but i can't use the bufferedreader. So the only thing I could think of was to put it into an array first and the later on store it in a hashmap.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Don't use a delimiter on the scanner, read a line, split it and add it to a map. Done.

    I have no idea what a Reader is needed for.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    nickerhardt is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Got it now - Thanks for the help guys!

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Glad you got it sorted. Mark the thread as Solved, please.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. how to read the ArrayList inside HashMap
    By koddy in forum New To Java
    Replies: 6
    Last Post: 07-15-2010, 02:41 PM
  2. Read txt file to arrayList
    By koddy in forum New To Java
    Replies: 14
    Last Post: 04-29-2010, 06:15 PM
  3. Replies: 0
    Last Post: 02-11-2009, 10:53 AM
  4. Replies: 3
    Last Post: 01-29-2009, 11:20 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM

Posting Permissions

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