New answers tagged

0

It can be done with one stream like this: public static int calculateLuhnChecksum(String creditCardNumber) { int luhnSum = IntStream.range(0, creditCardNumber.length()) .mapToInt(i -> { char c = creditCardNumber.charAt(i); int value = Character.getNumericValue(c); if(isEven(i)) { // i % 2 != 0 - remember, index starts with 0, ...


3

I cannot help but ask myself if they don't teach you methods anymore today. Use methods whith meaningful names, use variables with meaningful names, when you do streams use line breaks with one operation per line. Refrain from supposedly "clever tricks" like doing an actual calculation in an if. Rewrite of your code (whithout testing or ...


0

To only have one stream, push down the decision of separating odd and even values: int sum = IntStream.range(0, creditCardNumber1.length()) .map(i -> { int value = Character.getNumericValue(creditCardNumber1.charAt(i)); if (i % 2 == 0) { value = value * 2; } return value; }) .map(value -> value &...


2

Braces should always be used even if the body of the if or else contains only a single statement. Missing input validation: shift=0 causes an infinite loop. One variable per declaration: as @riskypenguin also suggested, avoid declarations in one line like int i = 0, j = Math.min(len, src.length()); Using IntStream could be an alternative: private static ...


2

You should generally avoid multiple variable initializations on one line: private int i = 0; private int j = Math.min(len, src.length()); is more readable. EDIT: The following suggestion is left in for reference, but is based on an incorrect assumption. From your sample output I would assume the following desired behaviour as well: streamStrings("...


2

Naming things There are several ways in which you can improve the way you name things. For example, don't do unnecessary abbreviations. Why NetworkBuff instead of NetworkBuffer? If you do want to abbreviate, make sure you do it the same way as other things do. For example, as you've already seen, the standard library provides std::streambuf, so NetworkBuf ...


2

I tried to compile it in Visual Studio 2019. I got several warnings and 1 error. I would recommend the following change to the SocketIOError class because the what function caused a warning: class SocketIOError : public std::exception { private: std::string type; std::string msg; public: SocketIOError(std::string t) : type(t) {...


Top 50 recent answers are included