Results 1 to 4 of 4
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Am I doing this wrong? Making a song collection sort program

    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\tRank\tArtist\tTitle
    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 (\t). 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:
    [CODE]
    public static Song parse(String s) { //Parse a string of the form “Year\tRank\tArtist\tTitle” 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
    }

    public String getArtist() { //returns the artist of the song
    }

    public String getTitle() { //returns the title of the song
    }

    public String toString() { //returns a string representation of this song

    }
    }

    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;
    
    	public static Song parse(String s) {
    		Scanner input = new Scanner(s);
    		Scanner tokens = new Scanner(input.nextLine());
    		tokens.useDelimiter("\t");
    		Song instance = new Song();
    		instance.year = input.nextInt();
    		instance.rank = input.nextInt();
    		instance.artist = input.next();
    		instance.title = input.next();
    		return instance;
    	}
    
    	public int getYear() {
    		return year;
    	}
    
    	public int getRank() {
    		return rank;
    	}
    
    	public String getArtist() {
    		return artist;
    	}
    
    	public String getTitle() {
    		return title;
    	}
    
    	public String toString() {
    		String output = "Year =" + year + "\nRank =" + rank + "Artist = " + artist + "\nTitle = " + title;
    		return output;
    	}
    }
    Main method:
    Java Code:
    public class GazillionSongs {
    	public static void main(String[] args){
    		Song test = new Song();
    		test.parse("10\t1\tArtist\tTitle");
    	}
    }
    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?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Am I doing this wrong? Making a song collection sort program

    It's hard to say without seeing the actual error and appropriate line number. I would also recommend a couple of things.

    Get the input in your main program and then pass it to a constructor when creating Song. Even if you don't do that you
    are getting an instance of Song to call your static parse method. Your don't need to (and shouldn't) do that since parse is a class
    method and is declare static.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Am I doing this wrong? Making a song collection sort program

    What is tokens used for? Set your delimiter on your input instance of scanner.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Am I doing this wrong? Making a song collection sort program

    Quote Originally Posted by jim829 View Post
    What is tokens used for? Set your delimiter on your input instance of scanner.

    Regards,
    Jim
    Oh sorry, I decided to use StringTokenizer instead since I knew how to use that.
    Now I realize that I was supposed to use tokens instead of input for the variables now that you pointed that out. Forgot about that.

Similar Threads

  1. Get MIDI Song Title
    By mcindafizzy in forum New To Java
    Replies: 1
    Last Post: 01-20-2011, 01:28 AM
  2. Trying to write a program that uses the Collection API
    By javanewbie410 in forum New To Java
    Replies: 0
    Last Post: 10-12-2010, 08:24 PM
  3. Help with my sort program
    By kneil in forum New To Java
    Replies: 6
    Last Post: 07-22-2010, 01:50 AM

Posting Permissions

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