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
