Java Video Tutorial 2

Java Video Tutorial 2In this video I continue where I left you in part 1 of the Java Video Tutorial. Definitely watch it first.

Here I show you how to import class libraries, except user input, check for valid input, catch errors in input, a bunch of math functions and more.

All of the code follows the video. You should print it out because it will greatly help you to understand the code better.

If you like videos like this share them

Code From Video

// To import an external class you use import
// You can import whole libraries of classes like this import java.util.*;
import java.util.Scanner; 

public class LessonTwo
	/* static means that only a class can call for this function to execute
	* Creates a new scanner object named userInput
	* You create the Scanner object by calling new and passing the Scanner constructor
	* the input stream to look at ( = keyboard input)
	static Scanner userInput = new Scanner(;
	public static void main(String[] args)
		System.out.print("Your favorite number: "); // Same as println without a newline
		/* The if statement will only execute the code that lies between {} if the value inside () is true
		 * userInput.hasNextDouble() returns true if the next value entered is a Double
		 * There are similar methods for the other data types
		 * hasNextInt() : Integer input
		 * hasNextFloat() : Float input
		 * There are others for Boolean, Byte, Long, and Short
		if (userInput.hasNextInt())
			int numberEntered = userInput.nextInt();
			/* userInput.nextDouble() receives user input and stores it in the variable numberEntered
			 * You have to use a different method based on the type of input
			 * nextInt() : Works for Integers
			 * nextDouble() : Works for Doubles
			 * nextFloat() : Works for Floats
			 * nextLine() : Works for Strings
			 * There are others for Boolean, Byte, Long, and Short
			 * If the user enters a value of the wrong type the program crashes
			System.out.println("You entered " + numberEntered);
			// Here I perform basic mathematics calculations
			int numEnteredTimes2 = numberEntered + numberEntered;
			System.out.println(numberEntered + " + " + numberEntered + " = " + numEnteredTimes2);
			int numEnteredMinus2 = numberEntered - 2;
			System.out.println(numberEntered + " - 2 " + " = " + numEnteredMinus2);
			int numEnteredTimesSelf = numberEntered * numberEntered;
			System.out.println(numberEntered + " * " + numberEntered + " = " + numEnteredTimesSelf);
			// Without the cast the value wouldn't consider fractions
			double numEnteredDivide2 = (double)numberEntered / 2; 
			System.out.println(numberEntered + " / 2 " + " = " + numEnteredDivide2);
			// % Modulus returns the remainder of a division
			int numEnteredRemainder = numberEntered % 2;
			System.out.println(numberEntered + " % 2 " + " = " + numEnteredRemainder);
			// Shorthand way to add to 2 to a variable and assign the result to self
			numberEntered += 2; // *= 	/=	%=  Also work
			numberEntered -= 2;
			// Shorthand way to add 1 to a variable
			// Shorthand way to subtract 1 from a variable
			int numEnteredABS = Math.abs(numberEntered); // Returns the absolute value
			// Returns the larger of the two arguments (They must be of the same type)
			int whichIsBigger = Math.max(5, 7); 
			// Returns the smaller of the two arguments (They must be of the same type)
			int whichIsSmaller = Math.min(5, 7);
			// Returns the square root argument 
			double numSqrt = Math.sqrt(5.23);
			// Rounds the number provided up
			int numCeiling = (int) Math.ceil(5.23);
			System.out.println("Ceiling: " + numCeiling);
			// Rounds the number provided down
			int numFloor = (int) Math.floor(5.23);
			System.out.println("Floor: " + numFloor);
			// Rounds the number based on the fraction
			int numRound = (int) Math.round(5.23);
			System.out.println("Rounded: " + numRound);
			// Generates random numbers between 0 to 9
			int randomNumber = (int) (Math.random() * 10); 
			System.out.println("A random number " + randomNumber);
		// If the above condition is false, the code following else is executed	
		} else {
			System.out.println("Sorry you must enter an integer");

57 Responses to “Java Video Tutorial 2”

  1. punit says:

    Thank you, again awesome tutorial, keep it up…

  2. supprof1 says:

    thank you derek
    you did another impact with this serie of java tutorial as always

  3. Israel says:

    As always, magnificent!

  4. kamgar ahmad durrani says:

    awesome as always

  5. Djpancake says:

    Love all your tuts
    I am going to make an typing calculator out of this
    Your awesome keep up the brill tutorials

  6. Mickey says:

    Great tuts Derek. There is one rule here 🙂 When you want to learn something, almost every journey on finding youtube tutorials ends up on some of your series. 🙂

  7. bill gates says:

    How much will it cost to download the java videos? I travel alot and so my connection is not the most reliable. thanks

    • admin says:

      I don’t charge anything. A site like Keepvid will allow you to download any YouTube video from what I’ve heard? The only reason why I don’t make the videos very easy to download is because a bunch of people started downloading and then selling them. It’s still possible to do though

      • bill gates says:

        selling? wow. thats so wrong. I wanted to view your lessons on my tablet while I travel as thats when I have the most time to devote in front of a screen to learn, otherwise I’m gaming inbetween. You should setup at and make if ongoing. the java tutorials there suck. I like the way you are very thorough in your explanations and use of plain english. well, kudos for this. keep up the excellent job.

  8. joel says: rock..really helpfull.never seen a java tutorial like this b4.god bless

  9. Joel says:

    Super derek.i love your making it so simple need of referring to any textbooks.can you explain us some of the frameworks.

  10. SouravB says:

    Please tell me where I am wrong as and in this program after it gets compiled suppose i press a number and enter then nothing gets printed and then again if I press another number , it gets printed… Plz help!!!

    import java.util.Scanner;
    public class JavaLessonTwo {
    static Scanner userInput = new Scanner(;

    public static void main(String[] args) {

    int Scanner = userInput.nextInt();

    int numberEntered = userInput.nextInt();
    System.out.println(“The number you entered is :” + numberEntered);



  11. Calvin says:


  12. Anonymous says:

    please i need to download this video,have not been able to download it,or is it not free?

  13. Paul says:

    Just wondering, If I were to create a basic calculator with some integers, how would I make it so the number the user entered would be added to the second number the user entered.

  14. MSkiLLz says:

    Great vids. I have a couple of questions.

    1. Is there a difference between numberEntered++ and ++numberEntered? Or is it just a preference thing?

    2. I’ve noticed you use a lot of functions within other classes like min/max in the math class. My question is how do you know that class contains those functions? Is there a pretty list out there within ecclipse that lets you know what functions are within a certain class? Or do you just memorize this? I was recently told to sort numbers within an array and it would have helped if I knew that the Array class had a sort function :/

    • Derek Banas says:

      Thank you 🙂 numberEntered++ uses the original value of numberEntered in an operation and then increments it after. ++numberEntered would increment first and then use the new value in an operation. Does that make sense?

      I’m not big on memorizing. Naturally you’ll just remember things like this through repetition. I use the Java API when I want to know what methods an object may have.

  15. gaurav says:

    i want to ask one favour derek sir …i’m a college guy from india.
    recently i came across ur java tutorials,which i guess r realy gud or rather best till d date…
    so the problem is i want to download all of the core java tutorials coz i will be leaving for college in 10 days… but the data is quite large…n my net is not very gud…
    so cud u plz find any way to compress them or rather highly compress them n we people can decompress thm wid the tools u compress.. videos in 720p will do the work…
    n sorry if i had asked too much….

    • Derek Banas says:

      Hi, I’m sorry, but my hosting company will not allow me to offer the videos through direct download off my site. I put everything on YouTube because they provide the best streaming. I wish there was something I could do 🙁

  16. Edmond says:

    Really an interesting tutorial – truely well presented and gripping!
    It would be most greatfully appreciated if you would kindly advise and instruct how to write a Program Design Language (PDL) version of the above code, please.
    Thanks again for your great tutorials. Hope there is no stopping you.

  17. Random_Guy says:

    Derek, I learned Jquery from videos and I love your way to explain. Thanks man…
    It will help me to recollect my Java skills.

  18. Jason Tucker says:

    Thanks for all the work you put into these. They are very helpful! 🙂

  19. Tim Neal says:

    Im currently in a Java class. Thank you for putting out these videos. Can you please tell me which video explains constructors in a class?

    Thank you,


  20. Luis says:

    Thank you again, i have to take a class based on Java but I am only familiar in c++, that was like 2 yeArs ago these tutorials are helpings out a whole bunch…thank you

  21. Tariq says:

    Thank you for this.
    I do shell scripting as part of my job, however am going back to school and before picking up my compilers class after AGES, will be going through all your tutorials. It looks like it’s got a fantastic pace and very clear directions.

  22. Amit kaushik says:

    You makes the Java learning really easy. I hate learn java from books, so i loves watching your videos.

  23. alan says:

    Simply the best teacher of all. Thank you

  24. Daniel Reis says:

    This is really amazing. I started watching some java tutorial videos on udemy and it has definitly nothing to do with yours. You easily surpass any of its videos by just a few words and the way you organize your work and actually show it to the people. Its motivating. I hope you stick around for a very long time. I’ve got much to learn yet ;P .

    Keep up the excelent work Derek. You are definitly a masterpiece when it comes to tutorials.

    Best of luck!

    • Derek Banas says:

      Thank you for all the nice compliments 🙂 It is always nice to hear that I’m providing videos for free to compare well against the expensive courses others are putting out. Many more are coming.

Leave a Reply

Your email address will not be published.