funcion para asignar un long a variable....

errjey
13 de Marzo del 2004
Wenas!!

Seguro q es una xorrada pero es k aun no tengo los conocimientos para poder hacerlo yo mismo.

Necesito una funcion que lea un long introducido x teclado y lo asigne a una variable.

EJ. a = readlong();

Muchas gracias!!

cricubx
13 de Marzo del 2004
TOMA UTILIZA ESTA CLASE PARA LEER TODO TIPO DE DATOS SI LA INCLUYES EN EL MISMO DIRECTORIO DE TUS PROGRAMAS LA PUEDES USAR Mensionando el nombre de la clase y su atributo.... EJEMPLo
a=datoLong();

import java.io.*;
public class Lee
{
// Definir un flujo de caracteres de entrada: flujoE
private static InputStreamReader isr = new InputStreamReader(System.in);
private static PushbackReader flujoE = new PushbackReader(isr);
public static void limpiar()
{
int car = 0;
try
{
while (flujoE.ready()) flujoE.read(); // limpiar flujoE
}
catch(IOException e)
{
System.err.println("Error: " + e.getMessage());
}
}

public static char mirar()
{
int car = 0;
try
{
car = flujoE.read();
flujoE.unread(car);
}
catch(IOException e)
{
System.err.println("Error: " + e.getMessage());
}
return (char)car; // retornar el primer carácter disponible
}

public static String dato()
{
StringBuffer sdato = new StringBuffer();
int car = 0;
try
{
// Leer. La entrada finaliza al pulsar la tecla Entrar
while ((car = flujoE.read()) != 'r' && car != -1)
sdato.append((char)car);
limpiar();
}
catch(IOException e)
{
System.err.println("Error: " + e.getMessage());
}
if (car == -1) return null;
return sdato.toString(); // devolver el dato tecleado
}

public static char carácter()
{
int car = 0;
try
{
car = flujoE.read();
}
catch(IOException e)
{
System.err.println("Error: " + e.getMessage());
}
return (char)car; // devolver el dato tecleado
}

public static short datoShort()
{
try
{
String sdato = dato();
if (sdato == null)
{
System.out.println();
return Short.MIN_VALUE;
}
else
return Short.parseShort(sdato);
}
catch(NumberFormatException e)
{
System.out.print("Ese dato no es válido. Teclee otro: ");
return datoShort();
}
}

public static int datoInt()
{
try
{
String sdato = dato();
if (sdato == null)
{
System.out.println();
return Integer.MIN_VALUE;
}
else
return Integer.parseInt(sdato);
}
catch(NumberFormatException e)
{
System.out.print("Ese dato no es valido. Teclee otro: ");
return datoInt();
}
}

public static long datoLong()
{
try
{
String sdato = dato();
if (sdato == null)
{
System.out.println();
return Long.MIN_VALUE;
}
else
return Long.parseLong(sdato);
}
catch(NumberFormatException e)
{
System.out.print("Ese dato no es válido. Teclee otro: ");
return datoLong();
}
}

public static float datoFloat()
{
try
{
String sdato = dato();
if (sdato == null)
{
System.out.println();
return Float.NaN; // No es un Número; valor float.
}
else
{
Float f = new Float(sdato);
return f.floatValue();
}
}
catch(NumberFormatException e)
{
System.out.print("Ese dato no es válido. Teclee otro: ");
return datoFloat();
}
}

public static double datoDouble()
{
try
{
String sdato = dato();
if (sdato == null)
{
System.out.println();
return Double.NaN; // No es un Número; valor double.
}
else
{
Double f = new Double(sdato);
return f.doubleValue();
}
}
catch(NumberFormatException e)
{
System.out.print("Ese dato no es válido. Teclee otro: ");
return datoDouble();
}
}
}

CHIAO

errjey
13 de Marzo del 2004
Perfecto!! Es que tenia una libreria parecida que va muy bien, pero justamente no tiene funciones para "long", intente hacer una funcion yo, pero no lo consegui.
Aqui te pongo la libreria que utilizo.

/* Aquesta es una adaptacio d'un conjunt de classes
per a fer entrada/sortida de texte amb Java.
La versio original va ser creada per Per Brinch
Hansen (s'inclou el copyright de la versio original)
*/

/* THE JAVA TEXT PROGRAM
28 April 1999
Copyright (c) 1999 Per Brinch Hansen

This program defines a set of Java classes for text
processing. These text classes enable Java programs to
output text to screen and disk files and input text from
keyboard and disk files. The present version runs on
Macintosh, Unix, and Windows 95 systems.
*/

import java.io.*;

class inout
{ private final char cr = 'r', eof = 'uFFFF',
nl = 'n', sp = ' ';
private final String EOF = "eof";


public inout() { }

/* instruccions d'escriptura */

public void write(boolean value) throws Exception
{ write(value, 1); }

public void write(boolean value, int width)
throws Exception
{ String word = (String)(value?"true":"false");
writespace(width - word.length());
write(word);
}

public void write(char value) throws Exception
{ System.out.print(value); }

public void write(char value, int width) throws Exception
{ write(value); writespace(width - 1); }

public void write(double value) throws Exception
{ write(value, 1); }

public void write(double value, int width)
throws Exception
{ String numeral = String.valueOf(value);
writespace(width - numeral.length());
write(numeral);
}

public void write(int value) throws Exception
{ write(value, 1); }

public void write(int value, int width) throws Exception
{ String numeral = String.valueOf((int)value);
writespace(width - numeral.length());
write(numeral);
}

public void write(String value) throws Exception
{ write(value, 1); }

public void write(String value, int width)
throws Exception
{ int length = value.length();
for (int i = 0; i < length; i++)
write(value.charAt(i));
writespace(width - length);
}

public void writeln() throws Exception
{ write(nl); }

public void writeln(boolean value) throws Exception
{ writeln(value, 1); }

public void writeln(boolean value, int width)
throws Exception
{ write(value, width); writeln(); }

public void writeln(char value) throws Exception
{ writeln(value, 1); }

public void writeln(char value, int width)
throws Exception
{ write(value, width); writeln(); }

public void writeln(double value) throws Exception
{ writeln(value, 1); }

public void writeln(double value, int width)
throws Exception
{ write(value, width); writeln(); }

public void writeln(int value) throws Exception
{ writeln(value, 1); }

public void writeln(int value, int width) throws Exception
{ write(value, width); writeln(); }

public void writeln(String value) throws Exception
{ writeln(value, 1); }

public void writeln(String value, int width)
throws Exception
{ write(value, width); writeln(); }

public void writesame(char value, int number)
throws Exception
{ for (int i = 1; i <= number; i++) write(value); }

public void writespace(int number) throws Exception
{ writesame(sp, number); }


/* instruccions de lectura */

private BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
private char[] buffer = new char[80];
private int typed= 0, used= 0;

private char readkey() throws Exception
{ if (used == typed)
{ String line = readkeyline();
if (line.equals(EOF))
{ typed = 0; buffer[typed] = eof; }
else
{ typed = line.length();
for (int i = 0; i < typed; i++)
buffer[i] = line.charAt(i);
buffer[typed] = nl;
}
typed = typed + 1; used = 0;
}
char ch = buffer[used];
if (ch == cr) ch = nl;
used = used + 1; return ch;
}

private String readkeyline() throws Exception
{ String line = in.readLine();
return (line == null?"":line);
}

private boolean ahead = false;
private char ch /*next character (if ahead)*/;


public boolean blank() throws Exception
{ getahead();
return (ch == cr) | (ch == nl) | (ch == sp);
}

public boolean digit() throws Exception
{ getahead();
return ('0' <= ch) & (ch <= '9');
}

private void getahead() throws Exception
{ if (!ahead) readnext(); }

public boolean letter() throws Exception
{ getahead();
return (('a' <= ch) & (ch <= 'z')) |
(('A' <= ch) & (ch <= 'Z'));
}

public boolean more() throws Exception
{ getahead(); return ch != eof; }

public char next() throws Exception
{ getahead(); return ch; }

public char read() throws Exception
{ getahead(); ahead = false; return ch; }

public void readblanks() throws Exception
{ while (blank()) readnext(); }

public boolean readboolean() throws Exception
{ boolean value; String symbol = readname();
if (symbol.equals("false")) value = false;
else if (symbol.equals("true")) value = true;
else
{ String error = "boolean format: ";
throw new Exception(error + symbol);
}
return value;
}

public double readdouble() throws Exception
{ StringBuffer symbol = new StringBuffer();
//----------MODIFICACIO------------
readblanks();
if (ch == '+') readnext();
else if (ch == '-')
symbol.append(read());
while (digit()) symbol.append(read());
//---------------------------------
// hem substiuit el readint per aquest boci de codi.
if (ch == '.')
{ symbol.append(read());
while(digit()) symbol.append(read());
}
if ((ch == 'e') | (ch == 'E'))
{ symbol.append(read());
symbol.append(readint());
}
Double numeral =
new Double(symbol.toString());
return numeral.doubleValue();
}

public int readint() throws Exception
{ StringBuffer symbol = new StringBuffer();
readblanks();
if (ch == '+') readnext();
else if (ch == '-')
symbol.append(read());
while (digit()) symbol.append(read());
Integer numeral =
new Integer(symbol.toString());
return numeral.intValue();
}

public String readline() throws Exception
{ StringBuffer line = new StringBuffer();
getahead();
while (ch != nl)
{ line.append(ch); readnext(); };
ahead = false;
return line.toString();
}

public void readln() throws Exception
{ String skipped = readline(); }

public String readname() throws Exception
{ StringBuffer letters = new StringBuffer();
readblanks();
while (letter() | digit() | (ch == '_'))
{ letters.append(ch); readnext(); }
return letters.toString();
}

public void readnext() throws Exception
{ ch = (char)readkey(); ahead = true; }

public String readword() throws Exception
{ StringBuffer word = new StringBuffer();
readblanks();
while (!blank())
{ word.append(ch); readnext(); }
return word.toString();
}


// Instruccions per llegir vector i matrius d'enters i reals

public int[] read_int_array () throws Exception {
int v[] = new int[100];
int f[];
int i=0;
readblanks();
while (next() != ';') {
v[i] = readint();
readblanks();
i = i+1;
}
read();
f = new int[i];
for (i=0; i < f.length; i++) f[i] = v[i];
return f;
}

public void write (int[] v) throws Exception {
for (int i=0; i < v.length; i++) write (" " + v[i]);
}

public void writeln (int[] v) throws Exception {
write (v); writeln();
}

public double[] read_double_array () throws Exception {
double v[] = new double[100];
double f[];
int i=0;
readblanks();
while (next() != ';') {
v[i] = readdouble();
readblanks();
i = i+1;
}
read();
f = new double[i];
for (i=0; i < f.length; i++) f[i] = v[i];
return f;
}

public void write (double[] v) throws Exception {
for (int i=0; i < v.length; i++) write (" " + v[i]);
}

public void writeln (double[] v) throws Exception {
write (v); writeln();
}

public int[][] read_int_matrix () throws Exception {
int v[][] = new int[100][100];
int f[][];
int i=0;
readblanks();
while (next() != ';') {
v[i] = read_int_array();
readblanks();
i = i+1;
}
read();
f = new int[i][];
for (i=0; i < f.length; i++) {
f[i] = new int[v[i].length];
for (int j=0; j < v[i].length; j++) f[i][j] = v[i][j];
}
return f;
}

public void write (int[][] m) throws Exception {
for (int i=0; i < m.length; i++) {
write (m[i]);
write (";");
}
}

public void writeln (int[][] m) throws Exception {
for (int i=0; i < m.length; i++) {
write (m[i]);
writeln (";");
}
}

public double[][] read_double_matrix () throws Exception {
double v[][] = new double[100][100];
double f[][];
int i=0;
readblanks();
while (next() != ';') {
v[i] = read_double_array();
readblanks();
i = i+1;
}
read();
f = new double[i][];
for (i=0; i < f.length; i++) {
f[i] = new double[v[i].length];
for (int j=0; j < v[i].length; j++) f[i][j] = v[i][j];
}
return f;
}

public void write (double[][] m) throws Exception {
for (int i=0; i < m.length; i++) {
write (m[i]);
write (";");
}
}

public void writeln (double[][] m) throws Exception {
for (int i=0; i < m.length; i++) {
write (m[i]);
writeln (";");
}
}
}

/********* THE LAST LINE OF THE JAVA TEXT PROGRAM *********/