Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: My program reads a file and must be read into 3 arrays, I've done that, but,

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My program reads a file and must be read into 3 arrays, I've done that, but,

    I've read the text file into 3 different arrays because the file data has to be separated according to the exercise stipulations. I'm now having an issue with retrieving it and formatting it into a table. I'm not sure where I'm making the mistakes.


    /* muTunes program processes a sampling of my music collection in
    * my library. The program reads the data into three arrays
    * (2 Strings for title and artist, and 1 integer for song length)
    * from my text file. The songs will be output in a formatted table,
    * table, indicating total of all songs, the longest song, the
    * shortest song, and the song titles in alphabetical order.
    */
    import java.io.*;
    import java.util.*;

    public class muTunes {
    // Read file and call methods to proceed
    public static void main(String[] args) throws FileNotFoundException {
    Scanner fileScanner = new Scanner(new File("Music.txt"));
    readSongs(fileScanner);
    //printTable();
    //findSongs();
    //alphaSongTitles();

    }

    public static void readSongs(Scanner fileScanner) {
    int numOfSongs = fileScanner.nextInt();
    fileScanner.nextLine();
    while (fileScanner.hasNextLine()) {
    String songLine = fileScanner.nextLine();
    String delims = "[:]";
    int[] times = new int[10];
    String[] titles = new String[10];
    String[] artists = new String[10];
    String[] tokens = songLine.split(delims);
    for (int i = 0; i < tokens.length - 2; i++) {
    String songTimes = tokens[0];
    // System.out.print(tokens[i]+" "); //testing tokens
    // System.out.print(songTimes + " "); //testing songTimes
    times[i] = Integer.parseInt(songTimes);
    //System.out.print(times[i] + " "); // test contents of array
    }
    for (int i = 1; i < tokens.length - 1; i++) {
    String songTitles = tokens[1];
    titles[i] = songTitles;
    //System.out.print(titles[i] + " "); // test contents of array
    }
    for (int i = 2; i < tokens.length; i++) {
    String songArtists = tokens[2];
    artists[i] = songArtists;
    //System.out.print(artists[i] + " "); // test contents of array
    }
    int totalTime = 0;
    for(int songSelect = 1; songSelect <= numOfSongs; songSelect++){
    totalTime += times[i];
    }
    printTable(titles, artists, times);

    }
    }

    public static void printTable(String[] titles, String[] artists, int[] times){
    System.out.printf("%-5s%,24s%,24s\n%-5s%,24s%,24s\n", "TITLE", "ARTIST", "TIME", "-----", "------", "----");
    System.out.printf("%-5s%,24s%,24s\n", titles, artists, times);
    System.out.println();
    System.out.println("%-10s%48d\n", "TOTAL TIME", totalTime);
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: My program reads a file and must be read into 3 arrays, I've done that, but,

    Please use the highlight tags when posting code. Also, code should be in the form of an SSCCE that demonstrates a single piece- in your case, outputting arrays as a table.

    What is the trouble? What does your code do that you don't want it to be doing? What does it not do that you do want it to do? what does it do instead?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Creating a program that reads from a file
    By Twoacross in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: November 20th, 2011, 10:19 PM
  2. Static class that reads from file -> performance issues?
    By rbk in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: April 18th, 2011, 09:59 AM
  3. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM
  4. [SOLVED] FileReader.read() reads garbage
    By Hackworth in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 27th, 2010, 01:06 PM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM