This commit is contained in:
@@ -14,7 +14,7 @@ public class Sum {
|
||||
// if character is whitespace
|
||||
if (!number.isEmpty()) {
|
||||
// if number is not empty
|
||||
sum = sum + Integer.parseInt(number.toString()); // add number to sum
|
||||
sum += Integer.parseInt(number.toString()); // add number to sum
|
||||
}
|
||||
number = new StringBuilder(); // empty the number by creating new empty StringBuilder
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public class Sum {
|
||||
// check for any remaining digits in number
|
||||
if (!number.isEmpty()) {
|
||||
// if number is not empty
|
||||
sum = sum + Integer.parseInt(number.toString()); // add number to sum
|
||||
sum += Integer.parseInt(number.toString()); // add number to sum
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ java Sum 1 2 -3 Expected output: 0." /><link rel="canonical" href="http://localh
|
||||
Examples:
|
||||
java Sum 1 2 3 Expected output: 6.
|
||||
java Sum 1 2 -3 Expected output: 0.">
|
||||
<meta itemprop="wordCount" content="1840">
|
||||
<meta itemprop="wordCount" content="2072">
|
||||
<meta name="twitter:card" content="summary">
|
||||
<meta name="twitter:title" content="Sum">
|
||||
<meta name="twitter:description" content="Task You need to create a Sum class which will sum integers from command line arguments and output the sum to console.
|
||||
@@ -198,7 +198,19 @@ java Sum 1 2 -3 Expected output: 0.">
|
||||
>Sum
|
||||
</a>
|
||||
|
||||
<ul class='hx:flex hx:flex-col hx:gap-1 hx:relative hx:before:absolute hx:before:inset-y-1 hx:before:w-px hx:before:bg-gray-200 hx:before:content-[""] hx:dark:before:bg-neutral-800 hx:ltr:pl-3 hx:ltr:before:left-0 hx:rtl:pr-3 hx:rtl:before:right-0 hx:ltr:ml-3 hx:rtl:mr-3'></ul>
|
||||
<ul class='hx:flex hx:flex-col hx:gap-1 hx:relative hx:before:absolute hx:before:inset-y-1 hx:before:w-px hx:before:bg-gray-200 hx:before:content-[""] hx:dark:before:bg-neutral-800 hx:ltr:pl-3 hx:ltr:before:left-0 hx:rtl:pr-3 hx:rtl:before:right-0 hx:ltr:ml-3 hx:rtl:mr-3'><li>
|
||||
<a
|
||||
href="#solution"
|
||||
class="hx:flex hx:rounded-sm hx:px-2 hx:py-1.5 hx:text-sm hx:transition-colors [word-break:break-word] hx:cursor-pointer [-webkit-tap-highlight-color:transparent] [-webkit-touch-callout:none] hx:contrast-more:border hx:gap-2 hx:before:opacity-25 hx:before:content-['#'] hx:text-gray-500 hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:dark:text-neutral-400 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50 hx:contrast-more:text-gray-900 hx:contrast-more:dark:text-gray-50 hx:contrast-more:border-transparent hx:contrast-more:hover:border-gray-900 hx:contrast-more:dark:hover:border-gray-50"
|
||||
>Solution</a>
|
||||
</li>
|
||||
<li>
|
||||
<a
|
||||
href="#solution-1"
|
||||
class="hx:flex hx:rounded-sm hx:px-2 hx:py-1.5 hx:text-sm hx:transition-colors [word-break:break-word] hx:cursor-pointer [-webkit-tap-highlight-color:transparent] [-webkit-touch-callout:none] hx:contrast-more:border hx:gap-2 hx:before:opacity-25 hx:before:content-['#'] hx:text-gray-500 hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:dark:text-neutral-400 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50 hx:contrast-more:text-gray-900 hx:contrast-more:dark:text-gray-50 hx:contrast-more:border-transparent hx:contrast-more:hover:border-gray-900 hx:contrast-more:dark:hover:border-gray-50"
|
||||
>Solution</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
|
||||
</li></ul>
|
||||
@@ -355,7 +367,15 @@ java Sum 1 2 -3 Expected output: 0.">
|
||||
</div></div></div></aside>
|
||||
|
||||
<nav class="hextra-toc hx:order-last hx:hidden hx:w-64 hx:shrink-0 hx:xl:block hx:print:hidden hx:px-4" aria-label="table of contents">
|
||||
<div class="hextra-scrollbar hx:sticky hx:top-16 hx:overflow-y-auto hx:pr-4 hx:pt-6 hx:text-sm [hyphens:auto] hx:max-h-[calc(100vh-var(--navbar-height)-env(safe-area-inset-bottom))] hx:ltr:-mr-4 hx:rtl:-ml-4"><p class="hx:mb-4 hx:font-semibold hx:tracking-tight">On this page</p><ul></ul><ul></ul>
|
||||
<div class="hextra-scrollbar hx:sticky hx:top-16 hx:overflow-y-auto hx:pr-4 hx:pt-6 hx:text-sm [hyphens:auto] hx:max-h-[calc(100vh-var(--navbar-height)-env(safe-area-inset-bottom))] hx:ltr:-mr-4 hx:rtl:-ml-4"><p class="hx:mb-4 hx:font-semibold hx:tracking-tight">On this page</p><ul>
|
||||
<li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6">
|
||||
<a class="hx:font-medium hx:inline-block hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:break-words" href="#solution">Solution
|
||||
</a>
|
||||
</li></ul><ul>
|
||||
<li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6">
|
||||
<a class="hx:font-medium hx:inline-block hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:break-words" href="#solution-1">Solution
|
||||
</a>
|
||||
</li></ul>
|
||||
<div class="hx:mt-8 hx:border-t hx:bg-white hx:pt-8 hx:shadow-[0_-12px_16px_white] hx:dark:bg-dark hx:dark:shadow-[0_-12px_16px_#111] hx:sticky hx:bottom-0 hx:flex hx:flex-col hx:items-start hx:gap-2 hx:pb-8 hx:border-gray-200 hx:dark:border-neutral-800 hx:contrast-more:border-t hx:contrast-more:border-neutral-400 hx:contrast-more:shadow-none hx:contrast-more:dark:border-neutral-400">
|
||||
<button aria-hidden="true" id="backToTop" onClick="scrollUp();" class="hx:cursor-pointer hx:transition-all hx:duration-75 hx:opacity-0 hx:text-xs hx:font-medium hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-100 hx:contrast-more:text-gray-800 hx:contrast-more:dark:text-gray-50">
|
||||
<span>Scroll to top</span>
|
||||
@@ -464,12 +484,13 @@ java Sum 1 2 -3 Expected output: 0.">
|
||||
</li>
|
||||
</ol>
|
||||
<hr>
|
||||
<h1>Solution</h1><p>After reading about <a href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html"target="_blank" rel="noopener"><code>String</code></a>, <a href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html"target="_blank" rel="noopener"><code>Integer</code></a>, <a href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#err"target="_blank" rel="noopener"><code>System.err</code></a> we now know about some usefull methods:</p>
|
||||
<h2>Solution<span class="hx:absolute hx:-mt-20" id="solution"></span>
|
||||
<a href="#solution" class="subheading-anchor" aria-label="Permalink for this section"></a></h2><p>After reading about <a href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html"target="_blank" rel="noopener"><code>String</code></a>, <a href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html"target="_blank" rel="noopener"><code>Integer</code></a>, <a href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#err"target="_blank" rel="noopener"><code>System.err</code></a> we now know about some usefull methods:</p>
|
||||
<ul>
|
||||
<li><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-"target="_blank" rel="noopener"><code>Integer.parseInt(String s, int radix)</code></a> which parses the string argument as a signed integer in the radix specified by the second argument. So it basically converts a number from the <code>String</code> data type to <code>int</code>.</li>
|
||||
<li><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isWhitespace-char-"target="_blank" rel="noopener"><code>Character.isWhitespace(char ch)</code></a> which checks if a character <code>ch</code> is a space symbol or not.</li>
|
||||
</ul>
|
||||
<p>Now let’s start coding. Firstly let’s define the structure of our program. I will be putting the name of the file at the first line comment in a file and its path if its nessesary.</p>
|
||||
<p>Now let’s start coding. Firstly let’s define the structure of our program. I will be putting the name of the file and its path (if it’s nessesary) at the first line comment.</p>
|
||||
<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
|
||||
|
||||
<div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// Sum.java</span>
|
||||
@@ -488,7 +509,7 @@ java Sum 1 2 -3 Expected output: 0.">
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<p>Okay that’s done. What do we do next? Let’s look at <code>String[] args</code> argument to our <code>main</code> method. It represents an array of command line arguments which we need to sum. So know we made our task a little bit easier. Now we can just say that we need to find sum of elements of array <code>args</code>.</p>
|
||||
<p>Okay that’s done. What do we do next? Let’s look at <code>String[] args</code> argument to our <code>main</code> method. It represents an array of command line arguments which we need to sum. So now we made our task a little bit easier. Now we can just say that we need to find sum of elements of array <code>args</code>.</p>
|
||||
<p>How are we going to do it though? First let’s understand what we can do with this array. Let’s modify our class a little bit.</p>
|
||||
<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
|
||||
|
||||
@@ -604,7 +625,7 @@ java Sum 1 2 -3 Expected output: 0.">
|
||||
<div class="hx:mt-6 hx:leading-7 hx:first:mt-0"><p>Here program gives us 1 instead of 0, because despite not having any numbers in the arguments a single whitespace is still an argument.</p></div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Now let’s try not obly to count our arguments but to list them as well. Let’s modify our program a little bit more.</p>
|
||||
<p>Now let’s try not only to count our arguments but to list them as well. Let’s modify our program a little bit more.</p>
|
||||
<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
|
||||
|
||||
<div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// Sum.java</span>
|
||||
@@ -849,33 +870,6 @@ java Sum 1 2 -3 Expected output: 0.">
|
||||
<p>So we figured out that we need to cast <code>argument</code> to integer type. Let’s change our code and test it.</p>
|
||||
<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
|
||||
|
||||
<div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// Sum.java</span>
|
||||
</span></span><span style="display:flex;"><span>
|
||||
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Sum</span> {
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String<span style="color:#f92672">[]</span> args) {
|
||||
</span></span><span style="display:flex;"><span> System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">"number of arguments: "</span> <span style="color:#f92672">+</span> args.<span style="color:#a6e22e">length</span>);
|
||||
</span></span><span style="display:flex;"><span>
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">int</span> sum <span style="color:#f92672">=</span> 0;
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">for</span> (String argument : args) {
|
||||
</span></span><span style="display:flex;"><span> sum <span style="color:#f92672">=</span> sum <span style="color:#f92672">+</span> Ineteger.<span style="color:#a6e22e">parseInt</span>(argument);
|
||||
</span></span><span style="display:flex;"><span> System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(argument);
|
||||
</span></span><span style="display:flex;"><span> System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(sum);
|
||||
</span></span><span style="display:flex;"><span> }
|
||||
</span></span><span style="display:flex;"><span>
|
||||
</span></span><span style="display:flex;"><span> System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(sum);
|
||||
</span></span><span style="display:flex;"><span> }
|
||||
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0">
|
||||
<button
|
||||
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
|
||||
title="Copy code"
|
||||
>
|
||||
<div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
<div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
|
||||
|
||||
<div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// Sum.java</span>
|
||||
</span></span><span style="display:flex;"><span>
|
||||
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Sum</span> {
|
||||
@@ -1140,9 +1134,96 @@ java Sum 1 2 -3 Expected output: 0.">
|
||||
<p class="hx:flex hx:items-center hx:font-medium"><svg height=16px class="hx:inline-block hx:align-middle hx:mr-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>Important</p>
|
||||
|
||||
<div class="hx:w-full hx:min-w-0 hx:leading-7">
|
||||
<div class="hx:mt-6 hx:leading-7 hx:first:mt-0"><p>Note, that we can’t just do <code>Integer.parseInt()</code></p></div>
|
||||
<div class="hx:mt-6 hx:leading-7 hx:first:mt-0"><p>Note, that we can’t just do <code>Integer.parseInt(number)</code>, because <code>number</code> is an instance of <code>StringBuilder</code>, not just a regular <code>String</code>. In order to get the current state of the string we can use <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#toString--"target="_blank" rel="noopener"><code>StringBuilder.toString()</code></a> method.</p></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="hx:overflow-x-auto hx:mt-6 hx:flex hx:flex-col hx:rounded-lg hx:border hx:py-4 hx:px-4 hx:border-gray-200 hx:contrast-more:border-current hx:contrast-more:dark:border-current hx:border-blue-200 hx:bg-blue-100 hx:text-blue-900 hx:dark:border-blue-200/30 hx:dark:bg-blue-900/30 hx:dark:text-blue-200">
|
||||
<p class="hx:flex hx:items-center hx:font-medium"><svg height=16px class="hx:inline-block hx:align-middle hx:mr-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>Note</p>
|
||||
|
||||
<div class="hx:w-full hx:min-w-0 hx:leading-7">
|
||||
<div class="hx:mt-6 hx:leading-7 hx:first:mt-0"><p>Also it’s worth mentioning, that we can shorten operations like thse</p>
|
||||
<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
|
||||
|
||||
<div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>arg1 <span style="color:#f92672">=</span> arg1 X arg2</span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0">
|
||||
<button
|
||||
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
|
||||
title="Copy code"
|
||||
>
|
||||
<div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
<div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<p>to</p>
|
||||
<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
|
||||
|
||||
<div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>arg1 X<span style="color:#f92672">=</span> arg2</span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0">
|
||||
<button
|
||||
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
|
||||
title="Copy code"
|
||||
>
|
||||
<div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
<div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<p>where <code>X</code> is some operation like <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code> and so on.</p></div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Let’s make this change to our code</p>
|
||||
<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
|
||||
|
||||
<div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// Sum.java</span>
|
||||
</span></span><span style="display:flex;"><span>
|
||||
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Sum</span> {
|
||||
</span></span><span style="display:flex;"><span>
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String<span style="color:#f92672">[]</span> args) {
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">int</span> sum <span style="color:#f92672">=</span> 0;
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">for</span> (String argument : args) {
|
||||
</span></span><span style="display:flex;"><span> StringBuilder number <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> StringBuilder(); <span style="color:#75715e">// we will form numbers here using StringBuilder</span>
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">char</span> c : argument.<span style="color:#a6e22e">toCharArray</span>()) {
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">if</span> (<span style="color:#f92672">!</span>Character.<span style="color:#a6e22e">isWhitespace</span>(c)) {
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#75715e">// if character is not whitespace</span>
|
||||
</span></span><span style="display:flex;"><span> number.<span style="color:#a6e22e">append</span>(c); <span style="color:#75715e">// concatenate new digit</span>
|
||||
</span></span><span style="display:flex;"><span> } <span style="color:#66d9ef">else</span> {
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#75715e">// if character is whitespace</span>
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">if</span> (<span style="color:#f92672">!</span>number.<span style="color:#a6e22e">isEmpty</span>()) {
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#75715e">// if number is not empty</span>
|
||||
</span></span><span style="display:flex;"><span> sum <span style="color:#f92672">+=</span> Integer.<span style="color:#a6e22e">parseInt</span>(number.<span style="color:#a6e22e">toString</span>()); <span style="color:#75715e">// add number to sum</span>
|
||||
</span></span><span style="display:flex;"><span> }
|
||||
</span></span><span style="display:flex;"><span> number <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> StringBuilder(); <span style="color:#75715e">// empty the number by creating new empty StringBuilder</span>
|
||||
</span></span><span style="display:flex;"><span> }
|
||||
</span></span><span style="display:flex;"><span> }
|
||||
</span></span><span style="display:flex;"><span>
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#75715e">// check for any remaining digits in number</span>
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">if</span> (<span style="color:#f92672">!</span>number.<span style="color:#a6e22e">isEmpty</span>()) {
|
||||
</span></span><span style="display:flex;"><span> <span style="color:#75715e">// if number is not empty</span>
|
||||
</span></span><span style="display:flex;"><span> sum <span style="color:#f92672">+=</span> Integer.<span style="color:#a6e22e">parseInt</span>(number.<span style="color:#a6e22e">toString</span>()); <span style="color:#75715e">// add number to sum</span>
|
||||
</span></span><span style="display:flex;"><span> }
|
||||
</span></span><span style="display:flex;"><span> }
|
||||
</span></span><span style="display:flex;"><span>
|
||||
</span></span><span style="display:flex;"><span> System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(sum);
|
||||
</span></span><span style="display:flex;"><span> }
|
||||
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0">
|
||||
<button
|
||||
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
|
||||
title="Copy code"
|
||||
>
|
||||
<div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
<div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<p>This is it! The code is ready.</p>
|
||||
<hr>
|
||||
<p>Below are some modifications to the code for deeper understanding of basic language constructions primarily data types. It is always advised to try completing them by yourself before reading the solution. Modifications progress in difficulty from easy to hard.</p>
|
||||
<h1>SumDouble</h1><ul>
|
||||
<li>Input data is 64-bytes floating point numbers.</li>
|
||||
<li>Class should be named <code>SumDouble</code>.</li>
|
||||
</ul>
|
||||
<h2>Solution<span class="hx:absolute hx:-mt-20" id="solution-1"></span>
|
||||
<a href="#solution-1" class="subheading-anchor" aria-label="Permalink for this section"></a></h2><p>This modification is fairly easy. All we need to do is to replace all integers with floating point numbers.</p>
|
||||
<hr>
|
||||
|
||||
</div>
|
||||
<div class="hx:mt-16"></div>
|
||||
|
||||
Reference in New Issue
Block a user