Lesson Weekend

In the last few lessons we've created programs that work exclusively with Strings. Let's mix things up and write a program focused on Integers instead.

In this lesson we'll practice writing a basic Java program that interacts with Integers. Similar to the JavaScript we wrote in Intro to Programming, Integers provided to Java programs by users are actually returned as Strings. We'll explore how to transform these values from Strings back intoIntegers.

Application Setup

Let's write an application that will take a number from a user, double it, then provide the larger number back to them. To do this, we'll create a numbers project directory. Within it, we'll also create a file called DoubleIt.java. This file will contain our application's required class and main() method:

src/main/java/DoubleIt.java
public class DoubleIt {
  public static void main(String[] args) {

  }
}

Gathering User Input

Next, let's prompt the user to provide a number, then gather the value they provide and assign it to a variable called yourNumber:

src/main/java/DoubleIt.java
public class DoubleIt {
   public static void main(String[] args) {
       System.out.println("Give me a number, and I will double it for you!");
   }
}

Remember, in order to gather input from the user through the command line we need to import the relevant Java packages, and use the code we implemented in the previous lesson.

Next, we'll double the number the user provided, store it in a new variable called yourDoubledNumber, and print this new number back to the terminal for the user to see:

src/main/java/DoubleIt.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DoubleIt {
   public static void main(String[] args) {
       System.out.println("Give me a number, and I will double it for you!");
       try{
           BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
           Integer yourNumber = bufferedReader.readLine();
           Integer yourDoubledNumber = yourNumber * 2;

           System.out.println("I doubled your number for you: " + yourDoubledNumber);

       }
       catch(IOException e)
       {
           e.printStackTrace();
       }
   }
}

Incompatible Types Error

But wait! We have the following squiggly on the screen, which displays this error when we run our app:

Information:7/20/17, 12:19 PM - Compilation completed with 1 error and 0 warnings in 577ms
/Users/epicodus_student/Desktop/favoritecolor/src/main/java/DoubleIt.java
Error:(13, 57) java: incompatible types: java.lang.String cannot be converted to java.lang.Integer

When we declare a variable's type, such as the Integer in the line Integer yourNumber or String in the line String favoriteColor, Java will throw an error if we attempt to set the variable to anything but the type we specifically declared.

The error above reads String cannot be converted to Integer, because we declared an Integer variable when we stated Integer yourNumber = ..., yet we attempted to set this variable to a String! Whenever you see a ___ cannot be converted to ___ error, it means a value in your application does not match the data type declared for it.

But why is this a String anyway? Similar to gathering form input from user's using JavaScript, like we did in Intro to Programming, gathering input from the user with bufferedReader.readLine() will always result in a string. Therefore, if we want to assign this data to a variable of an Integer data type, or use any methods upon it specifically meant for integers (like the arithmetic we'll need to perform in order to double this number) we must transform it from a string, back into an integer!

Transforming Strings into Integers

Thankfully, there are handy methods to transform a string into an integer. Let's instead save the user's input in a String variable called stringNumber, then use a method called parseInt() to transform this number into an Integer:

src/main/java/DoubleIt.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DoubleIt {
   public static void main(String[] args) {
       System.out.println("Give me a number, and I will double it for you!");
       try{
           BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
           String stringNumber = bufferedReader.readLine();
           Integer yourNumber = Integer.parseInt(stringNumber);
           Integer yourDoubledNumber = yourNumber * 2;

           System.out.println("I doubled your number for you: " + yourDoubledNumber);

       }
       catch(IOException e)
       {
           e.printStackTrace();
       }
   }
}

Integer.parseInt(String stringNumber) takes a String and transforms it into an Integer. Once this value is an integer, we can successfully multiply it. Likewise, Integer.toString(Integer yourDoubledNumber) takes an Integer and makes it into a String, so we can concatenate it with the String "I doubled your number for you: ".

We can compile and run our new program to see this data type transformation in action.

The same way we can use Integer.parseInt, we can also use Boolean.parseBoolean and Float.parseFloat to do similar operations. They work the same way.