IB Computer Science Unit 1
IB Computer Science Unit 2
IB Computer Science Unit 3
IB Computer Science Unit 4
IB Computer Science Unit 5
IB Computer Science Unit 6

IB Computer Science Java Variables and Input

IB Computer Science Learning Goals

In this IB Computer Science lesson you will be learning about:

  • How data is stored in a computer memory and how to access that data in a program
  • How to get keyboard input from the user of your program from the text console
  • How to output data back to the console

Data Types in Java

Computer programs work by manipulating data through user input or mathematical calculations.

There are different types of data that we can get from the keyboard or use in our programs. Java gives different types of data the following names

int – Positive and Negative integers (whole numbers)

double – Positive and Negative real (decimal numbers)

boolean – A true or false value

char – A single Character

String – a set of characters joined together (word or sentence)

During a programs operation data must be stored and manipulated in the computer’s memory. The computer needs to know how much space to allocate in its memory every time you read data from the keyboard or do a calculation. Each one of the data types above will take a different amount of memory so you must think and plan ahead about what types of data you will be using in your program.

Java doesn’t let you as the programmer access memory locations directly, it forces you to define a variable to represent that data location. A variable in computer science is exactly like a variable in math. It is a letter or word that is used to represent a piece of data.

You should follow the naming conventions for variables outlined below

  • Start with a lower case letter
  • Each word after the first will be capitalized
  • They cannot contain spaces or start with a number
  • Have meaningful names that represent the data they are storing
  • They are case sensitive

Declaring Variables

In order to use a variable, you must declare the type and the name of a variable before you can use it in your program

In Java, start with the data type followed by the variable name, and ending with a semicolon.

IB-Computer-Science-Variables-In-Java (1)

It is also possible to declare multiple variables of the same type on a single line by starting with the data type, then listing the variables separated by commas, and ending with a semicolon.

IB-Computer-Science-Variables-In-Java (2)

Assigning Values To Variables

To assign a value to a variable: start with the variable name, then an equal sign, followed by the value you wish to assign, end with a semicolon.

Variables must be assigned the same of type of value that they were declared to hold. This is because when you declare a variable it creates a certain amount of space in the computers memory for that piece of data.

  • You cannot assign a decimal number to a variable that was declared as an integer
  • You cannot assign a String to a variable declared as a character

There is one exception:

  • You can assign an integer value to a variable declared as a double
IB-Computer-Science-Variables-In-Java (3)

Notice that Strings are assigned values inside a set of “ “ double quotation marks and characters are assigned values inside a set of ‘ ‘ quotation marks

It is also possible to assign a value to a variable at the same time it is declared

IB-Computer-Science-Variables-In-Java (4)

Sometimes we have to wait to assign values to variables because they will be entered by the user when the program runs.

Keyboard Input of Data Using System Streams

Input is actually a very complicated aspect for computer programming languages. There are many different ways to do accomplish these tasks in Java. The standard way to do this is using methods contained inside the Java IO Library package.

In Java, a source of input data is called an input stream and the output data is called an output stream. Think of these streams like this:

 

You have already seen one system stream – System.out in the last lesson

The system stream that deals with input from the keyboard is called System.in

The data a keyboard sends to a program is character data, even when the characters include the digits ‘0’ through ‘9’.

In order to do this with Java you need to create a link in software to the System.in data stream. The class with the methods to do this is called InputStreamReader

A typical line to create this link would look as follows:

InputStreamReader inStream = new InputStreamReader (System.in ) ;

This line constructs an InputStreamReader object and gives it the name inStream

Now we need a way to actually read in the data that is actually typed into the keyboard

To make this task as simple as possible we will use a class called BufferedReader to read in the data from the input stream we just linked to.

A typical line would look something like this

BufferedReader input = new BufferedReader(inStream);

This line creates a BufferedReader object named input that links to the input stream object inStream created before.

Think of this process as an assembly line:

  • System.in gets characters from the keyboard.
  • The InputStreamReader reads the characters from System.in and hands them to the BufferedReader.
  • The BufferedReader object hands the data to your program whenever you want it to.

Putting that all together in code will like this.

IB-Computer-Science-Variables-In-Java (5)

You will notice I had to add a couple of lines of code

  • import java.io.*;
  • throws IOException

The import statement loads the InputStreamReader and BufferedReader class into the program


The throws IOException tells java to ignore its error checking on user input when the program is running.

  • Exceptions are thrown when something unexpected happens in your program during operation. If you catch that exception the program will gracefully stop without a total crash. Right now our programs are so simple we can just let the program crash so we just ignore all Input/Output exceptions that java might generate

Using Buffered Reader to Get Data Into Your Program

A typical line to read in data would be

dataIn = input.readLine();

Here dataIn would be a String variable that was created and input is the BufferedReader object that you created earlier. ALL data read from the keyboard using this method is treated as a STRING

Example:

Let’s write a program that asks the user to enter their first name and then enter their last name and we will store that data in memory


Programming Strategy:

  • Create the Stream and BufferedReader objects
  • Declare Variables for the input, first name, and last name
  • Prompt for the first name, read the input into a variable, store the input in the correct variable
  • Prompt for last name, read the input into a variable, store the input in the correct variable

 

IB-Computer-Science-Variables-In-Java (6)

What if you wanted to read in a number from the keyboard instead

  • You need to convert that String data to a different data type using the parseInt or parseDouble methods.

Let’s write a program that ask the user to enter an integer value and a decimal value

Programming Strategy:

  • Create the Stream and BufferedReader objects
    Declare Variables for the input, whole number, decimal number
  • Prompt for the whole number, read the input into a variable, convert and store the input in the correct variable
  • Prompt for the decimal number, read the input into a variable, convert and store the input in the correct variable
IB-Computer-Science-Variables-In-Java (7)

This seems like a lot of code to write for such a simple task, there are a few shortcuts we can take to reduce the number of lines of code

IB-Computer-Science-Variables-In-Java (1)

Java works a lot like BEDMAS in Math Class, it performs internal brackets first and works outward

Output Data Values to the Text Console

The following example illustrates how to print the value of variables to the screen

IB-Computer-Science-Variables-In-Java (10)

The following example shows how to output both text and variables to the screen at the same time

IB-Computer-Science-Variables-In-Java (11)

The plus symbol in the print statements is performing a String Concatenation. Basically its combining numerical data and String data together to form a longer String.

Looking to Learn More about Computer Science and Coding?

Check out our programing in python courses that focus on high school level coding.  

  • Grade 11 Computer Science ICS3U
  • Grade 12 Computer Science ICS4U

These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions

  • You can find more practice questions on using variables with user input at the bottom of the page

Return To International Baccalaureate Computer Science Main Page