Results 1 to 7 of 7

Thread: printing array

  1. #1
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default printing array

    how would you go about printing the contents of an array from a file one line at a time?

    I want it to print like this:

    Miami, Florida
    Daytona Beach, Florida
    San Francisco, California
    etc....

    Could anyone help me with this? thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    array from a file? Do you mean that you read the file into an array (or possibly better, a collection) of String?

    If so, wouldn't a for-loop seem like it would work well here?

  3. #3
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    Would you have an example of that?

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default A lot of questions...

    Which of the following:
    • Print contents from a File?
    • Print contents from an array?
    • Pass contents from a file to an array and then print it?
    • Pass contents form an array to a file and then print it?
    • None of the above

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    i have a file("employee.txt") that has a list of employees and all i want to is print the list of employees and im not sure how to go about doing that.

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    google search "java read from file" + i'm feeling lucky took me here:

    Java Practices -> Reading and writing text files

  7. #7
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Here's something I found on how to read a file as a String. Works great for me:
    Java Code:
    private String readFileAsString(String filePath) throws IOException {
         StringBuilder fileData = new StringBuilder(1000);
         BufferedReader reader = new BufferedReader(
              new FileReader(filePath));
         char[] buf = new char[1024];
         int numRead = 0;
         while((numRead=reader.read(buf)) != -1){
              fileData.append(buf, 0, numRead);
         }
         fileData.trimToSize();
         reader.close();
         return fileData.toString();
    }
    That reads the entire file as a String.. Not each word as a String in an Array..
    to get each word in array, you could do this:
    Java Code:
    ClassName cs = new ClassName();
    String sample = cs.readFileAsString(sample.txt);
    String[] sampleArray = sample.split(" ");
    I think that would work. I'm not the greatest at regular expressions, someone correct my split() if I'm wrong. Then use the following syntax to print the array:
    Java Code:
    for(int i = 0; i < sampleArray's length; i increments) {
         print out sampleArray[i];
    }
    Hope that helped.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 03:40 PM
  2. Printing Byte Array
    By suchismitasuchi in forum New To Java
    Replies: 3
    Last Post: 01-19-2009, 10:58 AM
  3. Printing Out Every Other Letter
    By Agent in forum New To Java
    Replies: 4
    Last Post: 11-20-2008, 12:43 AM
  4. Replies: 21
    Last Post: 11-05-2008, 02:31 PM
  5. Printing Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •