Helsingin yliopisto / Tietojenkäsittelytieteen laitos / 581258-1 Johdatus ohjelmointiin
Copyright © 1997 Arto Wikla. Tämän oppimateriaalin käyttö on sallittu vain yksityishenkilöille opiskelutarkoituksissa. Materiaalin käyttö muihin tarkoituksiin, kuten kaupallisilla tai muilla kursseilla, on kielletty.

A class for simple reading

(Last update 23.9.1998)

In Java there is no simple way of reading the input - the programmer herself/himself must take care of possible errors etc. That is why I programmed a small class, with which the students can easily read numbers and lines.

An easy (and clumsy!) way of using the class is to copy the Java file Lue.java to the directory, where own programs are stored. Before using the class you must, of course, compile it with the command javac.

You do not need to understand this class yet, just use it...

There are three tools in the class Lue:

  1. Lue.rivi() reads an entire line as a String-value
  2. Lue.kluku() reads an integer (int)
  3. Lue.dluku() reads a floating point number (double)
  4. Lue.merkki() reads a char value (rest of line is lost)
All the methods return a value, so they are used as functions, as expressions:
    int number1;
    double number2;
    String line;
    char character;

    number1 = Lue.kluku();
    number2 = Lue.dluku();
    line = Lue.rivi();
    character = Lue.merkki();

Important: The empty brackets () are necessary!

There is also an English version, Read.java, of this class!