r/javahelp • • Dec 06 '20

(Beginner in Java) I've made a program that counts the most frequent character in a string.

My issue is that the string is "IN A RIGHT TRIANGLE THE SQUARE OF THE HYPOTENUSE EQUALS THE SUM OF THE SQUARES OF THE LEGS" and it the output always shows up blank. But when I shorten the string, the output works. If anyone could help me that would be greatly appreciated.

https://pastebin.com/pqVu7QQz

18 Upvotes

18 comments sorted by

•

u/AutoModerator Dec 06 '20

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

25

u/1-12TH Dec 06 '20 edited Dec 06 '20

So your code does works, the issue is just that space (' ', ASCII 32) is the most frequently occuring char in the string. I changed your code to this:

String str = "IN_A_RIGHT_TRIANGLE_THE SQUARE_OF_THE_HYPOTENUSE_EQUALS_THE_SUM_OF_THE_SQUARES_OF_THE_LEGS";

And the result is:

Max occurring character is: _

3

u/[deleted] Dec 06 '20

ohhh, ok. Thank you!

3

u/Fenxis Dec 06 '20

You might want to ignore / strip whitespace from the string but that might be down to the question/requirements.

2

u/Python4fun Dec 07 '20

Another good way to debug this would be to wrap the answer with some other character like dashes (or anything)

10

u/Camel-Kid 18 year old gamer Dec 06 '20

It is because result is an actual space which comes up as "blank" however it is a character.

3

u/mIb0t Dec 06 '20

Because you have 16 whitespaces between all the words?

3

u/steave435 Dec 06 '20

Since your problem is already solved, I'll give you a general tip:

Your code is perfectly fine and looks good for a beginner, but you could make it a bit cleaner by doing the .toLowerCase in the getMax method rather than in main. The idea is that you should have "separation of concerns", which means that a method should take care of all the operations that it needs to do on its own - if the method is supposed to find the most common char in a string regardless of case, then you shouldn't have to do .toLowerCase before calling it. You should just be able to send it any string and get the most common char back. If you do want to do it in main anyway, you can write str.toLowerCase in the method call instead of having to store the result in a separate str2 variable.

2

u/_jetrun Dec 07 '20

but you could make it a bit cleaner by doing the .toLowerCase in the getMax method rather than in main

I disagree. If getMaxOccuringChar was a method from some third-party library, I wouldn't want it to just assume that 'a' and 'A' are the same character. I, as the consumer of it, want to make that decision. I may decide case matter or it doesn't. OP has the right idea here and 'principle of least surprise' applies.

you can write str.toLowerCase in the method call instead of having to store the result in a separate str2 variable

Why?

4

u/steave435 Dec 07 '20

I disagree. If getMaxOccuringChar was a method from some third-party library, I wouldn't want it to just assume that 'a' and 'A' are the same character. I, as the consumer of it, want to make that decision. I may decide case matter or it doesn't. OP has the right idea here and 'principle of least surprise' applies.

That's true, and I considered that, but since this is clearly a one off, my interpretation is that the method is intended to do that. It should also be renamed to clarify that in that case, but I didn't want to nag about too many things or make it too complicated by bringing up every possibility.

Why?

No reason to make whoever reads your code memorize more variables than needed, no need to use extra memory, no reason to make readers read more code when less will do (unless shortening it will make it hard to read, which it wouldn't in this case) etc.

0

u/_jetrun Dec 07 '20

It should also be renamed to clarify that in that case

It should be renamed IF the function is case insensitive. The default should be that test is case sensitive. Who assumes that 'a' and 'A' chars are the same?

no need to use extra memory

That's not a real concern. str.toLowerCase() creates a new string whether or not it is assigned to a variable. This is what's known as 'premature optimization', or solving non-existent problems.

Listen, there are changes related to the correctness that could be made (for example, the actual implementation could be made more robust if replaced by a single loop and no array map), but I don't think these kinds of formatting issues are worth for OP to be concerned about. Besides, I'm a huge fan of automatic-formatters anyway. On commit, I run an aggressive code formatter to auto-format my code based on rules I specify.

1

u/steave435 Dec 07 '20

It should be renamed IF the function is case insensitive

Hence why I said "in that case".

That's not a real concern. str.toLowerCase() creates a new string whether or not it is assigned to a variable. This is what's known as 'premature optimization', or solving non-existent problems.

Yes, but I'd bet that explicitly declaring a new variables could impact how it's treated by the garbage collector. Regardless, even if that's wrong, way to focus in on the by far least important reason. Please try to be constructive instead of trying to be right.

No, it's not an important change either way, I mentioned it primarily because doing it the way OP did takes more effort, so he probably did it the way he did because he didn't realize that he could do it more easily.

0

u/_jetrun Dec 07 '20

Hence why I said "in that case".

Hold on a second here, that's not what you said. Currently the function is case sensitive. You said the function should be renamed if OP doesn't make any changes (i.e. if OP leaves the function as case sensitive). I disagreed. The function name is fine as is given the current behaviour because case sensitivity is the default expected behaviour.

Yes, but I'd bet that explicitly declaring a new variables could impact how it's treated by the garbage collector.

Well you'd be wrong.

Please try to be constructive instead of trying to be right.

I see a lot of bad advice given on this forum by well-meaning junior programmers. For example, just in this last post you argued that a temporary variable will impact garbage collector. I'll tell you right now, it will have NO effect on the garbage collector. But you make it seem like you know what you're talking about and beginners will trust that, even though you're wrong.

0

u/steave435 Dec 07 '20

You'll notice that I didn't say anything I wasn't sure about (or which more up to personal taste) to him. You were the one asking about the why and I threw that in as a potential benefit. Again, the main point was letting him know that he could simplify the code to save time for himself.

You're still trying to be right rather than constructive, so I'm done with this conversation.

0

u/_jetrun Dec 07 '20

You were the one asking about the why

Because you gave this advice to OP, so it's only fair that you explain the reason for this advice.

You were the one asking about the why and I threw that in as a potential benefit.

But that isn't a potential benefit ... It's completely wrong. Your advice about renaming the function was wrong. OP actually had the right idea in the way they structured their code (not the actual implementation for the relevant function - that could use a little clean-up here and there, but I'm not worried about it for toy project).

You're still trying to be right rather than constructive

If you were in my shoes, and you saw someone give bad advice to a beginner, how would you engage that person to let them know they gave bad advice in a way that is 'constructive' and doesn't hurt their feelings?

1

u/steave435 Dec 07 '20

If you were in my shoes, and you saw someone give bad advice to a beginner, how would you engage that person to let them know they gave bad advice in a way that is 'constructive' and doesn't hurt their feelings?

Sure, that's actually worth talking about.

Your first post was perfectly fine. As you see, I responded to it by saying that if you're right about OPs intent, then you're right. Then I responded to your question of why I thought so, so I explained to you (not OP, so not a beginner) why I thought so. I provided two that I still stand by and one potential benefit that turned out to be wrong. So far so good.

There's plenty of room there for a constructive conversation. For the first part, you could argue that you think I'm wrong about his intentions, that even if I'm right that's still a bad approach or you could agree that depending on his intentions, both approaches are fine, and agree to disagree about our interpretations of those intentions.

Instead you chose to look for a single line that you could misconstrue as me disagreeing with you and burned that strawman.

For the second part, you again chose to focus in on the one thing that was wrong, "forgetting" and ignoring that the topic was which style should be used. Absolutely point out my mistake here, I learned from that so I appreciate it, but a normal person trying to be constructive would say something along the lines of "yeah, these two points are fair, but that other one is wrong", unless you disagree with them for some reason I can't think of.

You choose to focus only on whatever you, right or wrong, can interpret as being wrong so that you can be right rather than trying to work together to produce the best possible result, where both good and bad things are relevant.

To be clear, I'm not offended, I just don't find non-constructive conversations to be worth my time.

0

u/[deleted] Dec 06 '20

The whitespace is a character...

0

u/manasmaity Dec 06 '20

It’s working but returning space character, as that is the most occurring.