Learn Java programming tutorial lesson 5 - Data input and type conversions


Until now we have set the values of variables in the programs. Now we will learn how to get input from the user so that our programs are a bit more interesting and useful.

Reading single characters

To read a single character from the user you must use the System.in.read() method. This will return and integer value of the key pressed which must be converted to a character. We use (char) in front of System.in.read() to convert the integer to a character. After the user has pressed the key they must press enter and we must put a second System.in.read() to catch this second key press.
public class ReadChar
{
   public static void main(String[] args) throws Exception
   {
      char c;
      System.out.println("Enter a character");
      c = (char)System.in.read();
      System.in.read();
      System.out.println("You entered " + c);
   }
}

You will see that it says "throws Exception" at the end of the main method declaration. An exception is an error and "throws Exception" tells java to pass the error to the operating system for handling. Your program will not compile if you don't put this in.

Reading strings

The easiest way to read a string is to use the graphical input dialog. We must import the JOptionPane.showInputDialog method before we can use it. Then we can use the JOptionPane.showInputDialog method to read the string. We can print the entered string using the JOptionPane.showMessageDialog method. You must then put a System.exit(0) at the end of the program or else the program will not stop running.
import javax.swing.*;

public class ReadString
{
   public static void main(String[] args)
   {
      String s;
      s = JOptionPane.showInputDialog("Enter your name");
      JOptionPane.showMessageDialog(null, "Your name is " + s);
      System.exit(0);
   }
}

Type conversions

Once you have read a string you might want to convert it to another data type such as an integer. You have already seen in a previous example that you must use the type that you want to convert to between brackets in front of the variable to be converted. Here is an example of some conversions:
public class Convert
{
   public static void main(String[] args)
   {
      int i;
      char c = 'a';
      i = (int)c;
      i = 5;
      c = (char)i;
   }
}

Strings are not variables but actually classes which is why we must use the Integer.parseInt() method to convert a string to an integer.
import javax.swing.*;

public class ConvertString
{
   public static void main(String[] args)
   {
      String s;
      int i;
      s = JOptionPane.showInputDialog("Enter a number");
      i = Integer.parseInt(s);
      JOptionPane.showMessageDialog(null, "The number you entered is " + i);
      System.exit(0);
   }
}
Setec Institute || Group : SH6 | | Name : Kim Chanthy. Powered by Blogger.
 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hot Sonakshi Sinha, Car Price in India