How to add EOF in a map












0















I'm reading this file:



ab ab cab


and counting the number of times each character appears and putting it in a map:



Map<Character, Integer> map = new TreeMap<Character, Integer>();


The resulting map looks like this:



{ =2, a=3, b=3, c=1 }


but I need the result to be this:



{ =2, a=3, b=3, c=1, EOF=1}


How do I add EOF=1 to the map? This is for an assignment and says I need that EOF=1.










share|improve this question

























  • You have code that handles the end of file, so you know when the file ends. Maybe an indication that you've read an EOF character?

    – Chris Thompson
    Nov 26 '18 at 1:55











  • I need to be able to put this EOF from a map into a priority queue and then into a binary tree. But I don't understand how to add it into the map

    – 2cs
    Nov 26 '18 at 2:00
















0















I'm reading this file:



ab ab cab


and counting the number of times each character appears and putting it in a map:



Map<Character, Integer> map = new TreeMap<Character, Integer>();


The resulting map looks like this:



{ =2, a=3, b=3, c=1 }


but I need the result to be this:



{ =2, a=3, b=3, c=1, EOF=1}


How do I add EOF=1 to the map? This is for an assignment and says I need that EOF=1.










share|improve this question

























  • You have code that handles the end of file, so you know when the file ends. Maybe an indication that you've read an EOF character?

    – Chris Thompson
    Nov 26 '18 at 1:55











  • I need to be able to put this EOF from a map into a priority queue and then into a binary tree. But I don't understand how to add it into the map

    – 2cs
    Nov 26 '18 at 2:00














0












0








0








I'm reading this file:



ab ab cab


and counting the number of times each character appears and putting it in a map:



Map<Character, Integer> map = new TreeMap<Character, Integer>();


The resulting map looks like this:



{ =2, a=3, b=3, c=1 }


but I need the result to be this:



{ =2, a=3, b=3, c=1, EOF=1}


How do I add EOF=1 to the map? This is for an assignment and says I need that EOF=1.










share|improve this question
















I'm reading this file:



ab ab cab


and counting the number of times each character appears and putting it in a map:



Map<Character, Integer> map = new TreeMap<Character, Integer>();


The resulting map looks like this:



{ =2, a=3, b=3, c=1 }


but I need the result to be this:



{ =2, a=3, b=3, c=1, EOF=1}


How do I add EOF=1 to the map? This is for an assignment and says I need that EOF=1.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 1:56









ruakh

127k14205258




127k14205258










asked Nov 26 '18 at 1:52









2cs2cs

133




133













  • You have code that handles the end of file, so you know when the file ends. Maybe an indication that you've read an EOF character?

    – Chris Thompson
    Nov 26 '18 at 1:55











  • I need to be able to put this EOF from a map into a priority queue and then into a binary tree. But I don't understand how to add it into the map

    – 2cs
    Nov 26 '18 at 2:00



















  • You have code that handles the end of file, so you know when the file ends. Maybe an indication that you've read an EOF character?

    – Chris Thompson
    Nov 26 '18 at 1:55











  • I need to be able to put this EOF from a map into a priority queue and then into a binary tree. But I don't understand how to add it into the map

    – 2cs
    Nov 26 '18 at 2:00

















You have code that handles the end of file, so you know when the file ends. Maybe an indication that you've read an EOF character?

– Chris Thompson
Nov 26 '18 at 1:55





You have code that handles the end of file, so you know when the file ends. Maybe an indication that you've read an EOF character?

– Chris Thompson
Nov 26 '18 at 1:55













I need to be able to put this EOF from a map into a priority queue and then into a binary tree. But I don't understand how to add it into the map

– 2cs
Nov 26 '18 at 2:00





I need to be able to put this EOF from a map into a priority queue and then into a binary tree. But I don't understand how to add it into the map

– 2cs
Nov 26 '18 at 2:00












1 Answer
1






active

oldest

votes


















1














EOF is not a character, so a Map<Character, Integer> can't use EOF as a key. So you'll need to adjust your approach.



Here are two options:




  • You can choose some specific character that won't otherwise appear in your input (such as '' or uFFFF), and use that character to represent EOF.

  • You can use a Map<Integer, Integer> instead. Every character can be promoted to an integer (since integers range from −231 to 231−1, and characters only from 0 to 216−1), so this lets you represent every character and EOF.

  • You can store the count of EOF in a separate int variable.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473830%2fhow-to-add-eof-in-a-map%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    EOF is not a character, so a Map<Character, Integer> can't use EOF as a key. So you'll need to adjust your approach.



    Here are two options:




    • You can choose some specific character that won't otherwise appear in your input (such as '' or uFFFF), and use that character to represent EOF.

    • You can use a Map<Integer, Integer> instead. Every character can be promoted to an integer (since integers range from −231 to 231−1, and characters only from 0 to 216−1), so this lets you represent every character and EOF.

    • You can store the count of EOF in a separate int variable.






    share|improve this answer




























      1














      EOF is not a character, so a Map<Character, Integer> can't use EOF as a key. So you'll need to adjust your approach.



      Here are two options:




      • You can choose some specific character that won't otherwise appear in your input (such as '' or uFFFF), and use that character to represent EOF.

      • You can use a Map<Integer, Integer> instead. Every character can be promoted to an integer (since integers range from −231 to 231−1, and characters only from 0 to 216−1), so this lets you represent every character and EOF.

      • You can store the count of EOF in a separate int variable.






      share|improve this answer


























        1












        1








        1







        EOF is not a character, so a Map<Character, Integer> can't use EOF as a key. So you'll need to adjust your approach.



        Here are two options:




        • You can choose some specific character that won't otherwise appear in your input (such as '' or uFFFF), and use that character to represent EOF.

        • You can use a Map<Integer, Integer> instead. Every character can be promoted to an integer (since integers range from −231 to 231−1, and characters only from 0 to 216−1), so this lets you represent every character and EOF.

        • You can store the count of EOF in a separate int variable.






        share|improve this answer













        EOF is not a character, so a Map<Character, Integer> can't use EOF as a key. So you'll need to adjust your approach.



        Here are two options:




        • You can choose some specific character that won't otherwise appear in your input (such as '' or uFFFF), and use that character to represent EOF.

        • You can use a Map<Integer, Integer> instead. Every character can be promoted to an integer (since integers range from −231 to 231−1, and characters only from 0 to 216−1), so this lets you represent every character and EOF.

        • You can store the count of EOF in a separate int variable.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 2:00









        ruakhruakh

        127k14205258




        127k14205258
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473830%2fhow-to-add-eof-in-a-map%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin