como leer datos?
soy neofito y tengo el sig codigo
import java.io.*;
public class opera
{
public static void main(String arg[])
{
int x,y;
System.out.println(\\\"x= \\\");
x=System.in.read ();
System.out.println(\\\"y= \\\");
y=System.in.read ();
System.out.println(\\\"Suma=\\\"+x+y);
}
}
pero me marca este error:
D:\\\\practicasjava>javac opera.java
opera.java:10: unreported exception java.io.IOException; must be caught or decla
red to be thrown
x=System.in.read ();
^
opera.java:12: unreported exception java.io.IOException; must be caught or decla
red to be thrown
y=System.in.read ();
^
2 errors
import java.io.*;
public class opera
{
public static void main(String arg[])
{
int x,y;
System.out.println(\\\"x= \\\");
x=System.in.read ();
System.out.println(\\\"y= \\\");
y=System.in.read ();
System.out.println(\\\"Suma=\\\"+x+y);
}
}
pero me marca este error:
D:\\\\practicasjava>javac opera.java
opera.java:10: unreported exception java.io.IOException; must be caught or decla
red to be thrown
x=System.in.read ();
^
opera.java:12: unreported exception java.io.IOException; must be caught or decla
red to be thrown
y=System.in.read ();
^
2 errors
Una forma de leer datos seria asi:
import java.io.*;
public class opera
{
public static void main(String arg[])throws IOException
{
public static void main(String arg[])throws IOException
{
int x,y;
System.out.println("x=");
x=System.in.read ();
System.out.println("y= ");
y=System.in.read ();
System.out.println("Suma="+x+y);
}
}
}
o de la siguiente manera
import java.io.*;
public class opera
{
public static void main(String arg[])throws IOException
{
public static void main(String arg[])throws IOException
{
BufferedReader buf= new BufferedReader(new InputStreamReader(System.in));
int x,y;
System.out.println("x=");
x=Integer.parseInt(b.readLine());
System.out.println("y= ");
y=Integer.parseInt(b.readLine());
System.out.println("Suma="+x+y);
}
}
}
import java.io.*;
public class opera
{
public static void main(String arg[])throws IOException
{
public static void main(String arg[])throws IOException
{
int x,y;
System.out.println("x=");
x=System.in.read ();
System.out.println("y= ");
y=System.in.read ();
System.out.println("Suma="+x+y);
}
}
}
o de la siguiente manera
import java.io.*;
public class opera
{
public static void main(String arg[])throws IOException
{
public static void main(String arg[])throws IOException
{
BufferedReader buf= new BufferedReader(new InputStreamReader(System.in));
int x,y;
System.out.println("x=");
x=Integer.parseInt(b.readLine());
System.out.println("y= ");
y=Integer.parseInt(b.readLine());
System.out.println("Suma="+x+y);
}
}
}
Hola:
La excepción salta porque al leer (System.in.read()) es posible que haya algún error y debemos capturarlo. Debes meterlo en un try-catch
try
{
System.in.read();
}
catch (IOException e)
{
// No se puede leer
}
De todas formas, System.in sólo lee bytes. Si en el teclado escribes una A, te devolverá un 65, que es su código ascii. Para poder leer bien, debes usar el mecanismo que te comentan en la otra contestación. En http://chuidiang.blogspot.com/2005/09/entrada-standard-en-java.html tienes una explicación un poco más detallada sobre el tema.
Se bueno
La excepción salta porque al leer (System.in.read()) es posible que haya algún error y debemos capturarlo. Debes meterlo en un try-catch
try
{
System.in.read();
}
catch (IOException e)
{
// No se puede leer
}
De todas formas, System.in sólo lee bytes. Si en el teclado escribes una A, te devolverá un 65, que es su código ascii. Para poder leer bien, debes usar el mecanismo que te comentan en la otra contestación. En http://chuidiang.blogspot.com/2005/09/entrada-standard-en-java.html tienes una explicación un poco más detallada sobre el tema.
Se bueno
