Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!

Campbell Ritchie

Marshal
+ Follow
since Oct 13, 2005
Cows and Likes
Cows
Total received
179
In last 30 days
1
Total given
285
Likes
Total received
5067
Received in last 30 days
58
Total given
147
Given in last 30 days
6
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Campbell Ritchie

I am away and shan't be asking any questions, but welcome and thank you for coming here to discuss the book
1 day ago
iOS
Another thing: you aren't closing the Scanner; that may leave the file handle open and cause a resource leak. The best way to avoid that is with try with resources.
You need to have the structure of the file matching the code; if you have different numbers of fields and token you will have no end of trouble. We all know that tight coupling is a Bad Thing, but you aren't going to be able to avoid it here. If you add whitespace, you are adding another token and Scanners usually read token by token. A nextLine() call will move you to the end of the current line and you can discard the rest of the line by not using its returned value, but that can only work if you want to read the whole of the rest of the line, or get rid of the whole of the rest of the line. In the case you are describing, you have a date on one line, then the location as one line, then three numbers, distance hours minutes. Then you go back to nextLine(). And that reads the remainder of the line after the minutes and all of a sudden the Scanner is reading one token ahead of itself. It is the same problem as here. Two possible solutions:-
  • 1: Add the next date after the minutes number (ouch!)
  • 2: Call nextLine() and ignore what it returns.
  • It you have a file like this:. . . the problem occurs after the 30 for minutes. You will end up with code like this:-Use the hike data constructor rather than set methods. Don't use a String for date; parse it to a LocalDate immediately.
    2 days ago
    Where I am sitting, FA means something else and you wouldn&apost find it to work. Please avoid such abbreviations.
    3 days ago
    . . . and welcome to the Ranch

    Moving question to a different forum because it is too difficult for “beginning”
    3 days ago

    A few minutes ago, I wrote:\\w won't find the apostrophe.

    Try "[\\w|']+" instead.
    4 days ago
    \\w won't find the apostrophe.
    4 days ago
    Please post any errata found or suspected here. I shall leave this one errratum in its own thrrread.

    vinayak shete wrote:. . .  hashCode() method (will return same hash code - as 1 for each object.) . . .

    That is a very effective way to make your hash map execute with all the speed and athleticism of a dying snail. Write a hashCode method that computes a hash code for each instance; Carey has already shown you a possible implementation. If you are using an immutable type as the “K” in your map (as you should anyway), you may be able to cache the hash code.
    4 days ago
    Why have you got that method static? The object‑oriented way to do things would be to make that an instance method of a particular List. If you follow Jeanne's suggestion, you will be able to see why you need a reference to the current node.

    Welcome to the Ranch Thank you for using the code button, but you should have put the [/code] part after the rest of the code. Don't worry; I can correct that for you.
    4 days ago
    Please post any errata found or suspected here. I shall leave this one errratum in its own thrrread.

    Kathir jeyap wrote:. . . . abstract class many getters and setters and the class has few getters and setters . . . .

    Which fields do those classes have? Do any of the getXXX and getXXX methods have the same name in subclass and superclass.

    By the way: Call the superclass superclass, not base class; even though the term base class might be better, that isn't the usual name in Java®.
    4 days ago
    There are ways to ensure that an error message is passed if the wrong data type is entered, allowing you to enter new data, and avoid all exceptions. I think Carey is right, but in case of doubt, ask your teacher for clarification.
    4 days ago

    Rob Spoor wrote:. . . $[.]+ will never match anything.

    . . . which is why you aren't matching the ellipses.
    4 days ago

    Knute Snortum wrote:Scanner is great for scanning text files, but I don't like it for getting input from the keyboard.  . . . .

    Sorry I didn&apost;t have the time to reply last night, but I think we agree on that point; once you have created a utility class to deal with the vagaries of Scanner, which as MW points out are badly documented in many places, you find keyboard input so much easier and more reliable. I think my KeyboardInputs class differs rather from your Inputter class, but I think we both find such classes much easier to use from the keyboard than a plain simple Scanner. The reason is that it is possible to ensure consistent format of the contents of a text file, so nextInt() actually finds an int, but you can't be sure there won't be errors when entering data live from the keyboard.
    I agree with MW that beginners aren't taught to use Scanner properly; in fact I suspect most teachers think nextLine() means, “read the next line,” rather than, “move to the next line”. The fact that the default delimiter is 1+ whitespace characters means a line end sequence can be incorporated into the current delimiter sequence, so people think something similar will work for nextLine().
    I can't see that the read() method is any good. It is a low‑level method which would need all the inputs casting to a char so as to reconstruct the text before parsing it. If you simply want to read line by line, why not use the old method with BufferedReader#readLine()?
    4 days ago