This commit is contained in:
Binary file not shown.
@@ -5,8 +5,7 @@ public class ParseIntExample {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String s = "123";
|
String s = "123";
|
||||||
int sum = 321;
|
int sum = 321;
|
||||||
//! sum = sum + s;
|
String result = s + sum;
|
||||||
sum = sum + Integer.parseInt(s);
|
System.out.println(result);
|
||||||
System.out.println(sum);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
content/courses/prog-intro/homeworks/sum/Sum.class
Normal file
BIN
content/courses/prog-intro/homeworks/sum/Sum.class
Normal file
Binary file not shown.
@@ -1,17 +1,32 @@
|
|||||||
// Sum.java
|
// Sum.java
|
||||||
|
|
||||||
public class Sum {
|
public class Sum {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("number of arguments: " + args.length);
|
|
||||||
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (String argument : args) {
|
for (String argument : args) {
|
||||||
sum = sum + argument;
|
StringBuilder number = new StringBuilder(); // we will form numbers here using StringBuilder
|
||||||
System.out.println(argument);
|
for (char c : argument.toCharArray()) {
|
||||||
System.out.println(sum);
|
if (!Character.isWhitespace(c)) {
|
||||||
|
// if character is not whitespace
|
||||||
|
number.append(c); // concatenate new digit
|
||||||
|
} else {
|
||||||
|
// if character is whitespace
|
||||||
|
if (!number.isEmpty()) {
|
||||||
|
// if number is not empty
|
||||||
|
sum = sum + Integer.parseInt(number.toString()); // add number to sum
|
||||||
|
}
|
||||||
|
number = new StringBuilder(); // empty the number by creating new empty StringBuilder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println(sum);
|
System.out.println(sum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -235,11 +235,226 @@ public class ParseIntExample {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String s = "123";
|
String s = "123";
|
||||||
int sum = 321;
|
int sum = 321;
|
||||||
//! sum = sum + s;
|
//! int result = s + sum;
|
||||||
sum = sum + Integer.parseInt(s);
|
|
||||||
System.out.println(sum);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The commented out line (`//!`) would have given us an exception.
|
The commented out line (`//!`) would have given us an exception.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ javac ParseIntExample.java
|
||||||
|
ParseIntExample.java:8: error: incompatible types: String cannot be converted to int
|
||||||
|
int result = s + sum;
|
||||||
|
^
|
||||||
|
1 error
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> Please note that if we change `result` data type to `String`, the code will work just fine.
|
||||||
|
> ```java
|
||||||
|
> // ParseIntExample.java
|
||||||
|
> public class ParseIntExample {
|
||||||
|
>
|
||||||
|
> public static void main(String[] args) {
|
||||||
|
> String s = "123";
|
||||||
|
> int sum = 321;
|
||||||
|
> String result = s + sum;
|
||||||
|
> System.out.println(result);
|
||||||
|
> }
|
||||||
|
> }
|
||||||
|
> ```
|
||||||
|
> ```bash
|
||||||
|
> $ javac ParseIntExample.java && java ParseIntExample
|
||||||
|
> 123321
|
||||||
|
> ```
|
||||||
|
> In this case `sum` will be ***automatically casted*** to `String` and two strings will just concatenate.
|
||||||
|
|
||||||
|
So we figured out that we need to cast `argument` to integer type. Let's change our code and test it.
|
||||||
|
|
||||||
|
```java
|
||||||
|
// Sum.java
|
||||||
|
|
||||||
|
public class Sum {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("number of arguments: " + args.length);
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
for (String argument : args) {
|
||||||
|
sum = sum + Ineteger.parseInt(argument);
|
||||||
|
System.out.println(argument);
|
||||||
|
System.out.println(sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
// Sum.java
|
||||||
|
|
||||||
|
public class Sum {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("number of arguments: " + args.length);
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
for (String argument : args) {
|
||||||
|
sum = sum + Integer.parseInt(argument);
|
||||||
|
System.out.println(
|
||||||
|
"current argument: " + argument + ", current sum: " + sum
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("final sum: " + sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```bash
|
||||||
|
$ java Sum 1 2 3
|
||||||
|
number of arguments: 3
|
||||||
|
current argument: 1, current sum: 1
|
||||||
|
current argument: 2, current sum: 3
|
||||||
|
current argument: 3, current sum: 6
|
||||||
|
final sum: 6
|
||||||
|
```
|
||||||
|
```bash
|
||||||
|
$ java Sum 1 2 -3
|
||||||
|
number of arguments: 3
|
||||||
|
current argument: 1, current sum: 1
|
||||||
|
current argument: 2, current sum: 3
|
||||||
|
current argument: -3, current sum: 0
|
||||||
|
final sum: 0
|
||||||
|
```
|
||||||
|
```bash
|
||||||
|
$ java Sum "1 2 3"
|
||||||
|
number of arguments: 1
|
||||||
|
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 2 3"
|
||||||
|
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
|
||||||
|
at java.base/java.lang.Integer.parseInt(Integer.java:662)
|
||||||
|
at java.base/java.lang.Integer.parseInt(Integer.java:778)
|
||||||
|
at Sum.main(Sum.java:9)
|
||||||
|
```
|
||||||
|
|
||||||
|
As we can see because we have only 1 argument (`"1 2 3"`) we try to parse it as an integer but `"1 2 3"` contains empty spaces, so it can't be parsed as an integer.
|
||||||
|
|
||||||
|
We need to manually extract numeric values from the `argument` string.
|
||||||
|
|
||||||
|
In order to do that we will iterate over each symbol of the `argument` string and extract numeric values.
|
||||||
|
|
||||||
|
We will have another variable `number` of `String` type in which we will be storing extracted numeric values.
|
||||||
|
|
||||||
|
For every character or symbol in the string, we will check it to be an empty space or a digit. If it is a digit, we will add/concatenate with `number`, and if it is an empty space, we will add the numeric value of `number` to `sum` and then assign empty string to `number` because we reached the end of the current number and are ready to move to the next one.
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> It is important to add numeric value of `number` (if it's not empty) to `sum` after we iterated over the entire string because if it ends like this `"1 2 3"`, we don't nessesarily have an empty space at the end so `"3"` (in this case) will be stored in `number` but NOT added to `sum`.
|
||||||
|
|
||||||
|
Let's implement those ideas into solution. We will get rid of all the `println` messages so it's not drawing our attention.
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> Also I want to point out that we can't just iterate over `String`. First we need to turn it into an ***array of characters*** using [`String.toCharArray()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toCharArray--) method.
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> We will be checking for an empty space using [`Character.isWhitespace(char ch)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isWhitespace-char-) which outputs `true` or `false` accordingly.
|
||||||
|
|
||||||
|
```java
|
||||||
|
// Sum.java
|
||||||
|
|
||||||
|
public class Sum {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int sum = 0;
|
||||||
|
for (String argument : args) {
|
||||||
|
String number = ""; // we will form numbers here
|
||||||
|
for (char c : argument.toCharArray()) {
|
||||||
|
if (!Character.isWhitespace(c)) {
|
||||||
|
// if character is not whitespace
|
||||||
|
number = number + c; // concatenate new digit
|
||||||
|
} else {
|
||||||
|
// if character is whitespace
|
||||||
|
if (!number.isEmpty()) {
|
||||||
|
// if number is not empty
|
||||||
|
sum = sum + Integer.parseInt(number); // add number to sum
|
||||||
|
}
|
||||||
|
number = ""; // empty the number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for any remaining digits in number
|
||||||
|
if (!number.isEmpty()) {
|
||||||
|
// if number is not empty
|
||||||
|
sum = sum + Integer.parseInt(number); // add number to sum
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ java Sum 1 2 3
|
||||||
|
6
|
||||||
|
```
|
||||||
|
```bash
|
||||||
|
$ java Sum 1 2 -3
|
||||||
|
0
|
||||||
|
```
|
||||||
|
```bash
|
||||||
|
$ java Sum "1 2 3"
|
||||||
|
6
|
||||||
|
```
|
||||||
|
```bash
|
||||||
|
$ java Sum "1 2" " 3"
|
||||||
|
6
|
||||||
|
```
|
||||||
|
```bash
|
||||||
|
$ java Sum " "
|
||||||
|
0
|
||||||
|
```
|
||||||
|
|
||||||
|
This code works and passes all the tests!
|
||||||
|
|
||||||
|
But can we improve it? One efficiency improvement we can make is to get rid of concatenating strings. You can read why it's bad [here](https://stackoverflow.com/questions/18561424/using-for-strings-in-a-loop-is-it-bad-practice) or [here](https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://www.reddit.com/r/javahelp/comments/19e9jic/question_about_dont_concat_string_in_a_loop/&ved=2ahUKEwiCpva0y-GTAxUeB9sEHbvzHj4QFnoECCAQAQ&usg=AOvVaw3gk39Xk7Jz9_PmKVIO81nK).
|
||||||
|
|
||||||
|
We can use [StringBuilder](https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html) because it can ***build*** strings more efficiently.
|
||||||
|
|
||||||
|
So here's the updated code
|
||||||
|
|
||||||
|
```java
|
||||||
|
// Sum.java
|
||||||
|
|
||||||
|
public class Sum {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int sum = 0;
|
||||||
|
for (String argument : args) {
|
||||||
|
StringBuilder number = new StringBuilder(); // we will form numbers here using StringBuilder
|
||||||
|
for (char c : argument.toCharArray()) {
|
||||||
|
if (!Character.isWhitespace(c)) {
|
||||||
|
// if character is not whitespace
|
||||||
|
number.append(c); // concatenate new digit
|
||||||
|
} else {
|
||||||
|
// if character is whitespace
|
||||||
|
if (!number.isEmpty()) {
|
||||||
|
// if number is not empty
|
||||||
|
sum = sum + Integer.parseInt(number.toString()); // add number to sum
|
||||||
|
}
|
||||||
|
number = new StringBuilder(); // empty the number by creating new empty StringBuilder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> Note, that we can't just do `Integer.parseInt()`
|
||||||
|
|||||||
Binary file not shown.
@@ -5,8 +5,7 @@ public class ParseIntExample {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String s = "123";
|
String s = "123";
|
||||||
int sum = 321;
|
int sum = 321;
|
||||||
//! sum = sum + s;
|
String result = s + sum;
|
||||||
sum = sum + Integer.parseInt(s);
|
System.out.println(result);
|
||||||
System.out.println(sum);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
public/courses/prog-intro/homeworks/sum/Sum.class
Normal file
BIN
public/courses/prog-intro/homeworks/sum/Sum.class
Normal file
Binary file not shown.
@@ -1,17 +1,32 @@
|
|||||||
// Sum.java
|
// Sum.java
|
||||||
|
|
||||||
public class Sum {
|
public class Sum {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("number of arguments: " + args.length);
|
|
||||||
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (String argument : args) {
|
for (String argument : args) {
|
||||||
sum = sum + argument;
|
StringBuilder number = new StringBuilder(); // we will form numbers here using StringBuilder
|
||||||
System.out.println(argument);
|
for (char c : argument.toCharArray()) {
|
||||||
System.out.println(sum);
|
if (!Character.isWhitespace(c)) {
|
||||||
|
// if character is not whitespace
|
||||||
|
number.append(c); // concatenate new digit
|
||||||
|
} else {
|
||||||
|
// if character is whitespace
|
||||||
|
if (!number.isEmpty()) {
|
||||||
|
// if number is not empty
|
||||||
|
sum = sum + Integer.parseInt(number.toString()); // add number to sum
|
||||||
|
}
|
||||||
|
number = new StringBuilder(); // empty the number by creating new empty StringBuilder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println(sum);
|
System.out.println(sum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ java Sum 1 2 -3 Expected output: 0." /><link rel="canonical" href="http://localh
|
|||||||
Examples:
|
Examples:
|
||||||
java Sum 1 2 3 Expected output: 6.
|
java Sum 1 2 3 Expected output: 6.
|
||||||
java Sum 1 2 -3 Expected output: 0.">
|
java Sum 1 2 -3 Expected output: 0.">
|
||||||
<meta itemprop="wordCount" content="919">
|
<meta itemprop="wordCount" content="1840">
|
||||||
<meta name="twitter:card" content="summary">
|
<meta name="twitter:card" content="summary">
|
||||||
<meta name="twitter:title" content="Sum">
|
<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.
|
<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.
|
||||||
@@ -775,9 +775,7 @@ java Sum 1 2 -3 Expected output: 0.">
|
|||||||
</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">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> String s <span style="color:#f92672">=</span> <span style="color:#e6db74">"123"</span>;
|
</span></span><span style="display:flex;"><span> String s <span style="color:#f92672">=</span> <span style="color:#e6db74">"123"</span>;
|
||||||
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">int</span> sum <span style="color:#f92672">=</span> 321;
|
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">int</span> sum <span style="color:#f92672">=</span> 321;
|
||||||
</span></span><span style="display:flex;"><span> <span style="color:#75715e">//! sum = sum + s;</span>
|
</span></span><span style="display:flex;"><span> <span style="color:#75715e">//! int result = s + sum;</span>
|
||||||
</span></span><span style="display:flex;"><span> sum <span style="color:#f92672">=</span> sum <span style="color:#f92672">+</span> Integer.<span style="color:#a6e22e">parseInt</span>(s);
|
|
||||||
</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>}</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">
|
</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
|
<button
|
||||||
@@ -790,6 +788,361 @@ java Sum 1 2 -3 Expected output: 0.">
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>The commented out line (<code>//!</code>) would have given us an exception.</p>
|
<p>The commented out line (<code>//!</code>) would have given us an exception.</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-bash" data-lang="bash"><span style="display:flex;"><span>$ javac ParseIntExample.java
|
||||||
|
</span></span><span style="display:flex;"><span>ParseIntExample.java:8: error: incompatible types: String cannot be converted to int
|
||||||
|
</span></span><span style="display:flex;"><span> int result <span style="color:#f92672">=</span> s + sum;
|
||||||
|
</span></span><span style="display:flex;"><span> ^
|
||||||
|
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> error</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="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-purple-200 hx:bg-purple-100 hx:text-purple-900 hx:dark:border-purple-200/30 hx:dark:bg-purple-900/30 hx:dark:text-purple-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>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>Please note that if we change <code>result</code> data type to <code>String</code>, the code will work just fine.</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">// ParseIntExample.java</span>
|
||||||
|
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ParseIntExample</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> String s <span style="color:#f92672">=</span> <span style="color:#e6db74">"123"</span>;
|
||||||
|
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">int</span> sum <span style="color:#f92672">=</span> 321;
|
||||||
|
</span></span><span style="display:flex;"><span> String result <span style="color:#f92672">=</span> s <span style="color:#f92672">+</span> sum;
|
||||||
|
</span></span><span style="display:flex;"><span> System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(result);
|
||||||
|
</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-bash" data-lang="bash"><span style="display:flex;"><span>$ javac ParseIntExample.java <span style="color:#f92672">&&</span> java ParseIntExample
|
||||||
|
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">123321</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>In this case <code>sum</code> will be <em><strong>automatically casted</strong></em> to <code>String</code> and two strings will just concatenate.</p></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<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> {
|
||||||
|
</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> Integer.<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>(
|
||||||
|
</span></span><span style="display:flex;"><span> <span style="color:#e6db74">"current argument: "</span> <span style="color:#f92672">+</span> argument <span style="color:#f92672">+</span> <span style="color:#e6db74">", current sum: "</span> <span style="color:#f92672">+</span> sum
|
||||||
|
</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>(<span style="color:#e6db74">"final sum: "</span> <span style="color:#f92672">+</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-bash" data-lang="bash"><span style="display:flex;"><span>$ java Sum <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span>
|
||||||
|
</span></span><span style="display:flex;"><span>number of arguments: <span style="color:#ae81ff">3</span>
|
||||||
|
</span></span><span style="display:flex;"><span>current argument: 1, current sum: <span style="color:#ae81ff">1</span>
|
||||||
|
</span></span><span style="display:flex;"><span>current argument: 2, current sum: <span style="color:#ae81ff">3</span>
|
||||||
|
</span></span><span style="display:flex;"><span>current argument: 3, current sum: <span style="color:#ae81ff">6</span>
|
||||||
|
</span></span><span style="display:flex;"><span>final sum: <span style="color:#ae81ff">6</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-bash" data-lang="bash"><span style="display:flex;"><span>$ java Sum <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> -3
|
||||||
|
</span></span><span style="display:flex;"><span>number of arguments: <span style="color:#ae81ff">3</span>
|
||||||
|
</span></span><span style="display:flex;"><span>current argument: 1, current sum: <span style="color:#ae81ff">1</span>
|
||||||
|
</span></span><span style="display:flex;"><span>current argument: 2, current sum: <span style="color:#ae81ff">3</span>
|
||||||
|
</span></span><span style="display:flex;"><span>current argument: -3, current sum: <span style="color:#ae81ff">0</span>
|
||||||
|
</span></span><span style="display:flex;"><span>final sum: <span style="color:#ae81ff">0</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-bash" data-lang="bash"><span style="display:flex;"><span>$ java Sum <span style="color:#e6db74">"1 2 3"</span>
|
||||||
|
</span></span><span style="display:flex;"><span>number of arguments: <span style="color:#ae81ff">1</span>
|
||||||
|
</span></span><span style="display:flex;"><span>Exception in thread <span style="color:#e6db74">"main"</span> java.lang.NumberFormatException: For input string: <span style="color:#e6db74">"1 2 3"</span>
|
||||||
|
</span></span><span style="display:flex;"><span> at java.base/java.lang.NumberFormatException.forInputString<span style="color:#f92672">(</span>NumberFormatException.java:67<span style="color:#f92672">)</span>
|
||||||
|
</span></span><span style="display:flex;"><span> at java.base/java.lang.Integer.parseInt<span style="color:#f92672">(</span>Integer.java:662<span style="color:#f92672">)</span>
|
||||||
|
</span></span><span style="display:flex;"><span> at java.base/java.lang.Integer.parseInt<span style="color:#f92672">(</span>Integer.java:778<span style="color:#f92672">)</span>
|
||||||
|
</span></span><span style="display:flex;"><span> at Sum.main<span style="color:#f92672">(</span>Sum.java:9<span style="color:#f92672">)</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>As we can see because we have only 1 argument (<code>"1 2 3"</code>) we try to parse it as an integer but <code>"1 2 3"</code> contains empty spaces, so it can’t be parsed as an integer.</p>
|
||||||
|
<p>We need to manually extract numeric values from the <code>argument</code> string.</p>
|
||||||
|
<p>In order to do that we will iterate over each symbol of the <code>argument</code> string and extract numeric values.</p>
|
||||||
|
<p>We will have another variable <code>number</code> of <code>String</code> type in which we will be storing extracted numeric values.</p>
|
||||||
|
<p>For every character or symbol in the string, we will check it to be an empty space or a digit. If it is a digit, we will add/concatenate with <code>number</code>, and if it is an empty space, we will add the numeric value of <code>number</code> to <code>sum</code> and then assign empty string to <code>number</code> because we reached the end of the current number and are ready to move to the next one.</p>
|
||||||
|
<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-purple-200 hx:bg-purple-100 hx:text-purple-900 hx:dark:border-purple-200/30 hx:dark:bg-purple-900/30 hx:dark:text-purple-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>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>It is important to add numeric value of <code>number</code> (if it’s not empty) to <code>sum</code> after we iterated over the entire string because if it ends like this <code>"1 2 3"</code>, we don’t nessesarily have an empty space at the end so <code>"3"</code> (in this case) will be stored in <code>number</code> but NOT added to <code>sum</code>.</p></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p>Let’s implement those ideas into solution. We will get rid of all the <code>println</code> messages so it’s not drawing our attention.</p>
|
||||||
|
<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 I want to point out that we can’t just iterate over <code>String</code>. First we need to turn it into an <em><strong>array of characters</strong></em> using <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toCharArray--"target="_blank" rel="noopener"><code>String.toCharArray()</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>We will be checking for an empty space using <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 outputs <code>true</code> or <code>false</code> accordingly.</p></div>
|
||||||
|
</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> {
|
||||||
|
</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> String number <span style="color:#f92672">=</span> <span style="color:#e6db74">""</span>; <span style="color:#75715e">// we will form numbers here</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:#f92672">=</span> number <span style="color:#f92672">+</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> sum <span style="color:#f92672">+</span> Integer.<span style="color:#a6e22e">parseInt</span>(number); <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:#e6db74">""</span>; <span style="color:#75715e">// empty the number</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> sum <span style="color:#f92672">+</span> Integer.<span style="color:#a6e22e">parseInt</span>(number); <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>
|
||||||
|
<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-bash" data-lang="bash"><span style="display:flex;"><span>$ java Sum <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span>
|
||||||
|
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">6</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-bash" data-lang="bash"><span style="display:flex;"><span>$ java Sum <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> -3
|
||||||
|
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">0</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-bash" data-lang="bash"><span style="display:flex;"><span>$ java Sum <span style="color:#e6db74">"1 2 3"</span>
|
||||||
|
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">6</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-bash" data-lang="bash"><span style="display:flex;"><span>$ java Sum <span style="color:#e6db74">"1 2"</span> <span style="color:#e6db74">" 3"</span>
|
||||||
|
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">6</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-bash" data-lang="bash"><span style="display:flex;"><span>$ java Sum <span style="color:#e6db74">" "</span>
|
||||||
|
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">0</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 code works and passes all the tests!</p>
|
||||||
|
<p>But can we improve it? One efficiency improvement we can make is to get rid of concatenating strings. You can read why it’s bad <a href="https://stackoverflow.com/questions/18561424/using-for-strings-in-a-loop-is-it-bad-practice"target="_blank" rel="noopener">here</a> or <a href="https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://www.reddit.com/r/javahelp/comments/19e9jic/question_about_dont_concat_string_in_a_loop/&ved=2ahUKEwiCpva0y-GTAxUeB9sEHbvzHj4QFnoECCAQAQ&usg=AOvVaw3gk39Xk7Jz9_PmKVIO81nK"target="_blank" rel="noopener">here</a>.</p>
|
||||||
|
<p>We can use <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html"target="_blank" rel="noopener">StringBuilder</a> because it can <em><strong>build</strong></em> strings more efficiently.</p>
|
||||||
|
<p>So here’s the updated 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> 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> 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>
|
||||||
|
<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-purple-200 hx:bg-purple-100 hx:text-purple-900 hx:dark:border-purple-200/30 hx:dark:bg-purple-900/30 hx:dark:text-purple-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>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>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div class="hx:mt-16"></div>
|
<div class="hx:mt-16"></div>
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user