Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

split arrays has only index zero

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a Scanner object to read from a file and using split to store the items in an array. I'm confused about how to access the array because the array has only index of zero.



How do I split the file items into individual individual array index?
 
Bartender
Posts: 7065
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nextLine() already stops when it sees a new-line, so just call nextLine() repeatedly.
 
Omadegbo Ojo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I  want to store the items in an array so I can further use them elsewhere. I saw a similar post which suggested I use ArrayList which I have. That solved my issue. Thanks.
 
Marshal
Posts: 68899
275
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid I can see four errors in your code.
  • 1: Don't use hasNextLine() when reading a file. There is a risk that the file will end with a blank line, and that will be read incorrectly. Use while (myScanner.hasNext()) ... instead.
  • 2: You are using the wrong delimiter. You can be confident that you won't find \n anywhere inside the line you are reading.
  • 3: You aren't using the powers of your Scanner properly. Don't try reading the line and passing the whole line to your Movie constructor. If possible, use nextInt() to read the date, and similar.
  • 4: You aren't closing the Scanner when you have finished. The best way to close such a Scanner is with try with resources. You are risking keeping the file open and it will be locked and inaccessible for reading later.
  •  
    Campbell Ritchie
    Marshal
    Posts: 68899
    275
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why are you using a setXXX() method to get the location of a file? That looks wrong, too.
     
    Carey Brown
    Bartender
    Posts: 7065
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

  • 1: Don't use hasNextLine() when reading a file. There is a risk that the file will end with a blank line, and that will be read incorrectly. Use while (myScanner.hasNext()) ... instead.
  • Won't next() return just a piece of a line if there are spaces in it? Then you'd have to concatenate all the pieces together to get a whole line. Also, wouldn't next() treat a  space and a new-line the same so that you wouldn't know when you've reached the end of a line? Or are you thinking of changing the delimiter for this?
     
    Campbell Ritchie
    Marshal
    Posts: 68899
    275
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not next() but hasNext(). Many files end with an empty line, so hasNextLine() returns true and hasNext() returns false.
    Maybe I was a bit overoptimistic suggesting nextInt() to find the date of the movie, though. You can search for ints and words, but as you say, you would probably have to concatenate various bits of text together before use.
     
    Omadegbo Ojo
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Why are you using a setXXX() method to get the location of a file? That looks wrong, too.



    What's wrong with using setter to get the file? Should I put it in a try..catch?

    Thanks
     
    Campbell Ritchie
    Marshal
    Posts: 68899
    275
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A setXXX method should set something and a getXXX method should get something. You are trying to do three things in that statement; you should do one thing in one statement, as far as possible.Then the setXXX() method can have void instead of a return type, as setXXX() methods usually do.
     
    Omadegbo Ojo
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:A setXXX method should set something and a getXXX method should get something. You are trying to do three things in that statement; you should do one thing in one statement, as far as possible.Then the setXXX() method can have void instead of a return type, as setXXX() methods usually do.



    The thinking behind my code was to use setXX/getXX to assign and retrieve the file path which will serve as input for the scanner object but I guess it doesn't serve much purpose in this case.
     
    Campbell Ritchie
    Marshal
    Posts: 68899
    275
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the pie, OO
     
    It was the best of times. It was the worst of times. It was a tiny ad.
    Two software engineers solve most of the world's problems in one K&R sized book
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic