String nombre;
int edad;
BufferedReader cadena=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Ingrese el nombre");
try { nombre=cadena.readLine();
System.out.print("Ingrese la edad de "+nombre);
edad=Integer.parseInt(cadena.readLine());
System.out.println(nombre+"---------"+edad); }
catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
Ejemplo 1:Aplicación Java para leer un número desde consola y verificar si es par o impar
import java.io.*;
public class ejemplo1 {
public static void main(String[] args){
try {
int num;
BufferedReader cadena =new BufferedReader( new InputStreamReader(System.in));
num=Integer.parseInt(cadena.readLine());
if(num%2==0)
System.out.println("Este numero es par");
else
System.out.println("Este numero es impar");
} catch (IOException e) {}
}
}
Ejemplo 2:Escribir una aplicación Java para leer 10 números desde consola. Debe indicar el orden del número leído por ejemplo: "1.-" para leer le primero, "2.-" para leer el segundo, etc.
Finalmente calcular la suma, el promedio, el número mayor y menor
import java.io.*;
public class ejemplo1 {
public static void main(String[] args){
try {
int num;
BufferedReader cadena =new BufferedReader( new InputStreamReader(System.in));
System.out.print("1.-");
num=Integer.parseInt(cadena.readLine());
int mayor=num,menor=num,s=0;
for(int i=2;i<=10;i++){ System.out.print(i+".-");
num=Integer.parseInt(cadena.readLine());
s=s+num;//Acumulador para sumar
if(num>mayor)
mayor=num;
if(num menor)
menor=num;
}
System.out.println("La suma es: "+s);
System.out.println("El promedio es: "+s/10);
System.out.println("El mayor es: "+mayor);
System.out.println("El menor es: "+menor);
} catch (IOException e) { }
}
}
viernes, 7 de diciembre de 2007
jueves, 28 de junio de 2007
Leyendo texto desde un archivo en JAVA
try {BufferedReader in = new BufferedReader(new FileReader("infilename"));
String str;
while ((str = in.readLine()) != null) {
// Procesar la variable str;
}
in.close();
} catch (IOException e) { }
/* Ejercicios
1.- Leer 10 números guardados en un archivo externo numeros.txt.
Luego calcular:
- La suma de dichos números
- El Promedio
- El número mayor
- El número menor
- Cantidad de números pares
- Cantidad de números impares
2.- Leer 5 nombres de alumno de un archivo nombres.txt y otro de un archivo anio.txt los años en que nacieron. Mostrar la lista de los alumno y su correspondiente edad.
String str;
while ((str = in.readLine()) != null) {
// Procesar la variable str;
}
in.close();
} catch (IOException e) { }
/* Ejercicios
1.- Leer 10 números guardados en un archivo externo numeros.txt.
Luego calcular:
- La suma de dichos números
- El Promedio
- El número mayor
- El número menor
- Cantidad de números pares
- Cantidad de números impares
2.- Leer 5 nombres de alumno de un archivo nombres.txt y otro de un archivo anio.txt los años en que nacieron. Mostrar la lista de los alumno y su correspondiente edad.
Agregando a un archivo externo
try {
BufferedWriter out = new BufferedWriter(new FileWriter("nombrearchivo", true));
out.write("Leguia Loayza Elio");
out.close();
} catch (IOException e) {
}
BufferedWriter out = new BufferedWriter(new FileWriter("nombrearchivo", true));
out.write("Leguia Loayza Elio");
out.close();
} catch (IOException e) {
}
Escribiendo en una archivo externo en JAVA
// Si no existe el archivo escribir.txt se crea
try{
BufferedWriter salida=new BufferedWriter(new FileWriter("Escribir.txt"));
salida.write("Leguia Loayza Elio");
salida.close();
}
catch(IOException e){}
try{
BufferedWriter salida=new BufferedWriter(new FileWriter("Escribir.txt"));
salida.write("Leguia Loayza Elio");
salida.close();
}
catch(IOException e){}
miércoles, 23 de mayo de 2007
Ventana de validacion
import java.awt.Insets;
import javax.swing.*;
public class ejemplo1 extends JFrame{
JButton aceptar,cancelar;
JPasswordField pas;
JTextField usu;
JLabel lbUsuario,lbPasword;
public ejemplo1() {
setSize(320, 240);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Validacion");
setIconImage(new ImageIcon("img/Key.gif").getImage());
getContentPane().setLayout(null);
lbUsuario=new JLabel("Usuario");
lbUsuario.setBounds(50, 60, 80, 20);
getContentPane().add(lbUsuario);
lbPasword=new JLabel("Password");
lbPasword.setBounds(50,100,80,20);
getContentPane().add(lbPasword);
pas=new JPasswordField();
pas.setBounds(140, 100, 100, 20);
getContentPane().add(pas);
usu=new JTextField();
usu.setBounds(140,60,100,20);
getContentPane().add(usu);
aceptar=new JButton("Aceptar",new ImageIcon("img/Redo.gif"));
aceptar.setMargin(new Insets(-10,-10,-10,-10));
aceptar.setBounds(40,150,90,24);
getContentPane().add(aceptar);
cancelar=new JButton("Cancelar",new ImageIcon("img/Delete.gif"));
cancelar.setMargin(new Insets(-10,-10,-10,-10));
cancelar.setBounds(180, 150, 90, 24);
getContentPane().add(cancelar);
}
public static void main(String[] args) {
new ejemplo1().setVisible(true);
}
}

import javax.swing.*;
public class ejemplo1 extends JFrame{
JButton aceptar,cancelar;
JPasswordField pas;
JTextField usu;
JLabel lbUsuario,lbPasword;
public ejemplo1() {
setSize(320, 240);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Validacion");
setIconImage(new ImageIcon("img/Key.gif").getImage());
getContentPane().setLayout(null);
lbUsuario=new JLabel("Usuario");
lbUsuario.setBounds(50, 60, 80, 20);
getContentPane().add(lbUsuario);
lbPasword=new JLabel("Password");
lbPasword.setBounds(50,100,80,20);
getContentPane().add(lbPasword);
pas=new JPasswordField();
pas.setBounds(140, 100, 100, 20);
getContentPane().add(pas);
usu=new JTextField();
usu.setBounds(140,60,100,20);
getContentPane().add(usu);
aceptar=new JButton("Aceptar",new ImageIcon("img/Redo.gif"));
aceptar.setMargin(new Insets(-10,-10,-10,-10));
aceptar.setBounds(40,150,90,24);
getContentPane().add(aceptar);
cancelar=new JButton("Cancelar",new ImageIcon("img/Delete.gif"));
cancelar.setMargin(new Insets(-10,-10,-10,-10));
cancelar.setBounds(180, 150, 90, 24);
getContentPane().add(cancelar);
}
public static void main(String[] args) {
new ejemplo1().setVisible(true);
}
}

miércoles, 9 de mayo de 2007
SWING con Timer

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ejemplo extends JFrame implements ActionListener{
Timer timer;
JLabel avion;
JButton abajo;
int x=1,y=10;
public ejemplo(){
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
avion=new JLabel(new ImageIcon("avion.gif"));
avion.setBounds(0, 10, 200, 80);
getContentPane().add(avion);
timer=new Timer(1,this);
timer.start();
abajo=new JButton("abajo");
abajo.setBounds(200,500,100,40);
getContentPane().add(abajo);
abajo.addActionListener(this);
}
public static void main(String[] args) {
new ejemplo().setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==timer){
x=x+1;
avion.setLocation(x, y);
}
if(e.getSource()==abajo){
y=y+1;
avion.setLocation(x, y);
}
}
}
import java.awt.event.ActionListener;
import javax.swing.*;
public class ejemplo extends JFrame implements ActionListener{
Timer timer;
JLabel avion;
JButton abajo;
int x=1,y=10;
public ejemplo(){
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
avion=new JLabel(new ImageIcon("avion.gif"));
avion.setBounds(0, 10, 200, 80);
getContentPane().add(avion);
timer=new Timer(1,this);
timer.start();
abajo=new JButton("abajo");
abajo.setBounds(200,500,100,40);
getContentPane().add(abajo);
abajo.addActionListener(this);
}
public static void main(String[] args) {
new ejemplo().setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==timer){
x=x+1;
avion.setLocation(x, y);
}
if(e.getSource()==abajo){
y=y+1;
avion.setLocation(x, y);
}
}
}
Ventana Básica SWING
import javax.swing.*;
public class ventana extends JFrame{
public ventana(){
setSize(400, 300);
setTitle("mi Ventana");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new ventana().setVisible(true);
}
}
public class ventana extends JFrame{
public ventana(){
setSize(400, 300);
setTitle("mi Ventana");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new ventana().setVisible(true);
}
}

Suscribirse a:
Entradas (Atom)