Java : Reading Input

Reading an input, can be done in two ways,

1. Using a buffered reader
2. Using a JOptionPane

I Prefer the second.

Using buffered reader:

import java.io.*;

First create a InputStreamReader for the System.in object

InputStreamReader fis = new InputStreamReader(System.in);

Then create a buffered reader for fis object,

BufferedReader br = new BufferedReader(fis);

now use the readLine() method of BufferedReader to read input which returns a string.

String input = br.readLine();

Using JOptionPane:

Use the swing package.

import javax.swing.JOptionPane;

now use the showInputDialog() method to get the input.

String input = JOptionPane.showInputDialog(“Enter your name:”);

The string passed inside the InputDialog will be shown on a DialogBox and a text field is give to enter the input.

The value should be collected into a string. And parsed to it’s respective type.

For example, assume I want to get the age of a person, so,

String input = JOptionPane.showInputDialog(“Enter your age:”);

int age = Integer.parseInt(input);

The string value is converted to an integer value using the parseInt() method available in Integer class.

Advertisements

Java – Environment Variables

After you install Java SDK in your Windows system, here are some things that you should do.

Right-Click My Computer and go to Properties.

Then click Advanced and the Environment Variables button.

Add a new Environment Variable called JAVA_HOME and enter the path where the java software is installed.

By Default: C:\Program Files\Java\jdk1.5.0\

Also ,

append the following to the PATH variable after a semi colon.

C:\Program Files\Java\jdk1.5.0\bin;

Now wherever you want you can write your java file and compile & Run it.

Java Introduction

Hi,

Here is a quick rundown on the basics of Java…

Introduction:

The Java SDK and JVM-

Download Java SDK at the SUN website. Also get software like JCreator, BlueJ or eclipse for your comfort.

www.bluej.org/download/download.html

www.eclipse.org/downloads/ – Eclipse for Java developer’s

http://www.jcreator.com/download.htm – JCreator LE version(it’s free)

Also do download the Java Documentation from SUN, it’ll help you a lot in development.

All these will be necessary , if you are going to write code in Java.

If you just want to learn the concepts, then don’t bother. But I tell you this, coding in java is the easiest and best way to learn it. And to top it all it’s fun, atleast for people like me.

Well let’s make you understand some basic stuff,

Class – It’s a template (or just something for now) from which you create objects.

Objects – Most of the operations in java are performed on an object. An object is an instance of a class.

If you didn’t get it don’t worry, just keep reading.

Here is a class,

class Student
{
private String name;

public Student(String name)
{
this.name = name;
}

public void getName()
{
return this.name;
}

public static void main(String args[])
{
Student st = new Student(“Jovian”);
System.out.println(st.getName());
}

}

This is a class student which gives you an outline of what does it contain. It specifies some fields , in this case it is the name of the student. Also it specifies some method, in this case it is getName() which when executed will return the name of the student.

Note the code inside the main(),

Student st = new Student(“Jovian”);

Here st is an object of class Student. “st” has the field name and the method getName() which is specified by the class.

To call the method getName we use

st.getName();

Let’s assume that I write this piece of code in a file named as MyStudent.java

when i compile this file a class file is created and it is saved at the same directory where MyStudent.java exists.

Now let’s break up this process,

After you install Java and set the environment variables [ refer to Java -Environment Variables Topic] you have an SDK i.e. Software Development Kit, which has something called a JVM [ Java Virtual Machine ].

The JVM compiles the java file and lets you know if you have any errors. Thus compilation leads to forming of the class file. The name of the class file will be the name of the class which you have specified in the file. In this case it’ll be Student.

Now lets see how to execute the code,

when you run command > java Student

The JVM interprets the class file into the binary file and is executed by the machine. And you get the output as Jovian which is the name of the student.

Thus Java language is an compiled and interpreted.