Article View: es.comp.os.linux.programacion
Article #7991Re: Leer un fichero
From: Alvaro Sanchez-Mariscal
Date: Fri, 03 Jan 2003 01:06
Date: Fri, 03 Jan 2003 01:06
75 lines
2594 bytes
2594 bytes
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
>> Quiero sacar la media de una serie de datos que tengo almacenados en
>> un fichero. El fichero podria ser de la siguiente manera:(datos.txt)
>> 15
>> 47
>> 30
>> 80
>> 11
>> 42
>> 65
>> 2
>> 25
>> 23
>> 16
Son casi las 2 de la ma�ana y no puedo dormir, as� que ah� va una
soluci�n en Java (s�, en Java, me he pasado con los caf�s hoy...).
import java.io.*;
public class PruebaEstadisticas {
public static void main(String[] args) {
String linea = "";
double resultado = 0;
int i = 0;
try {
BufferedReader br = new BufferedReader(new
FileReader(args[0]));
} catch (FileNotFoundException fnfe) {
System.err.println("Feliz 2003!");
System.exit(-1);
}
try {
linea = br.readLine();
while (linea != null) {
linea = br.readLine();
try {
resultado += Double.parseDouble(linea);
i++;
} catch (NumberFormatExceptio nfe) {
System.err.println("xDDDDD");
}
}
} catch (IOException ioe) {
System.err.println("Jur jur");
System.exit(-1);
}
System.out.println("La media es: "+(resultado/i));
}
}
Como ves es la soluci�n m�s *r�pida* y *sencilla*. �Qui�n dec�a que Java
no val�a para todo? :-D
Buenas noches.
- --
.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
Alvaro Sanchez-Mariscal
mariscal@di.uc3m.es
Debian GNU/Linux 3.0
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org
iD8DBQE+FOIKJKROHiQUCxMRAr+xAKCEMS3rnO84KO17JvXR6AMb+KS98gCdGjGh
xUDvLhyCxIRwEAdxWjreMFg=
=Bp2P
-----END PGP SIGNATURE-----
Technical Details
Message-ID:
<av2nni$b9p30$1@ID-146972.news.dfncis.de>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!fu-berlin.de!uni-berlin.de!213.37.153.58!not-for-mailReferences:
<7d4ff0e4.0301020707.6388a1a6@posting.google.com> <av2gsa$fs2$1@titan.bt.es>Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: f86c8,5cf283a7acb0d167 X-Google-Attributes: gidf86c8,public X-Google-ArrivalTime: 2003-01-02 17:06:59 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!fu-berlin.de!uni-berlin.de!213.37.153.58!not-for-mail From: Alvaro Sanchez-Mariscal <mariscal-NOSPAM@di.uc3m.es> Newsgroups: es.comp.os.linux.programacion Subject: Re: Leer un fichero Date: 3 Jan 2003 01:06:58 GMT Organization: Universidad Carlos III de Madrid Lines: 75 Message-ID: <av2nni$b9p30$1@ID-146972.news.dfncis.de> References: <7d4ff0e4.0301020707.6388a1a6@posting.google.com> <av2gsa$fs2$1@titan.bt.es> NNTP-Posting-Host: 213.37.153.58 Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: fu-berlin.de 1041556018 11854944 213.37.153.58 (16 [146972]) User-Agent: slrn/0.9.7.4 (Linux)