Making A Song Collection Sort Program
Mar 8, 2014
I'm doing a project with very defined requirements. Input and output will be done to and from a file. Both the input and output files should have the same format. Each file will consist of a series of lines formatted as follows:
Year Rank Artist Title
That is, each line of the file will consist of the year, rank, artist, and title of a single song, with each of the fields separated by tabs ( ). Output files must maintain this format—you should be able to use the output file of one run of the program as the input to another run.
The first part of my project is to make a Song class, with 6 methods:
public static Song parse(String s) { //Parse a string of the form “Year Rank Artist Title” and create a Song object with the given values.
}
public int getYear() { //returns the year of the song
}
public int getRank() { //returns the rank of the song
[Code] .....
So far, I have worked out my Song class like this:
Java Code:
import java.util.Scanner;
public class Song {
private int year;
private int rank;
private String artist;
private String title;
[Code] ....
I know there's definitely a problem with my parsing, as I am getting the
Exception in thread "main" java.util.NoSuchElementException
when I attempt to input a test String at my variables.
Should I try using StringTokenizer and Integer.parseInt()? I think that perhaps the reason why an error is occuring is because the String is being parsed into Strings and there are no int values to be inputted into the year and rank variables.
Or is there a way to do this with Scanner?
View Replies
ADVERTISEMENT
Nov 27, 2014
I working on a bank program that gets a user id, name and balance from a text file. I need to be able to sort based on each field. I'm using Collection.sort and Comparator. I'm using String[] array = file.toFile().list(); to get the values for the array from the text file. I can't figure out how to make this into an arraylist. I understand the concept when hardcoding the data but having trouble doing it from a text file.
View Replies
View Related
Apr 8, 2015
I am working on a class that sorts and matches items in a collection of arrays. In the end, elements common to all 3 arrays should be printed. The idea is that as the first array is compared to the second it stores the matched items in tempArray. When all items are compared the tempArray should overwrite the checked array, and the process continues until all arrays are checked. I have set all the checking in a do loop that should run while the value is <= to the array length. This allows all items in the reference array to be checked. I get an index out of bounds message but not where I would expect it. I have tried varying the condition in the do while loop in the match method, but it did not change the result. There may be other issues I have not addressed with solving the algorithm, but this one has me stumped and I am not able to progress.
package testing;
public class TestMatchMain {
public static void main(String[] args) {
Comparable[] innerCollection0 = {1,2,3,4,5};
Comparable[] innerCollection1 = {1,1,5,6,7};
[Code] .....
View Replies
View Related
Feb 24, 2014
i want to design a media player but my program can't play any song it is showing me error "Uncompilable source code - cannot find symbol
symbol: class MediaPanel
location: class media.Media
at media.Media.main(Media.java:28)"
This is my code.
package media;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFileChooser;
[Code] .....
I got the code from [URL] .....
View Replies
View Related
Oct 21, 2014
How would I modify this version of a selection sort into a recursive method?
public static void selectionSortRecursive(Comparable [] list, int n)
{
int min;
Comparable temp;
for(int index =0; index < n-1; index++){
min = index;
[code]....
View Replies
View Related
Apr 6, 2014
I have to make a program on J2Me which I don't have a clue, the question is :
-------------
"Initial display is a text box, with a command button "Generate". When the user enters the list type and clicks on the Generate button, the application asks count of elements and gets the elements and finally displays the list."
--------------
View Replies
View Related
Aug 31, 2014
I just started programming yesterday, and I have been making little programs to learn some algorithms and the syntax of java. I tried making a program that could convert metric units into the American system. Whenever I try to compile the code, it shows up as an "incompatible type" error. I know this is probably a very obvious answer to most of you, but I can't figure it out.
import java.util.Scanner;
public class MetricToAmerican
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("fWhat measurement would you like to convert into US system?
celsius, meter, or kilogram ");
[code]....
View Replies
View Related
Apr 14, 2015
I am working on my generic insertion sort program. When I completed and run my code, I am having trouble with my code. So, I am just trying to see if I get an correct array from a file. However, i just get so many nulls in the array. Therefore, I can't run my insertionSort function because of null values.
Here is my code
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
[Code].....
View Replies
View Related
Jul 22, 2014
In my card game applet I have to play some music and the user must have the ability to play, stop and pause the song using buttons. I got the play and stop to work easy but I'm unable to add the pause option/method.
View Replies
View Related
Feb 23, 2014
I need to write up a code that will print the lyrics of the Green Bottles song. So it will ask the user to input a number and it will use that number to print the lyrics and then count down from that number. For example, user enters the number 5, the program will run and display:
"5 green bottles, hanging on the wall
5 green bottles hanging on the wall
and if one green bottles should fall
there will be 4 green bottles hanging on the wall
4 green bottles, hanging on the wall...." and so on.
So far I have managed to write a code that will display the number that is entered but I cant figure out how to subtract the number by 1 to continue it. My code so far:
import java.util.Scanner;
public class SomeJavaCode
{
public static void main(String[] args)
{
System.out.println("How many bottles to start with?");
[Code] ....
How to code it so that it substracts the number and repeats the rhyme till 0.
View Replies
View Related
Feb 15, 2014
write a program to sort names according to number of vowels sort({sam,ratan,alok,raj}) the op is {sam,ratan,alok,raj}
View Replies
View Related
Mar 9, 2014
What I'm supposed to be doing is making it so the program can accept multiple filter and sort commands at one time, and each should be separated by a whitespace.I was thinking about parsing the input again, using whitespace as the delimiter, then normally progressing with each token, as though there was only one command.
However, coding this the way I'm doing it will firstly probably take hours, and secondly, it's likely not even right. I don't have any real way to determine which token contains which data from the Song objects.These are the specific requirements for this portion:A sort/filter command consists of one or more of the following options:
-year:<year(s)>
-rank:<rank(s)>
-artist:<artist>
-title:<title>
-sortBy:<field>
Any number of these options may be given, and they may be given in any order. If multiple options are specified, they will be separated by whitespace.
GazillionSongs Class (the main)
Java Code:
import java.util.*;
import java.io.*;
[code]....
View Replies
View Related
Jun 10, 2014
import java.io.*;
Class bubble
{
public static void main()throws IOException
{
BuffetedReader br=new BufferedReader (new InputStreamReader (System.in));
int j, k, temp;
String st[]=new string [10];
[Code] .....
View Replies
View Related
Apr 7, 2014
I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class SwingSorts extends JFrame implements ActionListener
{
JRadioButton bubble;
JRadioButton selection;
[Code] .....
View Replies
View Related
Aug 16, 2014
I have created a playlist class that ask the user to enter a song name and etc. What I want to do is have the mp3 class plays the song that the user has entered, but I don't know how.
Here are my codes:
import java.util.Scanner;
public class PlaylistDriver
{//Start of class
public static Playlist list;
public static void main(String[] args)
{//Start of main
list = new Playlist();
[code]....
View Replies
View Related
Feb 14, 2015
I have small project to be implemented in Java, which, expected the management of a parking.
The project has a class abstract Vehicle, whence derive three classes: Car, Motorcycle, Heavy Vehicles; the cost estimated time for the 3 types of vehicles are: 2€ for Cars, 1€ for Motorcycle and 5€ for H.V.
In addition, in class Ticket will be stored the arrival time of the customer and the characteristics of his vehicle.
Finally, in the class Parking(which provides 80 places available), here it should be added the various types of vehicles.
Now, I though of using an Collection, as Set.. So that they can not, two Vheicle with same license plate.
View Replies
View Related
Jul 22, 2014
I have a method
void method(){
String s =null;
String s1 = new String();
}
In this all the two strings are eligible for garbage collection?
View Replies
View Related
Feb 27, 2014
I am using mysql database and I have downloaded the library also I have managed to get the database connection and query working. I need to know if I need to create separate classes to add/remove/edit items and view items? Do I need to put my database connection script in every class that I create or should I create methods for both the connection and the queries that will be called in the additional classes or methods that I have?
Below is what I have written so far and it is working, I will change the database and query soon to reflect the task I need to do because I have followed a tutorial.
Java Code:
/**
* cdCollection.java
*/
package org.com.mm00422_prototype;
//Import for the SQL package
import java.sql.*;
//Registering the JDBC driver
//Class.forName("com.mysql.jdbc.Driver");
[code]....
View Replies
View Related
Feb 23, 2015
I have a method that accepts JSONArray as parameter and returns the values of it as ArrayList Object. My question which of these ways is appropriate in populating the ArrayList object this method populates the arraylist upon creation of object (I don't know what the right term to use, but as netbeans IDE suggest, JSONArray object should be final since it was used in inner class.).
private List<String> getStringList(final JSONArray jsonArr) {
return new ArrayList<String>() {
{
try {
for (int i = 0; i < jsonArr.length(); i++) {
add(jsonArr.getString(i));
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
};
}
this second method is the usual way of populating collection
private List<String> getStringList(JSONArray jsonArr) {
List<String> strList = new ArrayList<String>();
try {
for (int i = 0; i < jsonArr.length(); i++) {
strList.add(jsonArr.getString(i));
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
What are the advantages and disadvantages between the two? like which is faster? or which consumed larger memory?
View Replies
View Related
Nov 26, 2013
In my applications garbage collection logs, I see below line.
2013-11-26T10:35:45.050-0500: 5195.298: [GC 5195.298: [DefNew: 17430K->456K(19136K), 0.0102408 secs] 796185K->779211K(2668480K), 0.0103687 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
2013-11-26T10:36:10.593-0500: 5220.840: [GC 5220.840: [DefNew: 17480K->398K(19136K), 0.0100548 secs] 796235K->779153K(2668480K), 0.0101835 secs]
The last line seems half cut. Does that indicate that there is memory leak in my application? I have not seen the app throwing the out of memory error.
View Replies
View Related
Mar 19, 2014
I need to create an algorithm that finds the common element(s) in all arrays that has a signature of public Comparable[] findCommonElements(Object[] collection) that has an efficiency of at most O(knlogn), uses a query array, and accepts as input a collection of arrays. I am aware my time would be better spent learning how to use array lists and hash sets, but I am supposed to use concepts already covered, and these have not been.
I feel like this code should work, but it is returning null for the array of common elements. Which means it obviously is not working correctly. I am also likely going to need implementing the sort algorithm, but I wanted to get the part of finding the common elements set first.
public class CommonElements2<T extends Comparable<T>>
{
Comparable[] tempArr;
Comparable[] common;
Comparable[] queryArray;
/*
sort algorithm goes here
*/
public Comparable[] findCommonElements(Object[] collections)
[code]....
View Replies
View Related
Feb 18, 2014
Write a function (or functions) that given a collection of files will produce a sum of integers from each line of each file. Each file can have any number of lines from 1 to N. Each line can contain only one integer and no other alphanumeric characters. All of the numbers from all of the files should be added to the final result. The result is just one number.
View Replies
View Related
May 11, 2014
I have wrote the method delete turtle below. Which is part of turtle collection (Hashmap)
public void deleteTurtle(String name) {
Iterator iterate = turtles.keySet().iterator();
while(iterate.hasNext()) {
String key = (String)iterate.next();
if(key.equals(name)) {
iterate.remove();
} else {
setErrorMessage("notFound");
}
}
}
I need to now modify it so the method ensures that the turtles shape collection is cleared before the Turtle object is deleted.
View Replies
View Related
Apr 20, 2015
I have Date as listed below:
17/03/2015 09:38:39 AM
17/03/2015 10:52:26 AM
10/03/2015 08:30:56 AM
02/03/2015 09:18:10 AM
02/03/2015 09:37:23 AM
02/03/2015 11:25:01 AM
02/03/2015 11:29:00 AM
02/03/2015 11:42:38 AM
02/03/2015 12:04:39 PM
02/03/2015 12:09:05 PM
02/03/2015 01:17:09 PM
02/03/2015 01:29:08 PM
I want them to sort them as per below result: (Same date one should be sort by timestamp)
17/03/2015 10:52:26 AM
17/03/2015 09:38:39 AM
10/03/2015 08:30:56 AM
02/03/2015 01:29:08 PM
02/03/2015 01:17:09 PM
02/03/2015 12:09:05 PM
02/03/2015 12:04:39 PM
02/03/2015 11:42:38 AM
02/03/2015 11:29:00 AM
02/03/2015 11:25:01 AM
02/03/2015 09:37:23 AM
02/03/2015 09:18:10 AM
I tried using Collection.sort using compareTo but result is not expected...
View Replies
View Related
Nov 18, 2014
So I have a persons class:
public class Person
{
private String name;
private int age;
public Person (String name, int age) {
this.name = name;
this.age = age;
[Code] .....
And I need to write a simple main method that creates lots of instances of the Person class and adds them to a generic instantiation of a Collection (ArrayList). And I need to make it so I as a programmer can define how many instances to create.
View Replies
View Related
Mar 25, 2014
I have the following
Java Code: list.stream().sorted((s, s1) -> s.getName().compareTo(s1.getName())).distinct(); mh_sh_highlight_all('java');
I want to save the results to a collection.
I saw an video tutorial on these by Oracle learning library and they mentioned an into(collection) method but I looked at the api and there isn't one.
How can I do it?
View Replies
View Related