<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Edwin Torres</title>
    <description>The latest articles on DEV Community by Edwin Torres (@realedwintorres).</description>
    <link>https://dev.to/realedwintorres</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F62791%2F75e89abd-d847-4e05-ac6c-04c7b7761ab1.jpeg</url>
      <title>DEV Community: Edwin Torres</title>
      <link>https://dev.to/realedwintorres</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/realedwintorres"/>
    <language>en</language>
    <item>
      <title>Using a Switch for Days of the Week</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Sun, 04 Sep 2022 15:07:13 +0000</pubDate>
      <link>https://dev.to/realedwintorres/using-a-switch-for-days-of-the-week-209</link>
      <guid>https://dev.to/realedwintorres/using-a-switch-for-days-of-the-week-209</guid>
      <description>&lt;p&gt;The &lt;code&gt;switch&lt;/code&gt; statement is a useful selection statement when there are many values that require different logic.&lt;/p&gt;

&lt;p&gt;Here is a program that asks the user to enter a day number (1-7) and outputs the full name of that day of the week.&lt;/p&gt;

&lt;p&gt;First, import the Scanner class (for user input), declare the class name, and declare the main method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Days&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, declare a Scanner variable and create the object. This object will retrieve user input later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declare a variable to store the user input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;dayNum&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ask the user to enter a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter a day number (1-7): "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the Scanner object to retrieve the user input. Note that the program will wait here until the user types a value and presses Enter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="n"&gt;dayNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a &lt;code&gt;switch&lt;/code&gt; statement that switches on the dayNum variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dayNum&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the &lt;code&gt;switch&lt;/code&gt; statement, add cases for each day. For example, the value &lt;code&gt;1&lt;/code&gt; will output Monday:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Monday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are the other cases. Note that case &lt;code&gt;5&lt;/code&gt; also outputs TGIF:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tuesday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wednesday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thursday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Friday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"TGIF!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Saturday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sunday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last case is a default case. This case occurs when the dayNum value has a number outside the range 1-7:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;      &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid day number."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, add the closing curly brackets for the &lt;code&gt;switch&lt;/code&gt; statement, main method, and class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="o"&gt;}&lt;/span&gt; 
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the complete program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Days&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;dayNum&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter a day number (1-7): "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;dayNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dayNum&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Monday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tuesday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wednesday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thursday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Friday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"TGIF!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Saturday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sunday"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;       
      &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid day number."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;                      
    &lt;span class="o"&gt;}&lt;/span&gt; 
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading. 😃&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;&lt;code&gt;@realEdwinTorres&lt;/code&gt;&lt;/a&gt; for more programming tips and help.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Calculate the Volume of a Cube in Java</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Sun, 04 Sep 2022 14:44:17 +0000</pubDate>
      <link>https://dev.to/realedwintorres/calculate-the-volume-of-a-cube-in-java-4em9</link>
      <guid>https://dev.to/realedwintorres/calculate-the-volume-of-a-cube-in-java-4em9</guid>
      <description>&lt;p&gt;The volume of a cube with side &lt;code&gt;s&lt;/code&gt; is: &lt;code&gt;s x s x s&lt;/code&gt; or &lt;code&gt;s&lt;/code&gt; to the 3rd power.&lt;/p&gt;

&lt;p&gt;In Java, the &lt;code&gt;Math.pow(x,y)&lt;/code&gt; function raises &lt;code&gt;x&lt;/code&gt; to the power &lt;code&gt;y&lt;/code&gt;. Both variables are double values, and the function returns a double result.&lt;/p&gt;

&lt;p&gt;Let’s write a Java program to calculate the volume of a cube. The first three lines import the &lt;code&gt;Scanner&lt;/code&gt; class (needed for user input), declare the Java program class, and declare the main method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Volume&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declare two variables in the main method to store the side of the cube and the volume:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// side&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// volume&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a Scanner object that scans standard input. Store the object in the variable in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ask the user to input a value for the side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Please enter the side of the cube: "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the &lt;code&gt;nextDouble()&lt;/code&gt; method of the Scanner object to accept the side from the user and store it in the variable &lt;code&gt;s&lt;/code&gt;. Note that the program will wait on this line until the user presses the &lt;em&gt;Enter&lt;/em&gt; key to input a value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextDouble&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the &lt;code&gt;Math.pow()&lt;/code&gt; function to raise &lt;code&gt;s&lt;/code&gt; to the power &lt;code&gt;3&lt;/code&gt;. Store the result in the variable &lt;code&gt;v&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The volume of cube with side "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, close the main method and Java program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the complete program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Volume&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// side&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// volume&lt;/span&gt;
    &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Please enter the side of the cube: "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextDouble&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The volume of cube with side "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading. 😃&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;&lt;code&gt;@realEdwinTorres&lt;/code&gt;&lt;/a&gt; for more programming tips and help.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Java Input and Arithmetic</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Sun, 04 Sep 2022 13:21:22 +0000</pubDate>
      <link>https://dev.to/realedwintorres/java-input-and-arithmetic-2mkk</link>
      <guid>https://dev.to/realedwintorres/java-input-and-arithmetic-2mkk</guid>
      <description>&lt;p&gt;This tutorial is a simple Java program that asks the user for two numbers, adds them together, and outputs the result.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;a href="https://dev.to/realedwintorres/tutorial-visual-studio-code-and-java-icm"&gt;new Java project&lt;/a&gt; in VS Code.&lt;/li&gt;
&lt;li&gt;Create a new Java file named &lt;code&gt;AddExample.java&lt;/code&gt;, where you will type the code below.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Import the &lt;code&gt;Scanner&lt;/code&gt; class. This built-in Java class lets you accept input from the user.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Start the class definition. It must match the filename. The curly bracket indicates the start of the program.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AddExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Inside the class definition, declare a main method. This method is the first method that the Java interpreter calls.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Inside the main method definition, create a Scanner object named &lt;code&gt;s&lt;/code&gt;. This object will accept input from the user.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Output a line that asks the user to enter two numbers.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Please enter two numbers: "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use the Scanner object to read in two integers and store them in two integer variables &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use arithmetic to add &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; and store the result in an integer variable &lt;code&gt;z&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Output the result.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The sum is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Close the main method definition with the ending curly brace.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Close the class definition with the ending curly brace.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now run your program to see the result.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Congratulations!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You have written a Java program to accept two numbers from the user, add them, and output the result.&lt;/p&gt;

&lt;p&gt;Thanks for reading. 😃&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;&lt;code&gt;@realEdwinTorres&lt;/code&gt;&lt;/a&gt; for more programming tips and help.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Removing Sensitive Data in Git</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Tue, 19 Apr 2022 16:27:42 +0000</pubDate>
      <link>https://dev.to/realedwintorres/removing-sensitive-data-in-git-15ab</link>
      <guid>https://dev.to/realedwintorres/removing-sensitive-data-in-git-15ab</guid>
      <description>&lt;p&gt;This guide explain how to remove sensitive text from your Git repo. It requires &lt;a href="https://rtyley.github.io/bfg-repo-cleaner/"&gt;BFG Repo-Cleaner&lt;/a&gt;, which is &lt;a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository"&gt;endorsed&lt;/a&gt; by GitHub, and Java.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Close/merge all pull requests.&lt;/li&gt;
&lt;li&gt;Make sure all developers' local branches are clean.&lt;/li&gt;
&lt;li&gt;Clone your repo to create an emergency backup.&lt;/li&gt;
&lt;li&gt;Download the &lt;code&gt;bfg&lt;/code&gt; JAR file from &lt;a href="https://rtyley.github.io/bfg-repo-cleaner/"&gt;BFG Repo-Cleaner&lt;/a&gt;, for example &lt;code&gt;bfg-1.14.0.jar&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Go to a temporary working folder.&lt;/li&gt;
&lt;li&gt;Clone a &lt;em&gt;bare mirror&lt;/em&gt; of your repo, for example: &lt;code&gt;git clone --mirror git@gitlab.com:SomeUser/myrepo.git&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a text file named &lt;code&gt;sensitive.txt&lt;/code&gt; with regular expressions to replace. For example, this text file will replace all occurrences of &lt;code&gt;password123&lt;/code&gt; with &lt;code&gt;***REMOVED***&lt;/code&gt; and all occurrences of &lt;code&gt;abc123&lt;/code&gt; with &lt;code&gt;samplePassword&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  password123
  &lt;span class="nv"&gt;abc123&lt;/span&gt;&lt;span class="o"&gt;==&amp;gt;&lt;/span&gt;samplePassword
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Copy in the &lt;code&gt;bfg-1.14.0.jar&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Execute this command to replace the sensitive text: &lt;code&gt;java -jar bfg-1.14.0.jar --no-blob-protection --replace-text sensitive.txt myrepo.git&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Go into the mirror repo: &lt;code&gt;cd myrepo.git&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Execute &lt;code&gt;git reflog expire --expire=now --all &amp;amp;&amp;amp; git gc --prune=now --aggressive&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Push to your remote branch: &lt;code&gt;git push&lt;/code&gt; . Note: If this fails, you may need to unprotect the branch in the remote Git server.&lt;/li&gt;
&lt;li&gt;Ask all developers to &lt;strong&gt;re-clone the repo&lt;/strong&gt; to get the rewritten Git histories.&lt;/li&gt;
&lt;li&gt;Verify that the repo looks correct, then delete the local backup repo and temporary working folder.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now all sensitive data is gone.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;@realEdwinTorres&lt;/a&gt; for programming tips, software engineering content, and career advice. 😊&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The content in this blog post is publicly available at &lt;a href="https://git-scm.com/"&gt;Git&lt;/a&gt;, &lt;a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository"&gt;GitHub&lt;/a&gt;, &lt;a href="https://gitlab.com/"&gt;GitLab&lt;/a&gt;, and &lt;a href="https://rtyley.github.io/bfg-repo-cleaner/"&gt;BFG Repo-Cleaner&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>gitlab</category>
      <category>github</category>
      <category>git</category>
    </item>
    <item>
      <title>Using Merge Requests and Feature Branches in your Git Workflow</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Wed, 02 Feb 2022 02:28:53 +0000</pubDate>
      <link>https://dev.to/realedwintorres/using-merge-requests-and-feature-branches-in-your-git-workflow-26c5</link>
      <guid>https://dev.to/realedwintorres/using-merge-requests-and-feature-branches-in-your-git-workflow-26c5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Multiple developers contributing to the same &lt;code&gt;master&lt;/code&gt; branch of the same repo is asking for trouble. The dreaded merge &lt;code&gt;CONFLICT&lt;/code&gt; will surely occur. Merge conflicts happen because multiple developers modify the same file at the same time. Git is not able to choose the correct version.&lt;/p&gt;

&lt;p&gt;One way to minimize merge conflicts and improve the Git workflow is to use &lt;em&gt;feature branches&lt;/em&gt; and &lt;em&gt;merge requests&lt;/em&gt;. This tutorial demonstrates one way to do that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Here are the prerequisites for this tutorial:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All collaborators have &lt;a href="https://gitlab.com/"&gt;GitLab&lt;/a&gt; accounts with appropriate &lt;a href="https://dev.to/realedwintorres/hello-git-a-beginners-tutorial-on-git-and-gitlab-k7i"&gt;SSH access&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;This tutorial uses a GitLab repo called &lt;code&gt;acme-corp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The default branch of the repo is called &lt;code&gt;main&lt;/code&gt;, but yours might be &lt;code&gt;master&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;One collaborator has the &lt;em&gt;Maintainer&lt;/em&gt; role. This person sets up the repo.&lt;/li&gt;
&lt;li&gt;All other collaborators have the &lt;em&gt;Developer&lt;/em&gt; role.&lt;/li&gt;
&lt;li&gt;All developers and maintainers have a terminal application on the local computer with &lt;em&gt;Git&lt;/em&gt; installed.&lt;/li&gt;
&lt;li&gt;All developers and maintainers have a basic knowledge of &lt;code&gt;git&lt;/code&gt; and &lt;code&gt;bash&lt;/code&gt; commands.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1 - Protect the &lt;code&gt;main&lt;/code&gt; branch
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;Maintainer&lt;/em&gt; prevents developers from directly committing to the &lt;code&gt;main&lt;/code&gt; branch:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From a web browser, the &lt;em&gt;Maintainer&lt;/em&gt; logs into the GitLab account and goes to the &lt;code&gt;acme-corp&lt;/code&gt; repo.&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Settings &amp;gt; Repository&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Expand &lt;em&gt;Protected branches&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Scroll down and ensure that the &lt;code&gt;main&lt;/code&gt; branch is protected. Only &lt;em&gt;maintainers&lt;/em&gt; should be allowed to push and merge to this branch.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 2 - Developers create feature branches
&lt;/h2&gt;

&lt;p&gt;Developers cannot commit directly to the &lt;code&gt;main&lt;/code&gt; branch. Instead, they create and work in feature branches:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open a terminal on your local computer.&lt;/li&gt;
&lt;li&gt;Clone the &lt;code&gt;acme-corp&lt;/code&gt; repo.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Complete the following steps:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# go into the repo folder&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;acme-corp
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# make sure the repo is clean&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git status
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# get the latest changes from the remote GitLab repo&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git pull origin main
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# create a feature branch off of main and switch to it&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout &lt;span class="nt"&gt;-B&lt;/span&gt; my-new-feature
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# work in the new feature branch&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"my new file"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; newfile.txt  &lt;span class="c"&gt;# an example&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# see the status of your new feature branch&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git status
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# add and commit local changes to the feature branch&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"changes to my feature branch"&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; 
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# push the new branch to the remote GitLab repo&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git push origin my-new-feature
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From the browser, refresh your &lt;code&gt;acme-corp&lt;/code&gt; repo in GitLab.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find the new &lt;code&gt;my-new-feature&lt;/code&gt; branch in GitLab.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 3 - Developers create merge requests
&lt;/h2&gt;

&lt;p&gt;Developers create merge requests to merge feature branches back into the &lt;code&gt;main&lt;/code&gt; branch:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From a web browser, the developer logs into GitLab and goes to the &lt;code&gt;acme-corp&lt;/code&gt; repo.&lt;/li&gt;
&lt;li&gt;In the left sidebar, click &lt;em&gt;Merge requests&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Click the &lt;em&gt;Create merge request&lt;/em&gt; or &lt;em&gt;New merge request&lt;/em&gt; button.&lt;/li&gt;
&lt;li&gt;Add a &lt;em&gt;Title&lt;/em&gt; and &lt;em&gt;Description&lt;/em&gt; for the merge request.&lt;/li&gt;
&lt;li&gt;Click the &lt;em&gt;Create merge request&lt;/em&gt; button.&lt;/li&gt;
&lt;li&gt;The developer monitors the merge request while it is in &lt;em&gt;Open&lt;/em&gt; status.&lt;/li&gt;
&lt;li&gt;The developer checks the merge request for any warnings about existing merge conflicts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 4 - Maintainers review merge requests
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is the responsibility of maintainers to review, accept, and reject merge requests. Do this from the web browser, in the GitLab &lt;em&gt;Merge request&lt;/em&gt; area.&lt;/li&gt;
&lt;li&gt;Use the &lt;em&gt;Merge request&lt;/em&gt; area in GitLab to start discussions, view code differences, comment on code, etc.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Maintainers should reject merge requests that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contain errors&lt;/li&gt;
&lt;li&gt;Contain merge conflicts&lt;/li&gt;
&lt;li&gt;Do not follow coding style guidelines&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Maintainers should &lt;em&gt;approve&lt;/em&gt; and &lt;em&gt;merge&lt;/em&gt; feature branches that contain correct code that follows coding style guidelines.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 5 - Developers update or delete feature branches
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If the maintainers reject the merge request, update the local feature branch:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Review the merge requests comments in GitLab.&lt;/li&gt;
&lt;li&gt;Make the appropriate changes to the local &lt;code&gt;my-new-feature&lt;/code&gt; branch on your computer.&lt;/li&gt;
&lt;li&gt;Add and commit the changes locally.&lt;/li&gt;
&lt;li&gt;Push your local feature branch to the remote repo in GitLab.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the maintainers approve and merge, then delete the local feature branch from your computer: &lt;code&gt;git branch -D my-new-feature&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Congratulations! You have used a feature branch and merge request to update your &lt;code&gt;main&lt;/code&gt; branch. Use this technique to minimize merge conflicts, facilitate simultaneous work among team members, and protect the &lt;code&gt;main&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;@realEdwinTorres&lt;/a&gt; for programming tips, software engineering content, and career advice. 😊&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>git</category>
      <category>gitlab</category>
      <category>github</category>
    </item>
    <item>
      <title>Simple Java Scanner Tutorial</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Sat, 22 Jan 2022 16:33:57 +0000</pubDate>
      <link>https://dev.to/realedwintorres/simple-java-scanner-tutorial-e52</link>
      <guid>https://dev.to/realedwintorres/simple-java-scanner-tutorial-e52</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is a simple tutorial on using the &lt;em&gt;Scanner&lt;/em&gt; class in Java. The Scanner class lets you accept input from the console. The user types on the keyboard and passes that data to the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install Java and Visual Studio (VS) Code. Here is a tutorial: &lt;a href="https://dev.to/realedwintorres/tutorial-visual-studio-code-and-java-icm"&gt;Tutorial: Visual Studio Code and Java&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Start VS Code.&lt;/li&gt;
&lt;li&gt;Create a new Java project as specified in the tutorial.&lt;/li&gt;
&lt;li&gt;In the left sidebar, right-click the &lt;code&gt;src&lt;/code&gt; folder and select &lt;em&gt;New File&lt;/em&gt;. Create a file named &lt;code&gt;Example.java&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Continue to the tutorial below.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;In the left sidebar in VS Code, click the &lt;code&gt;Example.java&lt;/code&gt; file to show it in the main editor window. Enter this initial code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;


  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use the Scanner class, import it at the top of the program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// NEW&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;


  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create a Scanner object to read from standard input (i.e., the console):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// NEW&lt;/span&gt;

  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now prompt the user to enter some data from the keyboard:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your favorite number? "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// NEW&lt;/span&gt;

  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the Scanner object to accept an integer value from standard input and assign it to a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your favorite number? "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// NEW&lt;/span&gt;

  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;num&lt;/code&gt; variable holds the input data. Output the &lt;code&gt;num&lt;/code&gt; variable to verify:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your favorite number? "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Your favorite number is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// NEW&lt;/span&gt;

  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a sample run of the program from the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;What is your favorite number? 44
Your favorite number is: 44
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Additional Exercise
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Modify the program to ask the user for an &lt;em&gt;age&lt;/em&gt; and &lt;em&gt;day of the month&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Output the &lt;em&gt;age&lt;/em&gt; and &lt;em&gt;day of the month&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;&lt;code&gt;@realEdwinTorres&lt;/code&gt;&lt;/a&gt; for more programming tips. 😀&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>java</category>
      <category>vscode</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Simple Test-Driven Development (TDD) With Mocha and Node.js</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Sun, 16 Jan 2022 21:31:39 +0000</pubDate>
      <link>https://dev.to/realedwintorres/simple-test-driven-development-tdd-with-mocha-and-nodejs-5ek2</link>
      <guid>https://dev.to/realedwintorres/simple-test-driven-development-tdd-with-mocha-and-nodejs-5ek2</guid>
      <description>&lt;p&gt;Here is a quick tutorial on Test-Driven Development (TDD) with the &lt;a href="https://mochajs.org/" rel="noopener noreferrer"&gt;Mocha&lt;/a&gt; test tool and Node.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Test-Driven Development&lt;/strong&gt;, or &lt;em&gt;TDD&lt;/em&gt;, is a &lt;a href="https://www.agilealliance.org/glossary/tdd/" rel="noopener noreferrer"&gt;programming style&lt;/a&gt; that includes three tightly integrated activities: &lt;em&gt;coding&lt;/em&gt;, &lt;em&gt;unit testing&lt;/em&gt;, and &lt;em&gt;refactoring&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The TDD approach is a cycle:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq5og41hq6bu7tk0rm7p9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq5og41hq6bu7tk0rm7p9.png" alt="Test-Driven Development" width="500" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;RED/FAIL&lt;/strong&gt; - Write one unit test for a single feature of the program, execute the test, and ensure that it fails. It should fail because the program feature has not been developed yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GREEN/PASS&lt;/strong&gt; - Write "just enough" code to make the test pass. The code may simply hard-code the correct output for a given input value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;REFACTOR&lt;/strong&gt; - Refactor the code with real logic until it passes the unit test. Implement the feature and remove the hard-coded result. Refactoring does &lt;em&gt;not&lt;/em&gt; change the behavior of the program; the given input must still produce the expected output. Refactoring may also enhance a program or improve efficiency.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Repeat&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  TDD With Mocha and Node.js
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Download and install &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; locally on your computer.&lt;/li&gt;
&lt;li&gt;A terminal application: &lt;em&gt;Terminal&lt;/em&gt; (macOS) or &lt;a href="https://gitforwindows.org/" rel="noopener noreferrer"&gt;&lt;em&gt;Git Bash&lt;/em&gt;&lt;/a&gt; (Windows).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tutorial
&lt;/h3&gt;

&lt;p&gt;Here is a new program feature to implement. The &lt;em&gt;Restaurant Tip Calculator&lt;/em&gt; calculates tips for a restaurant bill.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;
&lt;strong&gt;Feature:&lt;/strong&gt; Restaurant Tip Calculator&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Scenario:&lt;/strong&gt; Calculate a 20% restaurant tip.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;When&lt;/strong&gt; the restaurant bill is &lt;code&gt;$100&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Then&lt;/strong&gt; the tip amount is &lt;code&gt;$20&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here are steps that illustrate the TDD process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Open a terminal and go into a working folder:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;myfolder
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Initialize the folder to use the &lt;em&gt;Mocha&lt;/em&gt; test tool:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; git init &lt;span class="nb"&gt;.&lt;/span&gt;  &lt;span class="c"&gt;# for Git versioning later&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; mocha  &lt;span class="c"&gt;# install mocha locally&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; mocha  &lt;span class="c"&gt;# try mocha. It will fail; no tests yet...&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; Error: No &lt;span class="nb"&gt;test &lt;/span&gt;files found: &lt;span class="s2"&gt;"test"&lt;/span&gt;         
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From inside the &lt;code&gt;myfolder&lt;/code&gt; folder, create a directory named &lt;em&gt;test&lt;/em&gt;: &lt;code&gt;mkdir test&lt;/code&gt; .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create an empty unit test program inside the &lt;code&gt;test&lt;/code&gt; folder named &lt;code&gt;TipTest.js&lt;/code&gt;: &lt;code&gt;touch test/TipTest.js&lt;/code&gt; .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;myfolder&lt;/code&gt; folder create an empty program for the program we are testing: &lt;code&gt;touch Tip.js&lt;/code&gt; .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the Mocha test. We now have the minimal amount of code to have a failed test:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; mocha

  0 passing &lt;span class="o"&gt;(&lt;/span&gt;2ms&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the first unit test to the &lt;code&gt;TipTest.js&lt;/code&gt; program. This unit test checks the tip amount for a $100 amount. The correct/expected tip amount is $20:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;assert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;assert&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;Tip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../Tip.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;Tip&lt;/span&gt;  &lt;span class="c1"&gt;// program to test&lt;/span&gt;

&lt;span class="c1"&gt;// new unit test&lt;/span&gt;
&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Check tip amount&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sending in 100&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should return 20&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nc"&gt;CalculateTip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt; 
  &lt;span class="p"&gt;});&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;RED&lt;/strong&gt;: Add minimal code to the &lt;code&gt;Tip.js&lt;/code&gt; program that initially fails the test:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CalculateTip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// hard-code a result that will FAIL&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Tip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the Mocha test. It will execute the test and fail, because the actual result of the program is &lt;code&gt;0&lt;/code&gt;, but the expected result is &lt;code&gt;20&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; mocha

  Check tip amount
    sending &lt;span class="k"&gt;in &lt;/span&gt;100
      1&lt;span class="o"&gt;)&lt;/span&gt; should &lt;span class="k"&gt;return &lt;/span&gt;20

  0 passing &lt;span class="o"&gt;(&lt;/span&gt;6ms&lt;span class="o"&gt;)&lt;/span&gt;
  1 failing

  1&lt;span class="o"&gt;)&lt;/span&gt; Check tip amount
       sending &lt;span class="k"&gt;in &lt;/span&gt;100
         should &lt;span class="k"&gt;return &lt;/span&gt;20:

      AssertionError &lt;span class="o"&gt;[&lt;/span&gt;ERR_ASSERTION]: 20 &lt;span class="o"&gt;==&lt;/span&gt; 0
      + expected - actual

      &lt;span class="nt"&gt;-20&lt;/span&gt;
      +0

      at Context.&amp;lt;anonymous&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;test&lt;/span&gt;/TipTest.js:10:14&lt;span class="o"&gt;)&lt;/span&gt;
      at processImmediate &lt;span class="o"&gt;(&lt;/span&gt;internal/timers.js:456:21&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;GREEN&lt;/strong&gt;: Modify the &lt;code&gt;Tip.js&lt;/code&gt; program to hard-code it to succeed:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CalculateTip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// hard-code a result that will SUCCEED&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Tip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the Mocha test again. This time it succeeds, because the actual and expected results are both &lt;code&gt;20&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; mocha

  Check tip amount
    sending &lt;span class="k"&gt;in &lt;/span&gt;100
      ✔ should &lt;span class="k"&gt;return &lt;/span&gt;20

  1 passing &lt;span class="o"&gt;(&lt;/span&gt;4ms&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;REFACTOR&lt;/strong&gt;: Modify the code by implementing the &lt;em&gt;correct&lt;/em&gt; logic for the &lt;code&gt;Tip.js&lt;/code&gt; program:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CalculateTip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// NEW&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tipAmount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.20&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;tipAmount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Tip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Tip&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the Mocha test and see it succeed with real, correct logic:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; mocha

  Check tip amount
    sending &lt;span class="k"&gt;in &lt;/span&gt;100
      ✔ should &lt;span class="k"&gt;return &lt;/span&gt;20

  1 passing &lt;span class="o"&gt;(&lt;/span&gt;6ms&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You have successfully used TDD to develop this first feature of the &lt;em&gt;Tip Calculator&lt;/em&gt; program. Add more unit tests to test the program further or add new features.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Your turn
&lt;/h2&gt;

&lt;p&gt;Create a new unit test for the &lt;em&gt;Tip Calculator&lt;/em&gt; app that implements this feature:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;
&lt;strong&gt;Feature:&lt;/strong&gt; Restaurant Tip Calculator&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Scenario:&lt;/strong&gt; Calculate a 25% restaurant tip.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;When&lt;/strong&gt; the restaurant bill is at least &lt;code&gt;$200&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Then&lt;/strong&gt; the tip amount is 25% of the restaurant bill&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This completes the TDD tutorial with Mocha. Note how TDD occurs in iterations. Establish a test that you expect to fail. Modify the test to make it succeed with the expected value. Finally, implement real logic in your program that passes the test. Repeat the process for each new feature of the program.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres" rel="noopener noreferrer"&gt;@realEdwinTorres&lt;/a&gt; for programming tips, software engineering content, and career advice. 😊&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Simple Git Branching Tutorial</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Fri, 14 Jan 2022 16:22:45 +0000</pubDate>
      <link>https://dev.to/realedwintorres/simple-git-branching-tutorial-3n0f</link>
      <guid>https://dev.to/realedwintorres/simple-git-branching-tutorial-3n0f</guid>
      <description>&lt;h2&gt;
  
  
  Git Branching Tutorial
&lt;/h2&gt;

&lt;p&gt;Here is a quick tutorial on using branches in Git.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assumptions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You already have a GitLab/GitHub repo. This tutorial uses a repo named &lt;code&gt;my-repo&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Your repo has one branch named &lt;code&gt;master&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You already cloned the repo to your local computer.&lt;/li&gt;
&lt;li&gt;Your repo is clean: &lt;code&gt;git status&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tutorial
&lt;/h3&gt;

&lt;p&gt;Here is a terminal session that demonstrates Git branching. You will create a new branch named &lt;code&gt;my-branch&lt;/code&gt; from your &lt;code&gt;master&lt;/code&gt; branch. This makes a copy of the &lt;code&gt;master&lt;/code&gt; branch. Then, you will make changes to the new branch and push it to your remote GitLab/GitHub server.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;From your local computer, open a terminal and go to your local repo and make sure you are on the &lt;code&gt;master&lt;/code&gt; branch and the working tree is &lt;em&gt;clean&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;my-repo
&lt;span class="nv"&gt;$ &lt;/span&gt; git status  &lt;span class="c"&gt;# On branch master? working tree clean?&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a new local branch from &lt;code&gt;master&lt;/code&gt; and name it &lt;code&gt;my-branch&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout &lt;span class="nt"&gt;-B&lt;/span&gt; my-branch  &lt;span class="c"&gt;# create new branch&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git status  &lt;span class="c"&gt;# you are on the new branch now&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="nb"&gt;ls&lt;/span&gt;  &lt;span class="c"&gt;# this branch has the same files as master&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Switch back and forth between branches. Note that you stay in the same folder:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout master
&lt;span class="nv"&gt;$ &lt;/span&gt; git status &lt;span class="c"&gt;# now on master branch&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout my-branch
&lt;span class="nv"&gt;$ &lt;/span&gt; git status &lt;span class="c"&gt;# now on my-branch&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# when you switch branches, you stay in the same folder. Git changes the branch files for you:&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="nb"&gt;pwd&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# shortcut to switch to the previous branch&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout -
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add some files to your new branch:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout my-branch
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"print('hello')"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; hello.py  &lt;span class="c"&gt;# create a new file&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git status  &lt;span class="c"&gt;# changes pending in new branch&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# commit files to your new branch locally&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"changes to my new branch"&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You created and updated your new branch locally, but it is not yet in the remote GitLab/GitHub server. So, push your local branch to the remote:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout my-branch
&lt;span class="nv"&gt;$ &lt;/span&gt; git push origin my-branch  &lt;span class="c"&gt;# push to the remote&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# From your browser, look at your new branch in GitHub/GitLab&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# The master branch is the default, so manually select the my-branch branch to see it&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now your new branch exists locally on your computer and remotely in GitLab/GitHub. You can keep your local my-branch branch if you need to make future changes to that branch.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To merge your new branch back into &lt;code&gt;master&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout my-branch
&lt;span class="nv"&gt;$ &lt;/span&gt; git status  &lt;span class="c"&gt;# make sure it is clean&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout master
&lt;span class="nv"&gt;$ &lt;/span&gt; git status  &lt;span class="c"&gt;# make sure it is clean&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; &lt;span class="c"&gt;# do the merge&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout master  &lt;span class="c"&gt;# check out branch we are merging into&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git merge my-branch  &lt;span class="c"&gt;# merge new branch into master&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git push origin master  &lt;span class="c"&gt;# push the updated master branch to the remote&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now that you merged &lt;code&gt;my-branch&lt;/code&gt; into &lt;code&gt;master&lt;/code&gt;, you may not need &lt;code&gt;my-branch&lt;/code&gt; anymore. If you want to delete &lt;code&gt;my-branch&lt;/code&gt; from your local computer:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout master  &lt;span class="c"&gt;# you cannot be on the branch you are deleting&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt; git branch &lt;span class="nt"&gt;-D&lt;/span&gt; my-branch &lt;span class="c"&gt;# delete my-branch locally&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To also delete &lt;code&gt;my-branch&lt;/code&gt; from the remote GitLab/GitHub server:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt; git checkout master
&lt;span class="nv"&gt;$ &lt;/span&gt; git push origin &lt;span class="nt"&gt;--delete&lt;/span&gt; my-branch  &lt;span class="c"&gt;# deletes my-branch from the remote&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This ends this Git branching tutorial. Use branches to perform parallel work. Delete local branches if you no longer need them. Delete remote branches if you no longer need them, or you've merged them into another long-term branch.&lt;/p&gt;

&lt;p&gt;For more information and original content for this blog, see the Git &lt;a href="https://git-scm.com/"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;@realEdwinTorres&lt;/a&gt; for programming tips, software engineering content, and career advice. 😊&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hello Git: A Beginner's Tutorial on Git and GitLab</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Sat, 01 Jan 2022 18:28:06 +0000</pubDate>
      <link>https://dev.to/realedwintorres/hello-git-a-beginners-tutorial-on-git-and-gitlab-k7i</link>
      <guid>https://dev.to/realedwintorres/hello-git-a-beginners-tutorial-on-git-and-gitlab-k7i</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This tutorial is for beginners who are new to  &lt;a href="https://git-scm.com/"&gt;Git&lt;/a&gt;  and  &lt;a href="https://gitlab.com/"&gt;GitLab&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Git&lt;/em&gt; is a  "&lt;a href="https://git-scm.com/"&gt;free and open source distributed version control system&lt;/a&gt;  designed to handle everything from small to very large projects with speed and efficiency." Use Git locally on your computer to manage software versions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;GitLab&lt;/em&gt; is a  &lt;a href="https://about.gitlab.com/what-is-gitlab/"&gt;DevOps platform&lt;/a&gt;. It can host your local Git repos for remote collaboration, and a whole lot more. Some other Git servers are &lt;a href="https://github.com/"&gt;GitHub&lt;/a&gt;  and  &lt;a href="https://bitbucket.org/product"&gt;Bitbucket&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Register with GitLab and Create a Repo
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Register for a GitLab account &lt;a href="https://gitlab.com/users/sign_up"&gt;here&lt;/a&gt; or on your private GitLab server.&lt;/li&gt;
&lt;li&gt;From a web browser, log into your GitLab account.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a new repo:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new project:  &lt;a href="https://gitlab.com/projects/new"&gt;https://gitlab.com/projects/new&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Create blank project&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Project name: &lt;strong&gt;hello-world&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Click the &lt;em&gt;Create project&lt;/em&gt; button.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your remote GitLab repo is created.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Install Git Locally
&lt;/h2&gt;

&lt;p&gt;Make sure that you have Git locally on your computer.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if you already have Git. From your local computer, open a macOS Terminal or Git Bash terminal and enter: &lt;code&gt;git --version&lt;/code&gt;. If you have Git, then continue to Establish SSH Access.&lt;/li&gt;
&lt;li&gt;Install Git:

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Windows&lt;/em&gt;: &lt;a href="https://git-scm.com/download/win"&gt;https://git-scm.com/download/win&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;macOS&lt;/em&gt;: From the Terminal app, enter &lt;code&gt;git --version&lt;/code&gt; and follow instructions to install &lt;em&gt;Xcode Command Line Tools&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Introduce yourself to Git, using your real name and email:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nv"&gt;$ &lt;/span&gt; git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.name &lt;span class="s2"&gt;"Your Name"&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;
  &lt;span class="nv"&gt;$ &lt;/span&gt; git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"youremail@mail.com"&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the remainder of this tutorial, "terminal" refers to either the &lt;em&gt;macOS Terminal&lt;/em&gt; app or &lt;em&gt;Windows Git Bash&lt;/em&gt; terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Establish SSH Access
&lt;/h2&gt;

&lt;p&gt;To access your remote GitLab repos from your local computer, you must first establish SSH access. This requires uploading your public SSH key from your computer to GitLab.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Check if you already have an SSH key:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From the &lt;em&gt;macOS&lt;/em&gt; terminal, enter: &lt;code&gt;cat ~/.ssh/id_rsa.pub | pbcopy&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;From the &lt;em&gt;Windows Git Bash&lt;/em&gt; terminal, enter: &lt;code&gt;cat ~/.ssh/id_rsa.pub | clip&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If the command succeeds, then your public SSH key is in the clipboard. Otherwise go to the next step.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create your SSH key &lt;em&gt;if it does not exist&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Warning&lt;/strong&gt;: Do &lt;strong&gt;NOT&lt;/strong&gt; perform this step if the &lt;code&gt;~/.ssh/id_rsa.pub&lt;/code&gt; file already exists. This step will create/overwrite these files in your home directory: &lt;code&gt;~/.ssh/id_rsa ~/.ssh/id_rsa.pub&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;From the terminal, enter the following commands, replacing &lt;code&gt;YOUREMAIL@MAIL.COM&lt;/code&gt; with your email address: &lt;code&gt;cd  ;  ssh-keygen -o -t rsa -b 4096 -C "YOUREMAIL@MAIL.COM"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Press the &lt;code&gt;Enter&lt;/code&gt; key to accept all defaults. Do not specify a password, unless you want to enter a password each time you interact with the remote Git server.&lt;/li&gt;
&lt;li&gt;Go back to &lt;em&gt;Step 1&lt;/em&gt; above to copy your public SSH key to the clipboard.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add your SSH key to your GitLab account:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From a web browser, log into your GitLab account.&lt;/li&gt;
&lt;li&gt;Go to SSH Keys: &lt;a href="https://gitlab.com/-/profile/keys"&gt;https://gitlab.com/-/profile/keys&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;In the &lt;em&gt;Key&lt;/em&gt; textbox, paste your public SSH key from the clipboard.&lt;/li&gt;
&lt;li&gt;Click the &lt;em&gt;Add key&lt;/em&gt; button.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You now have SSH access from your local computer to your remote GitLab account.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Use Git Locally and GitLab Remotely
&lt;/h2&gt;

&lt;p&gt;Now try to use Git locally and interact with your remote repos in GitLab.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;From a web browser, log into your GitLab account.&lt;/li&gt;
&lt;li&gt;Go to your &lt;strong&gt;hello-world&lt;/strong&gt; repo, click the &lt;em&gt;Clone&lt;/em&gt; button, and copy the &lt;em&gt;Clone with SSH&lt;/em&gt; link to the clipboard.&lt;/li&gt;
&lt;li&gt;From your local computer, open a terminal and go (&lt;code&gt;cd&lt;/code&gt;) to a directory to work from.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Enter the following command to &lt;em&gt;clone&lt;/em&gt; your GitLab repo locally on your computer. Paste the SSH link at the end. For example, if your GitLab username is &lt;code&gt;JohnSmith&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone git@gitlab.com:JohnSmith/hello-world.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is now a &lt;code&gt;hello-world&lt;/code&gt; folder locally on your computer. From the terminal, go into the folder: &lt;code&gt;cd hello-world&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter &lt;code&gt;git status&lt;/code&gt; to see the current state of the repo. There are no changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a few local files:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"red blue green"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; colors.txt
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"apple cherry watermelon"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; fruits.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter &lt;code&gt;git status&lt;/code&gt; to see the current state of the repo. Now there are local changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the changes to the Git staging area:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add colors.txt fruits.txt  &lt;span class="c"&gt;# or simply: git add .&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Commit the changes to the local repo:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"my first Git commit"&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now the two new files are part of your repo. Push your local changes to the &lt;code&gt;master&lt;/code&gt; branch of the remote GitLab repo:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push origin master
&lt;span class="c"&gt;#  if you see an error, you may need to first: &lt;/span&gt;
&lt;span class="c"&gt;#  git pull origin master&lt;/span&gt;
&lt;span class="c"&gt;#  to pull/merge the latest from the remote GitLab repo&lt;/span&gt;

git status  &lt;span class="c"&gt;# repo is clean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From a web browser, go to your GitLab &lt;code&gt;hello-world&lt;/code&gt; repo. Refresh the browser and verify that your local changes made it to the remote server.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Congratulations!&lt;/strong&gt; You have successfully created a GitLab account and repo, established a local Git repo, and used Git both locally and remotely. Well done! Now learn more advanced Git commands and try them both locally and remotely.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;&lt;code&gt;@realEdwinTorres&lt;/code&gt;&lt;/a&gt; for programming tips, software engineering content, and career advice. 😊&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>10 Things I'd Tell My Younger Self About Programming</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Mon, 06 Dec 2021 01:05:53 +0000</pubDate>
      <link>https://dev.to/realedwintorres/10-things-id-tell-my-younger-self-about-programming-12kk</link>
      <guid>https://dev.to/realedwintorres/10-things-id-tell-my-younger-self-about-programming-12kk</guid>
      <description>&lt;p&gt;Here are &lt;em&gt;10 Things I'd Tell My Younger Self About Programming&lt;/em&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Don't be scared. It gets easier.&lt;/strong&gt; Early on in my career, basic programming concepts like loops and arrays were difficult. With time and practice, programming concepts become straightforward.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Work on your communication skills.&lt;/strong&gt; Communication skills are more important than you think. The way you present yourself to a colleague or customer has a big impact on your career. Write good documentation and emails. Practice presentation skills. Even if you are the most skilled programmer, nobody will give you credit if you don't communicate well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Earn your master's degree in computer science as soon as possible.&lt;/strong&gt; I earned my master's degree early on in my career. I wasn't motivated to do it, and I didn't quite see the benefit. But my master's degree has given me many opportunities such as career advancement, teaching, and my doctorate degree. Earning my master's degree is one of the best career moves I have ever made.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Take detailed notes.&lt;/strong&gt; You are going to learn a lot in this field. The information will be overwhelming at times. Keep detailed notes and refer to them often. This will accelerate your learning and demonstrate your ability to grasp concepts quickly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask questions, but ask them properly.&lt;/strong&gt; You'll have lots of programming questions throughout your career. Before you ask a question, do your research. Understand the problem. Explain how you are approaching it. Describe your current difficulties. This will help someone help you. And never ask the same question twice! That's what your detailed notes are for.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find good mentors.&lt;/strong&gt; Learning from other people is extremely effective. Teaching others benefits you too. Find a good mentor and have frequent technical discussions with that person. Be a mentor and be mentored by your peers. This personal exchange of information will accelerate your career.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plan your career.&lt;/strong&gt; There is a saying: &lt;em&gt;dress for the job you want, not the job you have.&lt;/em&gt; Think about where you want to be in your career in five years. Start planning for it by finding roles and responsibilities that give you experience for that career move. For example, if your next goal is to lead a development task, then spend some time with a current task leader and learn the role. Try to be a backup for that task leader to get real-world experience.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoid obsolescence.&lt;/strong&gt; Follow the technical industry trends and keep your skills current. Do not simply code in one language for your entire career; if that language becomes obsolete, then so will you. Look for training opportunities in future technologies. Read technical articles, blogs, and social media to keep a pulse on industry trends.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep your resume updated.&lt;/strong&gt; You never know when the right job opportunity might come. One day, you may be forced to find a new job. Keeping your resume updated will make it easier to find that next job.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Start a side gig.&lt;/strong&gt; Whether it’s a programming blog, book, website, or other personal project, a side gig is an excellent way to hone your skills. The side gig might even become your main gig one day.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;&lt;code&gt;@realEdwinTorres&lt;/code&gt;&lt;/a&gt; for more programming tips. 😀&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>A Simple 2D Point Class in Java</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Sun, 28 Nov 2021 21:28:15 +0000</pubDate>
      <link>https://dev.to/realedwintorres/a-simple-2d-point-class-in-java-19p3</link>
      <guid>https://dev.to/realedwintorres/a-simple-2d-point-class-in-java-19p3</guid>
      <description>&lt;p&gt;Creating a simple 2D point class in Java is fairly simple. A &lt;a href="https://en.wikipedia.org/wiki/Cartesian_coordinate_system"&gt;Cartesian coordinate system&lt;/a&gt; uses points in two dimensions along an &lt;em&gt;x-axis&lt;/em&gt; and &lt;em&gt;y-axis&lt;/em&gt;. Each point has a pair of &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; coordinates along these two axes.&lt;/p&gt;

&lt;p&gt;We define a &lt;code&gt;SimplePoint&lt;/code&gt; class that has two private properties &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimplePoint&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access each property, create two &lt;em&gt;getter&lt;/em&gt; methods in the class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getY&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could also provide &lt;em&gt;setter&lt;/em&gt; methods if desired.&lt;/p&gt;

&lt;p&gt;A constructor method makes it convenient to create a &lt;code&gt;SimplePoint&lt;/code&gt; object at a specific &lt;em&gt;x,y&lt;/em&gt; location:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;SimplePoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;y1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, to create two &lt;code&gt;SimplePoint&lt;/code&gt; objects at &lt;em&gt;x,y&lt;/em&gt; positions &lt;code&gt;2,1&lt;/code&gt; and &lt;code&gt;8,2&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;SimplePoint&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimplePoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;SimplePoint&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimplePoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, here is a &lt;code&gt;distance()&lt;/code&gt; function to calculate the distance between two &lt;code&gt;SimplePoint&lt;/code&gt; objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;distance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SimplePoint&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sqrt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;()-&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getY&lt;/span&gt;&lt;span class="o"&gt;()-&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To calculate and output the distance between &lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p1&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;distance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 6.082762530298219&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the complete source code for the &lt;code&gt;SimplePoint&lt;/code&gt; class, including the sample &lt;code&gt;main()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimplePoint&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;SimplePoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;y1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getY&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;distance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SimplePoint&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sqrt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;()-&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getY&lt;/span&gt;&lt;span class="o"&gt;()-&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;SimplePoint&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimplePoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;SimplePoint&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimplePoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;distance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 6.082762530298219&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Java makes it easy to represent 2D points. The &lt;code&gt;SimplePoint&lt;/code&gt; class is one way to do that. Create &lt;code&gt;SimplePoint&lt;/code&gt; objects at specified locations, then use the &lt;code&gt;distance()&lt;/code&gt; function to calculate the distances between them.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;&lt;code&gt;@realEdwinTorres&lt;/code&gt;&lt;/a&gt; for more programming tips. 😀&lt;/p&gt;

</description>
      <category>java</category>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Accessing Elements in 2D Arrays in Java</title>
      <dc:creator>Edwin Torres</dc:creator>
      <pubDate>Sun, 28 Nov 2021 20:45:05 +0000</pubDate>
      <link>https://dev.to/realedwintorres/accessing-elements-in-2d-arrays-in-java-1o7i</link>
      <guid>https://dev.to/realedwintorres/accessing-elements-in-2d-arrays-in-java-1o7i</guid>
      <description>&lt;p&gt;The two-dimensional (2D) array is a useful data structure. Like one-dimensional arrays, 2D arrays work well with &lt;code&gt;for&lt;/code&gt; loops.&lt;/p&gt;

&lt;p&gt;Here is a 2D array in Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;},&lt;/span&gt;
  &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;},&lt;/span&gt;
  &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;int[][]&lt;/code&gt; declaration means that &lt;code&gt;arr&lt;/code&gt; is a 2D array of &lt;code&gt;int&lt;/code&gt; values. Since there are two pairs of &lt;code&gt;[]&lt;/code&gt;, the array has two dimensions.&lt;/p&gt;

&lt;p&gt;Look at this 2D array. It is actually an &lt;em&gt;array of arrays&lt;/em&gt;. The array &lt;code&gt;arr&lt;/code&gt; has a length of &lt;code&gt;3&lt;/code&gt;. It has three elements, or &lt;em&gt;rows&lt;/em&gt;.  Each row is an array of length &lt;code&gt;5&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first array is &lt;code&gt;arr[0]&lt;/code&gt;. The second and third arrays are &lt;code&gt;arr[1]&lt;/code&gt; and &lt;code&gt;arr[2]&lt;/code&gt; respectively. A &lt;code&gt;for&lt;/code&gt; loop can access each of these arrays using an index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
  &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;0, 1, 2, 3, 4]
&lt;span class="o"&gt;[&lt;/span&gt;5, 6, 7, 8, 9]
&lt;span class="o"&gt;[&lt;/span&gt;10, 11, 12, 13, 14]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop accesses each row. Since the row is also an array, an inner &lt;code&gt;for&lt;/code&gt; loop can access each of its elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;","&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;": "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;0,0: 0
0,1: 1
0,2: 2
0,3: 3
0,4: 4
1,0: 5
1,1: 6
1,2: 7
1,3: 8
1,4: 9
2,0: 10
2,1: 11
2,2: 12
2,3: 13
2,4: 14
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the output, the first number is the row index. The second number is the column index. The third number is the element at that position of the 2D array.&lt;/p&gt;

&lt;p&gt;Note how two indexes are required when accessing a single element of the 2D array. For example, &lt;code&gt;arr[1][3]&lt;/code&gt; contains the &lt;code&gt;int&lt;/code&gt; value &lt;code&gt;8&lt;/code&gt;. The &lt;code&gt;for&lt;/code&gt; loop accesses each row in order. Once it has a row, then it uses an inner &lt;code&gt;for&lt;/code&gt; loop to access its &lt;code&gt;int&lt;/code&gt; values in order.&lt;/p&gt;

&lt;p&gt;Here is a complete example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;},&lt;/span&gt;
      &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;},&lt;/span&gt;
      &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
      &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;","&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;": "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using 2D arrays, use two indexes. The first index is for the row. The second index is for the column. In other words, each element of a 2D array is an array. The first index accesses an array element. Then use a second index to access the elements in that array. To facilitate this, use a &lt;code&gt;for&lt;/code&gt; loop with an inner &lt;code&gt;for&lt;/code&gt; loop to provide the row and column indexes.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;Follow me on Twitter &lt;a href="https://twitter.com/realEdwinTorres"&gt;&lt;code&gt;@realEdwinTorres&lt;/code&gt;&lt;/a&gt; for more programming tips. 😀&lt;/p&gt;

</description>
      <category>java</category>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>100daysofcode</category>
    </item>
  </channel>
</rss>
