• Post Reply Bookmark Topic Watch Topic
  • New Topic

Missing last entry in a text file  RSS feed

 
Chris Kislow
Greenhorn
Posts: 21
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code
my problem is that I have a text file with words in it and it seems to not read the last line in the text file, any help
the file has
hello
goodbye
take care
hello again

but hello again is missing  from the output

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To see what is happening add a print statement after each statement that reads from the file that prints what was read.
 
Chris Kislow
Greenhorn
Posts: 21
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:To see what is happening add a print statement after each statement that reads from the file that prints what was read.


looking at what you said at line 14 the code hit that line and hen give me an 'Exception in thread' error
not sure how to fix this and help

thanks for you help


 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are reading the files wrongly. You never get null from any Scanner methods, except those whose names start find... So your line will never be null, and your null test won't help you. In fact what will happen is that you get a no such element error when you get beyond the last line.
Suggestions:-
  • 1: Don't read both files in the same loop. The idea is to read one file at a time. You will get all sorts of weird and wonderful problems if you are trying to read fileA when you have run out of lines in fileB.
  • 2: Use try with resources←link to close the Scanners. It is neater and more reliable than an explicit close() call.
  • 3: Use hasNext() or similar for your loops. Read about the Scanner's methods here.
  • You will end up with code rather like this:-Read about Paths in the Java™ Tutorials.
    Why do you have // in that path string? Does that actually work? That looks like an unnamed directory with another directory inside it.
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You don't need double forward-slashes in you path name. It's only back-slash that needs doubling.
     
    Chris Kislow
    Greenhorn
    Posts: 21
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:You are reading the files wrongly. . . . Why do you have // in that path string? Does that actually work? That looks like an unnamed directory with another directory inside it.




    that worked great I read in both files no problem and I removed the // so that works fine

    now where I'm having issues with is how to proceeed next, I seem to over think things which (from ym understanding) in programming is bad
    1. should I use a while loop to compare to see if word from file B is in file A
    like:



    2. should I create a method called compare an do the compare in that method

    3. create another class to do the same as in number 2

    sorry for so many questions, just trying.
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Need some clarification on your requirements.

    First off, scanner.next() will get you the next word. If your file has a line "hello again", do you want to deal with it as a line, or do you want words? Words would give you two separate entries: "hello" and "again". When you say 'keyword', do you mean that this line will show up when you do A.contains("again") or must you look for an exact match of the line, as in A.contains("hello again")?

    You'll need to load fileA and fileB into their own collections, e.g. HashSet.

    Once you've got collectionA and collectionB, are you looking for items that are in both A and B (i.e. 'intersection')? What do you want to do once you find these items? Print them? Process them some more? Do you also need to know if an item is in A but not B, or in B and not A? What would you like to do in those cases?
     
    Knute Snortum
    Sheriff
    Posts: 4287
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    General rules of thumb about methods and classes are:

    1) A method should do only one thing and do it well.  Since it is doing something, its name should be a verb.  Methods that are longer than about 15 to 20 lines should be broken down (refactored) into smaller methods.

    2) In general, and class is a "thing" and should be named with a noun, but keep in mind that a "thing" can be abstract, like a controller or validator.
     
    Chris Kislow
    Greenhorn
    Posts: 21
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:Need some clarification on your requirements.

    First off, scanner.next() will get you the next word. If your file has a line "hello again", do you want to deal with it as a line, or do you want words? Words would give you two separate entries: "hello" and "again". When you say 'keyword', do you mean that this line will show up when you do A.contains("again") or must you look for an exact match of the line, as in A.contains("hello again")?

    You'll need to load fileA and fileB into their own collections, e.g. HashSet.

    Once you've got collectionA and collectionB, are you looking for items that are in both A and B (i.e. 'intersection')? What do you want to do once you find these items? Print them? Process them some more? Do you also need to know if an item is in A but not B, or in B and not A? What would you like to do in those cases?


    for the scanner part = I need the whole word, what I mean by keyword is the word from the keyword file to compare it to the fileA to see if that keyword is in that fileA
    once I found it then I need to check if there is an equals sign after that value and store that value. if no equals sign then check the next one.
    does this clear it up?
     
    Knute Snortum
    Sheriff
    Posts: 4287
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would think you could read the file a line at a time with scanner.nextLine().  Store all those lines in a List like ArrayList.  Get a keyword from the keyword file, then loop through the List to see if that line contains the keyword.
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Chris Kislow wrote:for the scanner part = I need the whole word, what I mean by keyword is the word from the keyword file to compare it to the fileA to see if that keyword is in that fileA
    once I found it then I need to check if there is an equals sign after that value and store that value. if no equals sign then check the next one.
    does this clear it up?

    Perhaps a more real-world example would help clarify things. Is this what you mean?

    FileA

    Keyword file

    Using keyword "ABC" find line 1 in FileA. Do NOT find line 4 in FileA because the keyword comes after the equals (=).
    Using keyword "IJK" finnd line 3 in FileA.
    Lines  2 and 4 in FileA will not be found.
     
    Chris Kislow
    Greenhorn
    Posts: 21
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:. . . Perhaps a more real-world example would help clarify things. Is this what you mean?

    FileA

    Keyword file

    Using keyword "ABC" find line 1 in FileA. Do NOT find line 4 in FileA because the keyword comes after the equals (=).
    Using keyword "IJK" finnd line 3 in FileA.
    Lines  2 and 4 in FileA will not be found.



    YES your correct
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's some pseudo code that might help you.
     
    Chris Kislow
    Greenhorn
    Posts: 21
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:Here's some pseudo code that might help you.



     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Seems reasonable. One case that would not work in your code:
    If you are using startsWith() to match the keyword then "ABC" will match both lines. A fix for that would be to append "=" to your keywords before adding them to your keyword list. That way you'd be trying to match "ABC=" and "ABCD=" which will now work correctly.

    Are you familiar with Maps?
     
    Chris Kislow
    Greenhorn
    Posts: 21
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:Seems reasonable. One case that would not work in your code:
    If you are using startsWith() to match the keyword then "ABC" will match both lines. A fix for that would be to append "=" to your keywords before adding them to your keyword list. That way you'd be trying to match "ABC=" and "ABCD=" which will now work correctly.

    Are you familiar with Maps?


    no I', familiar with Maps but I will start learning them, thanks for the help
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Chris Kislow wrote:. . .
    Line 1: Such comments belong outside the code tags.
    Line 3: Don't declare two variables on the same line. Don't initialise the Scanners to null.
    Don't read two files in the same loop if you can avoid it. You can copy the lines read into a List<String> and manipulate the contents of that List. The performance of an ArrayList will be much faster than reading from the file.
    Don't use the close() method of the Scanner instances: use try with resources instead. If there is an Exception causing the Scanners not to open properly, the close() call will suffer an Exception; this won't happen if you use try with resources.
    That method is much too long. Remember what Knute said about a method doing one thing. That method is doing at least three things, which shou‍ld each be done in their own method.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!