Tag Archives: java

Java Scanner Tutorial (Area of A Square)

Have you ever logged on to a computer, posted a status update or typed in a number on your calculator? Most of you reading this have, in our modern world of technology user input plays a huge role. User input is what allows us as programs to interact with our user. Today we will learn how to write a program that will prompt a user for information. Although quite simple this program will introduce you to the idea of user input. Lets Get Started!!!

Step 1) Go ahead and open your favorite Java IDE(Integrated Development Environment)

Step 2) Create a new Java Project (Im using NetBeans by the way!)
Screen Shot 2013-02-17 at 12.04.14 AMScreen Shot 2013-02-17 at 12.04.05 AM

Step 3) Give your project a name

Screen Shot 2013-02-17 at 12.04.29 AM

Step 4) Open the Java file (Area.java)

Screen Shot 2013-02-17 at 12.04.50 AMScreen Shot 2013-02-17 at 12.04.57 AM

Step 5) Lets start coding

Screen Shot 2013-02-17 at 12.07.45 AM

The java.util.scanner is what allows the user to send information from his/her keyboard to the program. In a sense it links the physical and virtual world. To use this feature we must import it. We do this by typing “import” and then specify the class that we want to use. Java has hundreds of these classes that do a range of things from graphics to sound, all together its called the library.

Step 6) Start in the Main

Screen Shot 2013-02-17 at 12.09.40 AM

This program allows a user to input the side lengths of a square. Then the program will diagram the square and compute its area. Once the user inputs the side length we need a place to store that number, so we create a variable. A variable is simply a place to store information. So if you were a computer, then you may have a variable called first name. And inside the first name variable the word “Carter”. This is in a sense how variables work, this also allows us to call upon the variables value in the future. So here we set up a variable for the side length and area.

Step 6) Prompt the user

Screen Shot 2013-02-17 at 12.32.40 AM

This bit of code above creates the backbone of the scanner set up. The first line is how we instantiate the scanner. In plain terms we are telling the computer that we are making a new scanner called “keyboard”. Next we print out on the screen a prompt, by using System.out.print instead of System.out.println we can make a prompt. The last line of code sets the value of the next int that is typed on the keyboard to the variable side.

Step 7) Draw the Square

Screen Shot 2013-02-17 at 12.15.05 AM

The for loop above is what draws the square. The logic within for loops can be tricky sometimes so please look at the for loop tutorial if you need some assistance.

Step 8) Give them the answer!!

Screen Shot 2013-02-17 at 12.16.29 AM

After the for loop we compute the area of the square (s^2 = area). We then take this newlwy stored information and print it on the screen with our “System.out.println” method.

Step 9) Run The code!!!!!

If you had any trouble you can find the full source code here (http://pastebin.com/8YKysJev)