Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Output to file

$
0
0
I have a gui, it gets input from the user, the input will be name, age, email, and number. I need this data to then be written to a file. I have read my book and it refers to a scanner from keyboard. The assignment must be done using a gui so the scanner is completely out of the question! I have created all the input variables and used a JOptionPane to get the information but now im lost on how to get these varaibles printed to a file.

Heres the starting of my code (as you can see i have soooo much more to do) but of course 1 step at a time for me!

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contactinformationprogram;

import javax.swing.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author T-Ali
 */
public class ContactInformationProgram {

    /**
     * Part 1
     * Design, implement, test, and debug a program with a JFrame that allows 
     * the user to enter a series of contacts’ names, ages, e-mail addresses,
     * and cell phone numbers, and creates a file from the entered data.
     * Validate the age entry to ensure that it is numeric and between 0 and 
     * 120. Include information for three to five contacts.
     * Part 2   --pg 831 of chapter 12
     * Design, implement, test, and debug a program that reads the file you 
     * created by the list in Part 1 and displays the records in a JFrame. You
     * may either display all entries in the list at once or display them one
     * at a time; the user interface is up to you. Protect against not being
     * able to open the file.

     */
    

     
     
        public static void main(String[] args) {
                 //jframe
            final int width = 1400,
                    height = 900;
            
            JFrame window = new JFrame ("Contact Information Program");
            
            //set size
            window.setSize(width, height);
            
            //specify what happens when x is clicked
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //display the window
            window.setVisible(true);
            
        // TODO code application logic here
            //variables
      String name;
      String phone ="";

      String strInput;
      int repeat;       
      int age;  
      String email ="";
      


            
          
    do {
   

        // name input dialog
         name = JOptionPane.showInputDialog(null, "Enter name of  person."
                 + "");
        
        
        //panel2 age  input dialog
         strInput = JOptionPane.showInputDialog(null, "Enter the persons "
                 + "age.");
         age = Integer.parseInt(strInput);
        
         //validate age entry between 0-120  input dialog
        if (age < 0){
         if (age > 120){
             JOptionPane.showMessageDialog(null, "You have entered a value that"
                     + " is incorrect. Please renter persons age between 0-120."
                     + "");
         }
         }
        else {
                
        // email address  input dialog
        email = JOptionPane.showInputDialog(null, "Enter Email address.");    
        
        // phone numbers  input dialog
        phone = JOptionPane.showInputDialog(null, "Enter Phone Number.");
        
        
        }
        
      
         //did the user need another person added?
              repeat = JOptionPane.showConfirmDialog(null, "Would you like to"
                 + " enter another person's data?", "Please Confirm.", 
                 JOptionPane.YES_NO_OPTION);
              
                  //create file from entered data
             PrintWriter outputFile;
                try {
                    outputFile = new PrintWriter("ContactData.txt");
               
outputFile.println(name);
outputFile.println(age);
outputFile.println(email);
outputFile.println(phone);
outputFile.close();
 } catch (FileNotFoundException ex) {
                    Logger.getLogger(ContactInformationProgram.class.getName()).log(Level.SEVERE, null, ex);
                }
    }
    while (repeat==JOptionPane.YES_OPTION);
        }
    

    
                

   
  
        
    
}
    

        
       
        
       



To make things easier on myself I have included the assignment details within the code (see part 1 for the output to file). Last week i got lost in the requirements so i figured it best to keep the requirements within the code for ease for me!

What am i missing?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>