Console input in java using scanner

Often we have to read input from the user through console and most of the time we need to read primitive types(int,long,float,char ...). For reading an array of integers as input , we often coded as shown below. That is we input was read as a string and converted to integer for using it.

int[] array = new int[size];
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int j = 0; j < array.length ; j++) {
int k = Integer.parseInt(br.readLine());
array[j] = k;
}
}catch (Exception e) {
e.printStackTrace();
}

From J2SE 5.0 onwards Java has added a new classes called Scanner which is basically used for reading primitive types of data. The above code can be implemented with the Scanner class as shown below.

int[] array = new int[size];
try {
Scanner in = new Scanner(System.in);
for (int j = 0; j < array.length ; j++) {
int k = in.nextInt();
array[j] = k;
}
}catch (Exception e) {
e.printStackTrace();
}

Scanner class is pretty much simpler to work with than the BufferedReader, when you have to read only the primitive types.

Scanner class can also be used for parsing input based on regular expression. For more details check http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

Network manager applet

The laptop users usually want switch their network when they move from place to place. The easiest way to do this is using the Network manager. You can configure more than one connection and select the one which you want at that moment.

You might have seen the Network manager applet on your GNOME panel.
You might have also noticed that it shows not connected even if your system is connected to network. This is because the network is configured in your system using some other method(For example /etc/network/interfaces and the networking service). To use the Network manager do as follows
Edit the file /etc/NetworkManager/nm-system-settings.conf
[main]
plugins=ifupdown,keyfile

no-auto-default=00:29:d1:62:2e:c5,

[ifupdown]
managed=true
Change the value managed to true(managed=true).
Now your networking device is managed by the the Network manager. You may require a restart.

To add different addresses, right click on the applet on your panel(In KDE, a plasma widget is available for network manager. In GNOME the applet is on the panel)
Add your network address. You can add more than one address, and different names for each of them. To enable a particular address at a time , left click on the applet and select the one you want.