Friday, October 8, 2010

7. Reading Input from the user!!!!!!

Okay so now that you know a couple things about java it's time to interact with your user!

In order to do this you need to use a Scanner which "scans" in what the user enters!  In order to use this scanner you need to have the code for it, don't worry you don't have to download some random code from some site you don't know, it's all on your computer already you just need to access it.  For regular java stuff (like what I've already told you) you don't need any special classes or code, this Scanner is something not included with the regular java stuff.

So enough of that, the code that you use to access the Scanner code is this:
import java.util.Scanner;
This will let you import the Scanner stuff you need.  Now you have to tell the computer you need to import before you actually do anything so this code HAS TO BE AT THE VERY TOP OF YOUR CLASS!!!!!!  Before you write anything put this at the very tippity-top.

In order to set up your scanner you need to do something like this.
Scanner input= new Scanner(System.in);
This creates a new scanner object which you can read data or input from.  (You don't have to call your scanner "input", you could name it "myScanner" or something and it wouldn't make a difference) So now that you have your scanner here's how you read in some code.

To read in the next line of input you do:
String myStringNameGoesHere = input.nextLine();
To read in the next integer you do this:
Int myIntNameGoesHere = input.nextInt();
To read whatever is inputted next (basically the next word) you do this:
String myStringNameGoesHere = input.next();

Okay so lets put it into action, here is the code for a simple java program that reads in you name and your age and then prints it out again:

import java.util.Scanner;

public class Userstuff
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please input your name and then click enter: ");
        String name = input.nextLine();
        System.out.print("Please input you age and then click enter: ");
        int age = input.nextInt();
        System.out.println("Hello " + name + " you are " + age + " years old!");
    }
}


Create a new class called Userstuff and paste this code into it, hopefully it should work, if not post a comment and I'll try to help you out.  Next I'll show you how to use the if statement so you can tell the user different things depending on what they enter!  Stay tuned.

EDIT:  Now unfortunately this doesn't seem to accommodate long lines of stuff so the code  looks really weird.  If you copy and paste it it should look fine but if not then look at my source code section and download the source code for this.
Or, if your feeling really brave, try writing it in!!!!!

Tuesday, October 5, 2010

Sorry!

So I obviously haven't been posting for a while but I have been caught up with school work and stuff and I'm not sure if I want to keep posting!  I'm trying to get into iPhone programming right now and so I don't have too much time for this blog.  I will try to post little tutorials about Java stuff and may keep doing these simple basic Java tuts.  I'm also trying to figure out a good way to organize the site because it's not organized too well.  Anyways, that what I have to say!  I'll try to post soon!