Java Input Using the In Class


Java is a very powerful and versatile programming language.  One of the reasons why Java is so popular is due to the fact that Java programs can run on many different devices and platforms.

 

Unfortunately, all of that power comes at a price: Java's input and output (particular the input) can be quite complicated and intimidating for new programmers.

 

To help resolve this, I use a special library of input commands for students who are new to Java (ICS3U).  Here is how to get it working:

 

  1. Download the In.class file from the provided link.
  2. You should already have your computer science course and files organized by folders.  At the very least, you should have some like:
    P:\00 ICS3U\Java
  3. Save the In.class file in the folder of your choice (e.g., the folder shown above).  Make sure it is spelled exactly "In.class", with a capital "I" to start, and everything else lowercase.
  4. In Dr.Java, you will be changing your preferences:
    Edit->Preferences
  5. You want to "Add" an "Extra Classpath".  Browse to the location where you saved "In.class" and choose "Select".  You class path should now be:
    P:\00 ICS3U\Java

    You should NOT have the name of the file, "In.class", in the class path.

 

Now that you have add the extra class path, you should have access to all of the input methods contained inside In.class.  Here is some sample code to get you started:

 

class TestInClass
{
  public static void main(String[] args)
  {

    System.out.println("Integer value?");
    int x = In.getInt();

    System.out.println("Decimal value?");
    double y = In.getDouble();

    System.out.println("String value?");
    String z = In.getString();
    
    System.out.println("The integer was " + x);
    System.out.println("The double was " + y);
    System.out.println("The string was " + z);
  }
}