1. #1
    High Overlord Zhaveros's Avatar
    10+ Year Old Account
    Join Date
    Jun 2011
    Location
    Vancouver Island, B.C. Canada
    Posts
    192

    Stuck on Java programming again.

    So I'm in the mood for experimenting and am stuck on making my program look pretty. When I run this program:

    Code:
    public class ToString { //It's called ToString because I made this class to learn about it. I'm conserving time.
    	private String Band;
    	private String genre;
    	private static int bands=0;
    	
    	public ToString(String bn, String gn) {
    		Band = bn;
    		genre = gn;
    		bands++;
    		
    		
    		if (genre.length() >= 10) {
    		System.out.printf("Band: %s\t Their genre: %s\t\t Number of bands: %d\n", Band, genre, bands );
    		}
    		else {
    			System.out.printf("Band: %s\t Their genre: %s\t Number of bands: %d\n", Band, genre, bands );
    		}
    	}
    	
    	
    }
    I get the issue where the text isn't inline on the last variable. The If statement I thought would be the cure, but the problem is, is that genre has multiple values, so while Rammstein and Death go ahead an extra tab, Gojira doesn't.

    Main class so you know what I'm talking about:

    Code:
    class Prac1 {
    	public static void main(String[] args) {
    		for (Prac2 games: Prac2.values()) 
    			System.out.printf("Game: %s\t Fun: %s\t Content: %s\n", games, games.getFun(), games.getContent());
    		
    		System.out.println("***And now for the set range!***");
    		
    		
    		for (Prac2 games: EnumSet.range(Prac2.WoW, Prac2.GW2))
    			System.out.printf("Game: %s\t Fun: %s\t Content: %s\n", games, games.getFun(), games.getContent());
    		System.out.println("\n***Metal Bands!***\n");
    	
    	ToString bnds1=new ToString("Rammstein", "New German Industrial Metal");	//Genre string length = 26
    	ToString bnds2=new ToString("Death", "Death metal");	
    	ToString bnds3=new ToString("Gojira", "Too many");	
    	}
    	
    }
    When this is run, the output looks like this:
    Code:
    Band: Rammstein	 Their genre: New German Industrial Metal		 Number of bands: 1
    Band: Death	 Their genre: Death metal		 Number of bands: 2
    Band: Gojira	 Their genre: Too many	 Number of bands: 3
    So how would I get it so that 'Number of bands' lines up with each other like the last two variables?

    ---------- Post added 2012-11-12 at 04:16 PM ----------

    Also the code before was just practising with Enum.

  2. #2
    Deleted
    Don't use tabs, they are unreliable (as you can see).

    You can either:

    make the fields (band name, genre) have a fixed length every time (with space characters at the end if necessary)

    - or -

    add the necessary amount of empty spaces at the end of band name/genre during printing so the fields line up (you'd need to know the largest band name and largest genre type and pass those values as parameters to the printing function)

    The second solution is the best since you don't waste space storing empty characters but it is a bit more complex as it involves storing or calculating the maximum length of the fields.

    Edit: Helpful link - String padding

  3. #3
    High Overlord Zhaveros's Avatar
    10+ Year Old Account
    Join Date
    Jun 2011
    Location
    Vancouver Island, B.C. Canada
    Posts
    192
    Would you happen to have some sample codes I can examine?

  4. #4
    The StackOverflow link has a code snippet showing padRight and padLeft, which are probably the methods you want to use for this kind of formatted output.

  5. #5
    Mechagnome
    10+ Year Old Account
    Join Date
    Nov 2010
    Location
    Melbourne, Australia
    Posts
    626
    Quote Originally Posted by User007 View Post
    Don't use tabs, they are unreliable (as you can see).
    That's not entirely true. In this case (because he's using a regular terminal), a tab is 8 characters which he could account for. However, the second you start saving to something else that might change (example: notepad++ on windows will allow you to change the tab length and is often 4 spaces, not 8).

    Of course, then you'd need to do a *lot* more work to know which one is going to be the longest. Some sort of output buffer that can be edited before output so you can add in extra tabs.

    Of course, setting a max length and accounting for it would work and just cut it off 4 spaces before the max character limit and add "... "

    Code:
    Band: Rammstein	 Their genre: New German I...		 Number of bands: 1
    Band: Death	 Their genre: Death metal		 Number of bands: 2
    Band: Gojira	 Their genre: Too many	                 Number of bands: 3

  6. #6
    Deleted
    Quote Originally Posted by stuck4cash View Post
    That's not entirely true. In this case (because he's using a regular terminal), a tab is 8 characters which he could account for. However, the second you start saving to something else that might change (example: notepad++ on windows will allow you to change the tab length and is often 4 spaces, not 8).
    Seems to me, that the behaviour you just described is exactly what one would classify as "unreliable". Anyhow, to the OP here goes something I cooked up here at work:

    Main program:
    Code:
    package entertainment;
    
    public class Entertainment {
    
    	public static void main(String[] args) {
    
    		Library library = new Library();
    		library.add(new Band("Rammstein", "New German Industrial Metal"));
    		library.add(new Band("Death", "Death metal"));
    		library.add(new Band("Gojira", "Too many"));
    		
    		System.out.println(library.toString());
    
    	}
    
    }
    "Library" class:
    Code:
    package entertainment;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Library {
    	
    	private List<Band> musicBands = new ArrayList<Band>();
    	
    	public int add(Band band) {
    
    		this.musicBands.add(band);
    		
    		return this.musicBands.size();
    	}
    	
    	public int remove(String bandName) {
    
    		// Find/Retrieve element with this "name"
    		Band toRemove = null;
    		for (Band band : this.musicBands) {
    			if (band.getName().equalsIgnoreCase(bandName)) {
    				toRemove = band;
    			}
    		}
    		
    		// Remove the element from the list (if found)
    		if (toRemove != null) {
    			this.musicBands.remove(toRemove);
    		}
    		
    		return this.musicBands.size();
    	}
    	
    	public String toString() {
    		
    		String outputString = "";
    		
    		int nameLength = 0;
    		int genreLength = 0;
    		
    		// Iterate all the bands and find the longest band name and longest genre length
    		for (Band band : this.musicBands) {
    			
    			int tempNameLength = band.getNameLength();
    			if (tempNameLength > nameLength) nameLength = tempNameLength;
    			
    			int tempGenreLength = band.getGenreLength();
    			if (tempGenreLength > genreLength) genreLength = tempGenreLength;
    			
    		}
    		
    		int bandCounter = 0;
    		
    		// Iterate bands and list each one in a different line
    		for (Band band : this.musicBands) {
    			
    			outputString += band.toString(nameLength, genreLength) + " Number of bands: " + ++bandCounter + "\n";
    			
    		}
    		
    		return outputString;
    	}
    
    }
    "Band" class:
    Code:
    package entertainment;
    
    public class Band {
    	
    	private String name = "";
    	private String genre = "";
    	
    	public Band(String name, String genre) {
    		this.name = name;
    		this.genre = genre;
    	}
    	
    	public String getName() {
    		return this.name;
    	}
    	
    	public int getNameLength() {
    		return this.name.length();
    	}
    	
    	public int getGenreLength() {
    		return this.genre.length();
    	}
    	
    	public String toString(int nameLength, int genreLength) {
    
    		return 	"Band: " + String.format("%1$-" + nameLength + "s", this.name) + 
    				" Their genre: " + String.format("%1$-" + genreLength + "s", this.genre);
    	}
    
    }
    Output:
    Code:
    Band: Rammstein Their genre: New German Industrial Metal Number of bands: 1
    Band: Death     Their genre: Death metal                 Number of bands: 2
    Band: Gojira    Their genre: Too many                    Number of bands: 3

Posting Permissions

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