Advertisement
vinedfs

EscritorDeEndereços.java

Jul 23rd, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package br.com.geracao.main;
  2.  
  3. import java.io.File;
  4. import java.io.PrintWriter;
  5. import java.util.ArrayList;
  6.  
  7. import javax.swing.JOptionPane;
  8.  
  9. public class EscritorDeEndereços {
  10.  
  11.     public static void main(String[] args) {
  12.        
  13.         ArrayList<String> listaDeEndereços = new ArrayList<String>();
  14.        
  15.         String resposta = JOptionPane.showInputDialog("Quantas pessoas você deseja cadastrar?");
  16.         int quantidade = Integer.parseInt(resposta);
  17.        
  18.         for (int i = 0; i < quantidade; i++) {
  19.             String nome = JOptionPane.showInputDialog("Qual é o " + (i + 1) + "º nome?");
  20.             String idade = JOptionPane.showInputDialog("Qual é a idade " + (i + 1) + "º nome?");
  21.             String cidade = JOptionPane.showInputDialog("Qual é a cidade " + (i + 1) + "º nome?");
  22.             String estado = JOptionPane.showInputDialog("Qual é o estado " + (i + 1) + "º nome?");
  23.             listaDeEndereços.add(nome + "#" + idade + "#" + cidade + "#" + estado);
  24.         }
  25.        
  26.         JOptionPane.showMessageDialog(null, "Foram cadastrados " + listaDeEndereços.size() + " nome(s).");
  27.        
  28.         escreverNoArquivo(listaDeEndereços);
  29.        
  30.         JOptionPane.showMessageDialog(null, "Programa finalizado.");
  31.     }
  32.  
  33.     private static void escreverNoArquivo(ArrayList<String> listaDeEndereços) {
  34.         File file = new File("C:\\arquivo.txt");
  35.         try {
  36.             PrintWriter pw = new PrintWriter(file);
  37.             for (int i = 0; i < listaDeEndereços.size(); i++) {
  38.                 String endereço = listaDeEndereços.get(i);
  39.                 pw.println(endereço);
  40.             }
  41.             pw.close();
  42.             JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso!");
  43.         } catch (Exception e) {
  44.             JOptionPane.showMessageDialog(null, "Erro ao escrever no arquivo (" + e.getLocalizedMessage() + ").");
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement