.

Comparing string object

It is often necessary to compare strings to see if they are the same, or to see which comes first in alphabetical order. It would be nice if we could use the comparison operators, like == and >, but we can’t. To compare Strings, we have to use the equals and compareTo methods. Syntax for using method compareTo : str1.compareTo(str2); The…

Read More

User Input from Keyboard

Accepting keyboard input in Java is done using a Scanner object. Consider the following statement Scanner console = new Scanner(System.in) This statement declares a reference variable named console. The Scanner object is associated with standard input device (System.in). To get input from keyboard, you can call methods of Scanner class. For example in following statment…

Read More

Reading Data from Text File

In Section 2.5, you learned how to use a Scanner object to input data from the standard input device (keyboard). Recall that the following statement creates the Scanner object console and initializes it to the standard input device: Scanner console = new Scanner(System.in); You can also use the Scanner class to read input from a file….

Read More

Writing data to a text file in Java

Writing Data to a Text File The simplest way to write text to a file requires us to use PrintWriter class from the standard package java.io . The class PrintWriter has the familiar print() and println() methods we have been using for writing to the console. The following program writes the name of four oceans…

Read More

File Input and Output in Java

Introduction to File Input and Output Data stored in variables and arrays is temporary — it’s lost when the program terminates. Java allows a program to read data from a file or write data to a file. Once the data is saved in a file on computer disk, it will remain there after the program…

Read More

Operator Precedence

Consider the following statement: X = 5 + 30 * 10 x will store 305. In this equation, the numbers 30 and 10are multiplied first, and the number 5 is added to their product. Multiplication, division and modulus have higher order than addition and subtraction, which means that they’re performed first. If two operators sharing…

Read More

Downloading and Installing Java in windows

1.1 Getting Started In order to teach you about java programming, we assume that you’ve done little or no programming in the past and you are using a machine running Windows. What you need To write all our code, we’ll use Notepad, a simple editor included with the Windows platforms. In order to run Java programs,…

Read More