Today I will teach you all about the scanner library. I used the scanner library in my last post, but I just realized that you may not yet know what it does! A scanner is a pre-programmed object in java that allows you to input data from the user. To be able to use a scanner in your program, you must first type the following code into your program before the start of the class:
import java.util.Scanner;
That will tell java that you want to use the scanner library for your code. Now that you have imported the scanner, you will need to create a scanner in your code:
Scanner scan = new Scanner(System.in);
This will create a new scanner named scan. Now that you have a new scanner, you can input data from the user. If you want a integer, you type:
int i = scan.nextInt();
For a double, you type:
double d = scan.nextDouble();
For a single word, you type:
String s = scan.next();
For multiple words, you type:
String m = scan.nextLine();
There is one problem with the next line method that you need to be aware of for your code to work properly; if you have a scanner method followed by a nextLine method, the nextLine method will not run. To solve this problem, you add another nextLine that is not attached to anything. This line will then be skipped instead of the line that you needed to run. The second error that you need to look out for is if the user enters a invalid data type. You can solve this problem by using the has method. The has method will check to see if the input is the correct data type and return a boolean value. Here are the has methods:
hasNext()
hasNextLine()
hasNextDouble()
hasNextInt()
Each of these methods check for the the corresponding return type. This is pretty much the basics for the scanner method, so hopefully you have learned a thing or two about the scanner method in java, and please post any questions that you have about anything that I have said in this or any other post in the comments below.
Sunday, October 28, 2012
Sunday, October 21, 2012
10/21/12 For loops
Today I will teach you about For loops. A for loop has three components, an initialization, a condition, and an increment. The initialization will define a new variable with a scope of the for loop, and the condition statement will contain a boolean value that will cause the for loop to run again if the condition is true at the end of the loop. The increment will modify the value of the initialization variable each time the loop runs. Here is how you would code a for statement in java:
for(type var; condition; increment){
}
Now that you know how make for statements, I will show you an example for how to use them:
int total = 0;
Scanner scan = new Scanner(System.in);
System.out.println("how many numbers?");
int times = scan.nextInt();
for(int timesDone = 0; timesDone<=times; timesDone++){
System.out.println("number?");
int number = scan.nextInt();
total+=number;
}
System.out.print("the total is " + total);
This code will first ask you how many numbers you want to add up and then it will prompt you for those numbers one at a time. It then prints the total of those numbers added together.
Hopefully you have learned a thing or two about for loops in java, and if you have any questions, feel free to post them in the comments. I would also like to know if anyone has any suggestions for how to make these posts better.
for(type var; condition; increment){
}
Now that you know how make for statements, I will show you an example for how to use them:
int total = 0;
Scanner scan = new Scanner(System.in);
System.out.println("how many numbers?");
int times = scan.nextInt();
for(int timesDone = 0; timesDone<=times; timesDone++){
System.out.println("number?");
int number = scan.nextInt();
total+=number;
}
System.out.print("the total is " + total);
This code will first ask you how many numbers you want to add up and then it will prompt you for those numbers one at a time. It then prints the total of those numbers added together.
Hopefully you have learned a thing or two about for loops in java, and if you have any questions, feel free to post them in the comments. I would also like to know if anyone has any suggestions for how to make these posts better.
Saturday, October 13, 2012
10/13/12 If and else
Today I will teach you about If and else statements and how to code them in Java. Last week, I tough you how to use booleans and evaluate if a statement is true or false. An if statement runs if and only if the boolean that is entered as a parameter is true. If the parameter is false and you have an else statement right after the if statement, then the else statement will run instead of the if one. This is an example of an if statement in Java:
boolean b = true;
if (b){
System.out.println("true");
}
else{
System.out.println("false");
}
This code would print the word true. If b was false instead of true, the code would print false instead. Instead of just entering a boolean by the if statement, you actually can enter any statement that can be evaluated as eater true or false like 2<4 or 1==2. This is a very simple way to use if statement, but you can do more than just this! If you want to, you can have else if statements that only run if the code before it was false and did not run. This is how it would work:
int test = 3;
if (test == 1){
System.out.println("one");
}
else if (test == 2){
System.out.println("two");
}
else if (test == 3){
System.out.println("three");
}
else if (test == 4){
System.out.println("four");
}
else if (test<=0){
System.out.println("less than or equal to 0");
}
else{
System.out.println("greater than or equal to 5");
}
This code would print the word four, but if test was one the code would print one, two it would print two, three would print three, if test was less than or equal to zero, it would print less than or equal to zero. Now if test was anything else, it would print greater than or equal to 5. Because there is no if after the last else, if none of the else if statements work, then the else statement will run.
Hopefully,
if(you read this post){
System.out.println("you have learned a thing or two about if and else statements");
}
else{
System.out.println("you should read this post so that you can learn more about if and else statements");
=)
Sunday, October 7, 2012
10/7/12 Booleans and basic logic
Today I will teach you about logic with booleans. A boolean is a primitive data type that can only hold a value of 0 or 1. In computer science, You use booleans to represent if a certain statement is true (1) or false (0). An example of a true statement is 2==2 and an example of a false one is 2==3. In java, you can also do complex statements that my include and (&&) or or (||).
Just in case you do not fully understand what I just said, let me give you some examples of what I meant:
1 == 2; is false
2 ==2; is true
Using and and or makes this a little bit more complicated. For && the statement is true iff both sides of the equation are true while for || the statement is true if ether side of the equation is true. I will give you some examples:
true && true; is true
true && false; is false
false && false; is false
true || true; is true
true || false; is true
false || false; is false
We can use both && and || in the same expression. If we do this, we need to know that you evaluate && before ||, but if something is in parentheses, you do that first. Here are some examples:
true && true || false; is true
false && true || false && false; is false
(true || false) && (false || false); is false
false || true && (false || true); is true
These can get infinity complex, but here are some helpful shortcuts to telling if something will be true or false:
1) true || (stuff) is always true
2) false && (stuff) is always false
Hopefully you learned a thing or two about booleans, and please comment if you have any further questions.
PS- This Saturday, I went to a Quiz Bowl tournament and our team won! Go LASA!
PS- This Saturday, I went to a Quiz Bowl tournament and our team won! Go LASA!
Subscribe to:
Posts (Atom)