Solved

Help with programming project, writing to a txt file

Posted on 2008-06-20
4
212 Views
Last Modified: 2010-04-21
With some previous help I have almost finished this programming project. I am at the final part of writing txt to a file from an array. I can get it to write once but for some reason it won't write all iterations of the text. It writes the first name and quits. I have a loop but it doesn't seem to make a difference. Can someone tell me where I am screwing up? I am getting pretty frustrated with Java. This is the last assignment for the class and I feel I should be seeing these mistakes myself...

It's the very last part from "create a file" I assume is wrong but I included the rest in case I screwed it up somewhere else.

Thanks in advance
public class TestName {

   

       public static void main(String[] args) throws Exception {

      // Create a File instance

         java.io.File file = new java.io.File("names.txt");

      

         java.util.Scanner input = new java.util.Scanner(file);

      

         Name[] nameArray = new Name[10];

      

         //System.out.println(input.delimiter());

      

      // Read data from a file

         int index = 0;

         while (input.hasNext()) {

            String first = input.next();

            String middle = input.next();

            String last = input.next();

         

            Name name = new Name(first, middle, last);

         

            nameArray[index++] = name;

         }

      

         printNameArray(nameArray);

         printOutPut(nameArray);

      

      // Close the file

         input.close();

      }

   

       public static void printNameArray(Name[] nameArray) {

         Name name;

         String first;

         String middle;

         String last;

         for (int i = 0; i < nameArray.length; i++) {

            name = nameArray[i];

            first = name.getFirst();

            middle = name.getMiddle();

            last = name.getLast();

         

            System.out.println(nameArray[i].getFirst() + " " + nameArray[i].getMiddle() +  " " + nameArray[i].getLast() + " " +  nameArray[i].initials());

         

         

         

         

         

         

         }

      }

       public static void printOutPut(Name[] nameArray) throws Exception {

         java.io.File outfile = new java.io.File("output.txt");

         if (outfile.exists()) {

            System.out.println("File already exists");

            System.exit(0);

         }

         

         // Create a file

         java.io.PrintWriter output = new java.io.PrintWriter(outfile);

         Name name;

         String first;

         String middle;

         String last;

         for (int i = 0; i < nameArray.length; i++) {

            name = nameArray[i];

            first = name.getFirst();

            middle = name.getMiddle();

            last = name.getLast();

            

            output.print(nameArray[i].getLast() + ", " + nameArray[i].getFirst() + " " + nameArray[i].getMiddle() + "\n");

                       

                    

            // Close the file

            output.close();            

            //System.out.println(nameArray[i].getFirst() + " " + nameArray[i].getMiddle() +  " " + nameArray[i].getLast() + " " +  nameArray[i].initials());

            

            

            

            

            

            

         }

                 

      }

   }

Open in new window

0
Comment
Question by:Hueydude
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21831011
You close 'output' in the loop. Should  close after it
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 21831032
i.e. change

                for (int i = 0; i < nameArray.length; i++) {
                        name = nameArray[i];
                        first = name.getFirst();
                        middle = name.getMiddle();
                        last = name.getLast();

                        output.print(nameArray[i].getLast() + ", " +
                                nameArray[i].getFirst() + " " + nameArray[i].getMiddle() + "n");

                        // Close the file
                        output.close();

                }

to
                for (int i = 0; i < nameArray.length; i++) {

                        name = nameArray[i];

                        first = name.getFirst();

                        middle = name.getMiddle();

                        last = name.getLast();
 

                        output.print(nameArray[i].getLast() + ", " +

                                nameArray[i].getFirst() + " " + nameArray[i].getMiddle() + "n");
 

                } 
 

                // Close the file

                output.close();

Open in new window

0
 

Author Closing Comment

by:Hueydude
ID: 31469140
See, that's what ticks me off about this class. I should have been able to figure that out on my own!!

Thanks for the help!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21831340
:-) You'll get to know which dogs can bite in time ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 83
Problem to start Neon 20 87
expectj telnet failing 5 26
eclipse apache tomcat admin console 52 66
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now