How to add EOF in a map
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
add a comment |
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
You have code that handles the end of file, so you know when the file ends. Maybe an indication that you've read anEOF
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
add a comment |
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
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
java
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 anEOF
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
add a comment |
You have code that handles the end of file, so you know when the file ends. Maybe an indication that you've read anEOF
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
add a comment |
1 Answer
1
active
oldest
votes
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
''
oruFFFF
), 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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
''
oruFFFF
), 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.
add a comment |
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
''
oruFFFF
), 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.
add a comment |
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
''
oruFFFF
), 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.
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
''
oruFFFF
), 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.
answered Nov 26 '18 at 2:00
ruakhruakh
127k14205258
127k14205258
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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