Newsgroup: it.comp.java
Titolo: Simple Input Output Question

Hello,
I'm new on java programming. Sorry if the question is stupid, but
I really don't know how to do that.
I want to read from keyboard two integer values, add them, and 
print the result.
The code:
---------------
import java.io.*;
class Celsius {
        public static void main (String args[]) throws IOException  {
	int C = 0, F = 0;
	DataInputStream stdin = new DataInputStream(System.in);
	C = (int)stdin.readInt();
	F = (int)stdin.readInt();
	System.out.println(C);
	System.out.println(F);
	System.out.println(C+F);
        }
} 
The Output:
----------------
100
200
825241610
825241610
1650483220
I don't understand why there are such values on the variables !
Can somebody give me some advice ?
Best Regards
Marco

 wrote:

Marco,
The problem is that DataInputStream.readInt() is expecting an int but
is getting a character string (that just happens to be a set of
characters that we humans choose to interpret as numeric digits that
could be interpreted as constituting an integer). DataInputStream
doesn't know the difference so interprets the ASCII character codes as
binary data representing an integer.
Modify the two lines of code that do the reads as follows:
   String inputC = (stdin.readLine()).trim();
   C = Integer.parseInt( inputC );
   String inputF = (stdin.readLine()).trim();
   F = Integer.parseInt( inputF );
That's the general idea, but to be safe, you should at least strip
leading and trailing blanks, for example:
   String inputC = (stdin.readLine()).trim();
Robert
pau...@crl.com

Il seguente codice risolve il tuo problema:
import java.io.*;
class Celsius {
        public static void main (String args[]) throws IOException  {
        DataInputStream stdin = new DataInputStream(System.in);
        Integer C = new Integer(stdin.readLine());
        Integer F = new Integer(stdin.readLine());
        System.out.println(C.intValue());
        System.out.println(F.intValue());
        System.out.println((C.intValue()+F.intValue()));
        }
}
Auguri di Buone Feste!!
 
	Gianni.

 
Simple:  the readInt method reads a 32 bit (4 byte) integer value, it
doesn't convert a string to an integer.  
"100\n" is the following four bytes 49, 48, 48, 10 (using ASCII)
If you put those four bytes together to make an integer:
49 * 16777216      (256^3)
48 *    65536      (256^2) 
48 *      256      (256^1)
10 *        1      (256^0)
822083584 + 3145728 + 12288 + 10 = 825241610
try this instead:
C = (new Integer(stdin.readLine())).intValue();
--GW

 
Il problema si trova nell'istruzione di lettura dei valori interi:
 
  C = (int)stdin.readInt();
Questa istruzione realmente legge 4 bytes consecutivi che rappresentano un
intero a 32 bits. Invece dallo standard input si leggono dei caratteri che
dopo potranno essere convertiti ad intero (int).
  C = Integer.valueOf(stdin.readLine()).intValue();
 
  - stdin.readLine() : Legge una stringa di caratteri ASCII (String) fino
alla fine della riga.
  - Integer.valueOf() : Metodo statico che converte un valore "String" in
"Integer".
  - intValue() : Valore "int" contenuto in un "Integer".
Il programma modificato sarebbe:
import java.io.*;
class Celsius {
     public static void main (String args[]) throws IOException  {
         int C = 0, F = 0;
         DataInputStream stdin = new DataInputStream(System.in);
         C = Integer.valueOf(stdin.readLine()).intValue();
         F = Integer.valueOf(stdin.readLine()).intValue();
         System.out.println(C);
         System.out.println(F);
         System.out.println(C+F);
     }
}
(Nota: Il file sorgente è allegato a questo messaggio) 
-- 
 _____________________________________
|                                     
|   Rafael Andrés Marín de la Cruz    
|
|           DIALECTICA s.a.s.         
|        Consulenza Informatica      
|            Milano,Italy             
|
| 
http://www.galactica.it/dialectica
| e-mail: 
dialect...@galactica.it