Buenas tengo un problema estoy comenzando a programar en java y me dejaron un proyecto que consiste en crear una matriz y guardarla en un archivo, el progama debe tener un menu: 1. Llenar Matriz 2. Crear Archivo de texto ya eso esta hecho el problema que tengo es que al correr el programa digito primero el 2 siempre crea el archivo y me piden q debo controlar q antes de crear el archivo de texto la matriz este cargada de lo contrario informar q primero debe crear la matriz antes de crear el archivo de texto.
Les agradeceria mucho su ayuda Gracias...!
que llevas echo ? ---Regla B. Se pregunta por conceptos abstractos. Aquí no estamos para hacerle el trabajo a nadie
SL2.
Esto es lo q tengo hecho
/**
* AWT Sample application
*
* @author
* @version 1.00 09/08/08
*/
import java.io.*;
public class pruebaProyecto7
{
public static void main (String[] args) throws IOException
{
BufferedReader dis;
dis = new BufferedReader(new InputStreamReader(System.in));
String Entrada;
int opcion = 0;
int tam = 0;
int mat[][];
mat=new int[tam][tam];
while(true)
{
muestraMenu();
Entrada = new String(dis.readLine());
opcion = Integer.parseInt(Entrada);
//opcion = Entrada;
//si se digita 5 sale del ciclo
if(opcion == 4)
{
System.out.println ("");
System.out.print ("Saliendo de la aplicacion ...");
break;
}
if(opcion > 4)
{
System.out.println ("");
System.out.print ("La opcion que ud eligio esta fuera del rango del menu...");
System.out.println ("");
//muestraMenu();
}
//evalua la opcion seleccionada
switch (opcion)
{
case 1: ingreso();
break;
case 2: CreaArchivo(mat, tam);
boolean existe;
if (existe = false)
{
ingreso();
System.out.print ("Primero debe llenar la matriz");
}
if (existe = true)
{
System.out.print ("Archivo guardado con el nombre proyecto.txt en C");
CreaArchivo(mat, tam);
}
}
}
}
public static void muestraMenu()
{
System.out.println ("");
System.out.println ("**********************************");
System.out.println ("");
System.out.println ("***************MENU***************");
System.out.println ("");
System.out.println (" ** ESCOJA UNA OPCION ** ");
System.out.println ("");
System.out.println(" 1. LLENAR MATRIZ");
System.out.println(" 2. CREAR ARCHIVO DE TEXTO");
System.out.println(" 3. LEER ARCHIVO DE TEXTO");
System.out.println(" 4. SALIR");
System.out.println ("");
System.out.println ("**********************************");
System.out.println ("");
}
//********************************************
public static void ingreso ()throws IOException
{
//*******************************
BufferedReader dis;
dis = new BufferedReader(new InputStreamReader(System.in));
String ent; //variables string
int mat[][];
int i=0,j=0,tam=0,n=1,pos=0,max=0,multiplo=0,cont=1; //variables enteras
System.out.println ("***********************************************************");
System.out.println ("**Digite el tamano de la matriz que sea impar y mayor a 3**");
System.out.println ("***********************************************************");
ent=new String(dis.readLine());
tam=Integer.parseInt(ent);
mat=new int[tam][tam];
max=tam*tam; //valor maximo de la matriz o tamaño maximo
if (tam>=3) // evalua si la matriz es impar y mayor a 3 para continuar
{
pos=tam/2; // identifica la posicion inicial y central del vector
j=pos;
}
while (n<=max) // hace el proceso mientras el numero insertado en la matriz sea menor o igual al tamaño de la matriz
{
mat[j]=n;
n++;
i=i-1;
j=j+1;
if (i<0)
{
i=tam-1;
}
if (j>=tam)
{
j=0;
}
multiplo=tam*cont; //aqui se sacan los multiplos del tamaño de la matriz
if (multiplo<max && n==multiplo) //se evaluan esos multiplos
{
mat[j]=n;
i++;
n++;
mat[j]=n;
cont++; //responsable de crear los multiplos de la matriz
}
}
impresion(mat,tam,max,n); //imprime matriz visualización previa
}
/****************************************************************************************************************/
public static void CreaArchivo(int mat[][], int tam)
{
try {
//ubicacion y nombre del archivo
String nombre = "C:/Proyecto1.txt";
FileWriter fw = new FileWriter(nombre);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter salida = new PrintWriter(bw);
bw.newLine();
int i = 0;
int j = 0;
salida.println("ENCABEZADO DEL ARCHIVO");
salida.println("ENCABEZADO1");
salida.println(" ");
while (i < tam)
{
while (j < tam)
{
salida.print("* "+mat[j]);
j++;
}
salida.println(" ");
j=0;
i++;
}
salida.close();
}
catch(java.io.IOException ioex) {
System.out.println("se presento el error: "+ioex.toString());
}
}
/********************************************************************************************/
public static void impresion(int mat[][],int tam,int max,int n)throws IOException
{
int i,j;
System.out.println ("----------Impresion de la matriz-----------------");
for (i=0; i<tam;i++)
{
for (j=0; j<tam; j++)
{
if (mat[j]<=max)
System.out.print (" "+mat[j]);
else if (mat[j]>max)
System.out.print (" "+mat[j]);
}
System.out.println ();
}
}
/****************************************************************************************************************/
}
si llevas hecho mas de la mitad y dices que te falta controlar si se creo el archivo o no.. es muy simple solo coloca un boolean "b" initcializado a falso que verifique que la matriz este creada entonces si al momento de crear el arhivo el boolean "b"esta en true entonces se procede si no muestra un mensaje y lo redirrecciona a la creacion del Archivo.. :)
SL2 y suerte.
si voy a hacer eso q escribistes muchas gracias x la ayuda y feliz navida ;-)