Java - imprimir mi método en block de notas

 
Vista:
sin imagen de perfil
Val: 4
Ha aumentado su posición en 8 puestos en Java (en relación al último mes)
Gráfica de Java

imprimir mi método en block de notas

Publicado por hans (3 intervenciones) el 16/07/2019 00:54:55
una ayuda como imprimo el resultado de mis metodos en block de notas???try catch
package ordenamiento;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class Ordenamiento {
    public static void muestra(int[]notasx, String[] nombresx){
        for(int i=0; i<notasx.length; i++){
            System.out.println(notasx[i]+ "\t" +nombresx[i]);
        }
    }
    public static void burbuja(int[]notasx,String[] nombresx){
        int i,j,aux_notas;
        String aux_nombres;
        for( i = 0; i<notasx.length-1; i++){
            for (j=i+1;j<notasx.length;j++){
                if(notasx[i] > notasx[j])
                {
                    aux_notas  = notasx[i];
                    notasx[i]  = notasx[j];
                    notasx[j]  = aux_notas;
 
                    aux_nombres = nombresx[i];
                    nombresx[i] = nombresx[j];
                    nombresx[j] = aux_nombres;
                }
            }
        }
    }
    public static void mayor_menor(int notasx[], String []nombresx){
 
            System.out.println("nota mayor:"+ notasx[0]+ "\t" +nombresx[0]);
            System.out.println("nota menor:"+ notasx[4]+ "\t" +nombresx[4]);
}
    public static void busqueda(int notasx[], String nombresx[]){
        for(int i=0; i<nombresx.length; i++){
            if(nombresx[i]=="Luis"){
 
            System.out.println("Alumno encontrado: "+notasx[i]+ "\t" +nombresx[i]);
            }
        }
    }
    public static void descen(int []notasx,String[] nombresx ){
        int i,j,aux_notas;
        String aux_nombres;
        for( i = 0; i<notasx.length-1; i++){
            for (j=i+1;j<notasx.length;j++){
                if(notasx[i] < notasx[j])
                {
                    aux_notas  = notasx[i];
                    notasx[i]  = notasx[j];
                    notasx[j]  = aux_notas;
                    aux_nombres = nombresx[i];
                    nombresx[i] = nombresx[j];
                    nombresx[j] = aux_nombres;
                }
            }
    }
        for(int x=0; x<notasx.length; x++){
            System.out.println(notasx[x]+ "\t" +nombresx[x]);
        }
    }
    public static double prom(int[] notasx){
        double acu=0,prom;
        for(int i=0; i<notasx.length; i++){
            acu=acu+notasx[i];
        }
        prom=(acu)/5;
        System.out.println("el promedio general: "+prom);
        return prom;
    }
    public static void main(String[] args)  {
        int []notas={15,18,16,13,17};
        String[] nombres={"Juan", "Pedro", "Luis","María", "Rosa"};
        System.out.println("--Arreglo Inicial --");
        muestra(notas,nombres);
        System.out.println("--Arreglo Ordenado ascendente--");
        muestra(notas,nombres);
        System.out.println("---Arreglo Ordenado descendente---");
        descen(notas,nombres);
        prom(notas);
        mayor_menor(notas,nombres);
        busqueda(notas,nombres);
        }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

imprimir mi método en block de notas

Publicado por Kabuto (1381 intervenciones) el 16/07/2019 13:35:01
Tienes que seguir 3 pasos.

1- Construir un String con los datos que quieres mostrar.
2- Con ese String, tienes que crear un archivo de texto .txt.
3- Una vez creado, utilizas la clase Desktop para pedir que abra ese archivo que has creado.
Desktop llamará al programa asociado a ese tipo de archivo. Al ser un .txt, si estas bajo Windows lo normal es que lo abra el notepad.

Te pongo un programa sencillo de ejemplo donde se siguen esos tres pasos.
La creación del fichero la hago en un método separado pero no es obligatorio hacerlo así.
Úsalo para adaptarlo a tu programa.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class AbrirTxt {
 
	public static void main(String[] args) {
 
		//String con texto que queremos mostrar
		String texto = "Lorem Ipsum Dolor";
 
		//Construimos fichero texto con el String a mostrar
		File fichero = crearFicheroTexto("FicheroPrueba", texto);
 
		//Ordenamos abrirlo con el programa que el S.O. tiene asociado al archivo creado.
		try {
			Desktop.getDesktop().open(fichero);
		} catch (IOException e) {
			e.printStackTrace();
		}
 
	}
 
	public static File crearFicheroTexto(String nombre, String texto) {
 
		File fichero = new File(nombre + ".txt"); //Crearemos un archivo. txt
 
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(fichero));
			bw.write(texto);
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
		return fichero;
	}
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

[Posible solucion]imprimir mi método en block de notas

Publicado por Billy Joel (876 intervenciones) el 16/07/2019 16:52:36
Se me ocurre que quieres algo como esto...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
 
public class Ordenamiento {
 
    public static void muestra(int[] notasx, String[] nombresx) {
        for (int i = 0; i < notasx.length; i++) {
            System.out.println(notasx[i] + "\t" + nombresx[i]);
            println(notasx[i] + "\t" + nombresx[i]);
        }
    }
 
    public static void burbuja(int[] notasx, String[] nombresx) {
        int i, j, aux_notas;
        String aux_nombres;
        for (i = 0; i < notasx.length - 1; i++) {
            for (j = i + 1; j < notasx.length; j++) {
                if (notasx[i] > notasx[j]) {
                    aux_notas = notasx[i];
                    notasx[i] = notasx[j];
                    notasx[j] = aux_notas;
                    aux_nombres = nombresx[i];
                    nombresx[i] = nombresx[j];
                    nombresx[j] = aux_nombres;
                }
            }
        }
    }
 
    public static void mayor_menor(int notasx[], String[] nombresx) {
        System.out.println("nota mayor:" + notasx[0] + "\t" + nombresx[0]);
        System.out.println("nota menor:" + notasx[4] + "\t" + nombresx[4]);
        println("nota mayor:" + notasx[0] + "\t" + nombresx[0]);
        println("nota menor:" + notasx[4] + "\t" + nombresx[4]);
    }
 
    public static void busqueda(int notasx[], String nombresx[]) {
        for (int i = 0; i < nombresx.length; i++) {
            if (nombresx[i] == "Luis") {
                System.out.println("Alumno encontrado: " + notasx[i] + "\t" + nombresx[i]);
                println("Alumno encontrado: " + notasx[i] + "\t" + nombresx[i]);
            }
        }
    }
 
    public static void descen(int[] notasx, String[] nombresx) {
        int i, j, aux_notas;
        String aux_nombres;
        for (i = 0; i < notasx.length - 1; i++) {
            for (j = i + 1; j < notasx.length; j++) {
                if (notasx[i] < notasx[j]) {
                    aux_notas = notasx[i];
                    notasx[i] = notasx[j];
                    notasx[j] = aux_notas;
                    aux_nombres = nombresx[i];
                    nombresx[i] = nombresx[j];
                    nombresx[j] = aux_nombres;
                }
            }
        }
        for (int x = 0; x < notasx.length; x++) {
            System.out.println(notasx[x] + "\t" + nombresx[x]);
            println(notasx[x] + "\t" + nombresx[x]);
        }
    }
 
    public static double prom(int[] notasx) {
        double acu = 0, prom;
        for (int i = 0; i < notasx.length; i++) {
            acu = acu + notasx[i];
        }
        prom = (acu) / 5;
        System.out.println("el promedio general: " + prom);
        println("el promedio general: " + prom);
        return prom;
    }
 
    public static void main(String[] args) {
        int[] notas = {15, 18, 16, 13, 17};
        String[] nombres = {"Juan", "Pedro", "Luis", "María", "Rosa"};
        System.out.println("--Arreglo Inicial --");
        println("--Arreglo Inicial --");
        muestra(notas, nombres);
        System.out.println("--Arreglo Ordenado ascendente--");
        println("--Arreglo Ordenado ascendente--");
        muestra(notas, nombres);
        System.out.println("---Arreglo Ordenado descendente---");
        println("---Arreglo Ordenado descendente---");
        descen(notas, nombres);
        prom(notas);
        mayor_menor(notas, nombres);
        busqueda(notas, nombres);
    }
 
    public static final String RUTA_ARCHIVO = "C:\\prueba.txt";
 
    public static void println(String lineMessage) {
        FileWriter fichero = null;
        PrintWriter pw = null;
        try {
            fichero = new FileWriter(RUTA_ARCHIVO, true);
            pw = new PrintWriter(fichero);
            pw.println(lineMessage);
        } catch (IOException e) {
            e.printStackTrace(System.out);
        } finally {
            try {
                if (fichero != null) {
                    fichero.close();
                }
                if (pw != null) {
                    pw.flush();
                    pw.close();
                }
            } catch (IOException e2) {
                e2.printStackTrace(System.out);
            }
        }
    }
}

Lo que hice fue un método que imprima tus salidas en consola en un archivo. La ruta del archivo la defino en la constante RUTA_ARCHIVO. Como podrás ver he modificado un poco tu código solo para añadir las impresiones al archivo.

Pruebalo y comentas!!

Saludos,
Billy Joel
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar