The numerical input problem: reading numbers from input in Java
  • Java's library method to read integer typed input:

      import java.util.Scanner;
    
      public class NumInp
      {
         Scanner in = new Scanner(System.in);
         int x;
    
         x = in.nextInt( );
         ...
      }
      

  • The nextInt( ) is as follows:

       public int nextInt( )
       {
           String s = next( ); // Read the number as String (ASCII codes)
           return Integer.parseInt(s);  // Convert to int and return it
       }
    

Why is number input so difficult while writing numbers in a program so easy

  • Integer numbers used in a computer program:

       int myFunction( )
       {
           int x = 15;   // Program can use numbers easily
       }
    

  • Integer numbers read in from the input (= keyboard) into a computer program:

      import java.util.Scanner;
    
       int myFunction( )
       {
          Scanner in = new Scanner(System.in);
          int x;
    
          x = in.nextInt( );   // User enters "15"
       }
    

  • Why is the latter so much more complicated ???

Compile-time versus run-time events

Compile-time events happen during the translation of a source program (Java, C, etc) to machine code:

Translation of constants used inside source program

Review:   integer constants (used in calculations) in the source program are translated into the 2s complement representation by the compiler:

Compile-time versus run-time events

Run-time events:   are events that happen during the execution of a machine code program:

 

Run-time events happen after the compilation process !!!

Important note:   during the run-time, the compiler is not running !!!

Compile-time versus run-time events

Run-time input:   a running program can read numerical input from the keyboard:

 

Important reminder:   run-time numeric inputs from keyboard are received as ASCII codes

The numerical input problem - illustrated

Recall that when you enter numerical input into a running program, the data is stored as ASCII code in memory:

The ASCII code data is not converted into 2s complement code (compiler is not running) !!

The numerical input problem - illustrated

The numerical data in ASCII code must be converted into 2s complement code before the program can use it in computations:

Converting a ASCII coded number 2s complement code is a little bit complicated !!

The app shows the need to convert number string ==> 2s compl code

Keyboard inputs can only allow users to enter a number string in ASCII code:

  • When user types this input
       

  • The computer program will receive the following:
       

Very important fact:

  • The compiler will/can not process data entered in an input operation (while the program is running

  • Therefore, the program must convert the number string (e.g. "12") to its 2s complement representation (e.g. 00001100) using an algorithm (= parseInt() !

Order of discuss
 

I will explain the conversion algorithm (used by a program) in a piece meal fashion:

  1. First, I will show you a naive solution that is simple to understand what we need to do

  2. Next, I will show you a simple solution that can only convert a number string that consists of exactly 1 character

  3. Then, I will show you a more complex solution that can only convert a number string that consists of exactly 2 characters

  4. Final, I will show you the general solution