Gaga Rigs

Song stats

Song stats
DOWNLOAD
For ech listener in the input file there is:

listener’s name
song title
minutes seconds
song title
minutes seconds
song title
minutes seconds
and there can be any number of them.
Notice that there is a space between the minutes and seconds.

For each listener, state their average song length

After looping thorugh all the input and at the end state the average song length
Also state the longest and shorted song.

In this version, there can be duplicate song titles with the same lengths.

Time is now in minutes and seconds.

Do not use the sentinel method: just keep reading song stat records until there are no more.

Send the output (the “statings”) to a file that the user picks.


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

public class songFile {
	public static void main(String[] args) throws IOException {

		System.out.println("testing file I/O");
		Scanner kbd = new Scanner(System.in);
		System.out.print("Enter the name of the file to read from: ");
		String filenameFromUser = kbd.nextLine();

		File file = new File(filenameFromUser);
		while (!file.exists()) {
			System.out.println("Can't open " + filenameFromUser + "!");
			System.out.print("Try again: ");
			filenameFromUser = kbd.nextLine();
			file = new File(filenameFromUser);
		}

		Scanner infl = new Scanner(file);

		System.out.print("Enter the name of the file to write to: ");
		while (!file.exists()) {
			System.out.println("Can't open " + filenameFromUser + "!");
			System.out.print("Try again: ");
			filenameFromUser = kbd.nextLine();
			file = new File(filenameFromUser);
		}

		String filenameWriteFromUser = kbd.nextLine();

		File fileO = new File(filenameWriteFromUser);

		if (fileO.exists()) {
			System.out.println("can no do, file exist");
			System.exit(1);
		}

		System.out.println();

		System.out.println("Thank you. Lets make your file: "+filenameWriteFromUser);
		System.out.println();

		PrintWriter outfl = new PrintWriter(fileO);
		final double MIN_SEC = 60;

		String aLineFromTheFile;

		double songLengh;
		double songSum = 0;
		double songMin, songMax;
		int songAvgmin;
		int songAvgsec = 0;
		double songCounter = 0;
		double songMinutes = 0, songSeconds = 0;

		double longest = 0, shortest = 0;
		double longestAll = 0, shortestAll = 0;
		double songSum2 = 0;

		String userName;
		String songName = null;
		String longestName = null, shortestName = null;
		String longestNameAll = null, shortestNameAll = null;

		double tmp = 0;
		double songTotalSum = 0;
		double songTotalAvg;
		int songTotalAvgMin, songTotalAvgSec;
		boolean once = true;
		int counter = 0;

		while (infl.hasNextLine()) {
			// to get username
			userName = infl.nextLine();
			outfl.println(userName);

			for (int i = 0; i < 3; i++) {
				if (infl.hasNextLine()) {
					songName = infl.nextLine();
					int number = i+1;
					outfl.print(number+ ": " + songName);
				}

				if (infl.hasNextInt()) {
					songMinutes = infl.nextInt();
				}
				// outfl.println(songMinutes);
				if (infl.hasNextInt()) {
					songSeconds = infl.nextInt();
				}
				infl.nextLine();
				// type cast to int so zeros don't show
				outfl.println(" -- " + (int) songMinutes + ":"
						+ (int) songSeconds);

				songSum2 = ((songMinutes * MIN_SEC) + songSeconds);

				songSum += ((songMinutes * MIN_SEC) + songSeconds);
				if (i == 0) {
					shortest = songSum2;
					shortestName = songName;
					longest = songSum2;
					longestName = songName;
					if (once)
					{
						longestAll = songSum2;
						longestNameAll = songName;
						shortestAll = songSum2;
						shortestNameAll = songName;
						once = false;
					}

				} else {
					if (songSum2 < shortest) {
						shortest = songSum2;
						shortestName = songName;
					}
					if (songSum2 < shortestAll)
					{
						shortestAll = songSum2;
						shortestNameAll = songName;

					}

					if (songSum2 > longest) {
						longest = songSum2;
						longestName = songName;
					}

					if (songSum2 > longestAll) {
						longestAll = songSum2;
						longestNameAll = songName;
					}
				}

				counter++;

			}

			songAvgmin = (int) ((songSum / 60) / 3.0);
			songAvgsec = (int) ((songSum / 3.0) % 60);
			if (songAvgsec < 10) {
				outfl
						.println("Song Average: " + songAvgmin + ":0"
								+ songAvgsec);
			} else {
				outfl.println("Song Average: " + songAvgmin + ":" + songAvgsec);

			}
			outfl.println("Longest Song : " + longestName);
			outfl.println("Shortest Song : " + shortestName);

			songTotalSum += songSum;
			songSum = 0;

		}

		songTotalAvgMin = (int) ((songTotalSum / 60) / counter);
		songTotalAvgSec = (int) ((songTotalSum / counter) % 60);

		if (songTotalAvgSec > 10) {
			outfl.println("Song Average of all songs: " + songTotalAvgMin + ":"
					+ songTotalAvgSec);
		} else {
			outfl.println("Song Average of all songs: " + songTotalAvgMin
					+ ":0" + songTotalAvgSec);
		}

		outfl.println("Longest Song of all songs: " + longestNameAll);
		outfl.println("Shortest Song of all songs: " + shortestNameAll);

		System.out.println("All, done. Now I'll clean up...");

		infl.close();
		outfl.close();
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>