Results 1 to 5 of 5
  1. #1
    nifa98 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Reading a .txt file into an array

    I am trying to read a text file consisting of 25 lines into an array. Each line consist of the name of a song, the title of a song, name of album and the length of the track. When I try to print out the array to confirm that I am indeed reading the entire text file into the array.
    It will only print out the first line repeatedly instead of filing the array with the remaining 24 lines .

    Here is the code I have thus far:


    import java.io.*;
    import java.util.*;


    public class SongListReader {
    static Scanner in = new Scanner(System.in);
    public static void main(String[] args) {

    int menuChoice=0;
    Songs songsObject = new Songs();
    String inputFile = "songList.txt";
    try{
    FileReader reader = new FileReader(inputFile);
    Scanner read = new Scanner(reader);
    String line = read.nextLine();
    //Read the lines of the .txt file into an array
    String[] arrSongs;
    arrSongs = new String[50];
    for (int i = 0; i < arrSongs.length; i++){
    arrSongs[i] = line;
    System.out.println(arrSongs[i]); /*only printed out 1st line from file*/
    }
    read.close();
    }catch(FileNotFoundException fnf){
    System.out.println("File not found");
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Reading a .txt file into an array

    Since you only ever read one line from the file I'm not surprised it doesn't do what you want.

  3. #3
    nifa98 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Reading a .txt file into an array

    I've struggled with arrays for some odd reason :-(

    Not sure if this makes any sense but I'm thinking I need to add a nested for loop to read the remaining lines....like this:

    String[][] arrSongs;
    arrSongs = new String[50][50];
    for (int i = 0; i < arrSongs.length; i++){
    for (int j =0; j< arrSongs.length; j++)
    }
    arrSongs[i][j] = line;
    System.out.println(arrSongs[i][j]);

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Reading a .txt file into an array

    No you don't need nested loops. You just need to read more than one line. HINT: when do you ever change the value of the line variable?

  5. #5
    nifa98 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Reading a .txt file into an array

    Thanks for your help! I made the changes below and was able to get the correct output:
    int menuChoice=0;
    Songs songsObject = new Songs();
    String inputFile = "songList.txt";
    try{
    FileReader reader = new FileReader(inputFile);
    Scanner read = new Scanner(reader);
    String line = read.nextLine();
    //Read the lines of the .txt file into an array
    String[] arrSongs;
    arrSongs = new String[24];
    for (int i = 0; i < arrSongs.length; i++){
    arrSongs[i] = read.nextLine();
    System.out.println(arrSongs[i]);
    }
    read.close();
    }catch(FileNotFoundException fnf){
    System.out.println("File not found");
    }

Similar Threads

  1. Reading a file into an array?
    By heycoa in forum New To Java
    Replies: 4
    Last Post: 07-07-2011, 05:22 AM
  2. Help with GUI, Array, and Reading File
    By bamagirl31 in forum New To Java
    Replies: 21
    Last Post: 07-05-2011, 02:14 AM
  3. Help with reading file into array
    By xkillswitchx14 in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 11:24 PM
  4. Reading csv file into 2D array - HELP!!!
    By mikeg in forum New To Java
    Replies: 17
    Last Post: 04-12-2011, 09:36 AM
  5. Help with reading from file into an array
    By Trad in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 01:16 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
  •