Friday, April 17, 2015

Using Scanners in Java

     Scanners can be very useful when you are working with a pure Java program and you want to be able to interact with your code. Here we will go through the process of creating a Scanner, as well as using it in your own programs. 

     First, let's take a look at this example. You can create your own Java class with this code in it. For the moment, ignore the "package Examples;" at the top, you will not need to use it. 
     The main structure of this program is the same as any other. Start it off by declaring your class name and main constructor. To start things off, add in a variable "name" and comment it so that you can easily reference it in your code later.
     Next, declare a scanner. Start off by declaring your class type as "Scanner" and then declare the name of your scanner. This name can be anything you want. I chose SCstring because the scanner will contain a string, but that does not matter. Now define the scanner as "= new Scanner(System.in);". This tells the computer that you are making a new scanner that will accept input from the user.
     Now that your scanner has been declared, you may use it anywhere in the constructor. This is where the program will actually start. As you can see, I printed out a statement asking the user to enter in their name. Next, I will define the String variable "name" to be equal to the next line that is entered into the scanner. What this means is that the scanner will wait for the user to type a response, and then define "name" as that response. This allows you, as the programmer, to accept and use input from a user.
     The final line of code simply prints out what the user typed in. This will be confirmation that the scanner worked correctly, and your program stored the correct string.

     So what are the limits to a scanner? Well, each scanner that you define can only accept one type of input. That is why I declared my scanner as "SCstring" because that way I know that that scanner is designed to accept strings from the user. I do this simply for my own benefit, as it does not change what the user sees in the program.
     If you want to accept a different kind of input, like say an integer, you will need to create a new scanner. In my case, if I wanted to accept input for an integer, I would make a new scanner called "SCint" and then in the program I would type in "name = SCint.nextInt();" in place of what we have in the example. It is important that you have the correct syntax in place for different types of variables, so keep track of what you are trying to accept from the user.

     So what are the implications of this? As a programmer, you now have the ability to accept input from the user to dictate what your program does. The user can enter a username and a password to get into a program, or the user can enter numbers into a calculator. Whatever you want to do. Further examples will be shown in future posts.

Happy coding!

Thursday, April 16, 2015

Everyone Has to Start Somewhere

Welcome to Code Geeks 101! This is a blog dedicated to talking about code, specifically Java, as well as working with Android Studio. We work on our own projects, as well as building apps and programs for other people. If you have any questions, let us know! We will regularly post source code to different functions that we implement into our own projects for you to use, and we will try to answer any questions you have. Happy coding!