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 4 of 4

Thread: NumberFormat to Java String Format

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default NumberFormat to Java String Format

    Help, on a homework assignment my instructor made the following comment, could anybody please help in making this change:

    In the Employee's toString, you are using the NumberFormat class to format your hourly rate and weekly pay but you are supposed to use java string formatting only (%f).
    You should change those to use only string formatting - no use of NumberFormat.

    package coursework;
     
    import java.text.NumberFormat;
     
    public class Employee {
     
    	private String firstName;
    	private String lastName;
    	private int employeeId;
    	private double hourlyRate;
    	public Timecard timeCard;
     
    	public Employee(String firstNameInput, String lastNameInput, int idInput,
    			double rateInput, int[] daysIn) {
    		setFirstName(firstNameInput);
    		setLastName(lastNameInput);
    		setEmployeeID(idInput);
    		setHourlyRate(rateInput);
    		setTimeCard(new Timecard(daysIn));
    	}
     
    	public void printEmpDetails() {
    		System.out.println("Name: " + firstName + " " + lastName);
    		System.out.println("Emp. Id: " + employeeId);
    		System.out.println("Hourly Rate: $" + hourlyRate);
    		System.out.println();
    	}
     
    	private void setFirstName(String firstName) {
    		// Here we are setting our class variable firstName to the passed in
    		// variable "name"
    		// But only if it has a length greater than 0 and less than or equal to
    		// 20
     
    		int nameLength = firstName.length();
     
    		if (nameLength < 1) {
    			System.out.println("First Name length is too small");
    			System.out.println(firstName + ": " + firstName.length());
    		} else if (nameLength > 20) {
    			System.out.println("First Name length is too long");
    			System.out.println(firstName + ": " + firstName.length());
    		} else {
    			this.firstName = firstName;
    		}
    	}
     
    	private void setLastName(String lastName) {
    		// Here we are setting our class variable firstName to the passed in
    		// variable "name"
    		// But only if it has a length greater than 0 and less than or equal to
    		// 20
     
    		int nameLength = lastName.length();
     
    		if (nameLength < 1) {
    			System.out.println("Last Name length is too small");
    			System.out.println(firstName + ": " + lastName.length());
    		} else if (nameLength > 20) {
    			System.out.println("Last Name length is too long");
    			System.out.println(firstName + ": " + lastName.length());
    		} else {
    			this.lastName = lastName;
    		}
    	}
     
    	private void setEmployeeID(int employeeId) {
    		// Here we are setting our class variable firstName to the passed in
    		// variable "name"
    		// But only if it has a length greater than 0 and less than or equal to
    		// 20
     
    		if (employeeId > 9999) {
    			System.out
    					.println("Number length error: this should be less than 9999: "
    							+ employeeId);
    		} else if (employeeId < 1000) {
    			System.out
    					.println("Number length error: this should be more than 1000: "
    							+ employeeId);
    		} else {
    			this.employeeId = employeeId;
    		}
    	}
     
    	private void setHourlyRate(double hourlyRate) {
    		// Here we are setting our class variable firstName to the passed in
    		// variable "name"
    		// But only if it has a length greater than 0 and less than or equal to
    		// 20
     
    		if (hourlyRate < 0) {
    			System.out
    					.println("Incorrect value, number must be greater than zero: "
    							+ hourlyRate);
    		} else {
    			this.hourlyRate = hourlyRate;
    		}
    	}
     
    	public void setTimeCard(Timecard newtimeCard) {
     
    		if (newtimeCard == null) {
    			System.out.print("time card cannot be null");
    			System.exit(-1);
    		}
    		timeCard = newtimeCard;
    	}
     
    	public Timecard getTimeCard() {
    		return timeCard;
    	}
     
    	public String getFirstName() {
    		// Here we are getting our class variable firstName
    		return firstName;
    	}
     
    	public String getLastName() {
    		// Here we are getting our class variable lastName
    		return lastName;
    	}
     
    	public int getEmployeeId() {
    		// Here we are getting our class variable firstName
    		return employeeId;
    	}
     
    	public double getHourlyRate() {
    		// Here we are getting our class variable firstName
    		return hourlyRate;
    	}
     
    	public double getWeeklyPay() {
    		int regHrs = 0;
    		int otHrs = 0;
    		for (int i = 0; i < 5; i++) {
    			int hrsDay = getTimeCard().getHoursByDay(i);
    			int ot = 0;
    			if (hrsDay > 8) {
    				ot = hrsDay - 8;
    				hrsDay = hrsDay - ot;
    			}
    			regHrs += hrsDay;
    			otHrs += ot;
    		}
    		double baseWeeklyPay = (regHrs * getHourlyRate()) + (otHrs * getHourlyRate() * 1.5);
    		char begId = String.valueOf(getEmployeeId()).charAt(0);
    		if (begId == '0' || begId == '2' || begId == '9') {
    			baseWeeklyPay = baseWeeklyPay * 1.1;
    		} else if (begId == '3') {
    			baseWeeklyPay = baseWeeklyPay * 0.9;
    		} else if (begId == '8') {
    			baseWeeklyPay = baseWeeklyPay * 1.2;
    		}
    		if (regHrs > 34) {
    			baseWeeklyPay = baseWeeklyPay * 0.94;
    		}
    		return baseWeeklyPay;
    	}
     
    	public String toString() {
    		StringBuilder sb = new StringBuilder();
    		String.format formatter = StringFormat.getCurrencyInstance();
     
    		sb.append(String.format("Name:          %s %s",getFirstName(),getLastName())).append("\n")
    				.append(String.format("Id:            %s",getEmployeeId())).append("\n")
    				.append(String.format("Hourly Rate:   %s",formatter.format(getHourlyRate()))).append("\n")
    				.append(getTimeCard());
     
    		sb.append(String.format("Weekly Pay :   %s",formatter.format(getWeeklyPay())));
    		sb.append("\n");
     
    		return sb.toString();
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,363
    Thanks
    49
    Thanked 2,207 Times in 2,180 Posts

    Default Re: NumberFormat to Java String Format

    you are supposed to use java string formatting
    Did you look in the String class's API doc to see what method the instructor was talking about?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: NumberFormat to Java String Format

    I have no idea

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,363
    Thanks
    49
    Thanked 2,207 Times in 2,180 Posts

    Default Re: NumberFormat to Java String Format

    Here is the link to the API doc. Look up the String class in the lower left frame, click on it and the API doc for the String class will be shown in the main frame.
    Java Platform SE 7

    Scroll down to where the class's methods are listed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Percentage with String.format
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 24th, 2013, 10:11 AM
  2. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  3. Replies: 1
    Last Post: March 15th, 2013, 03:04 AM
  4. Java using printf to format.
    By maple1100 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 31st, 2012, 07:29 PM
  5. NumberFormat class, rounding decimals (Beginner needs help!)
    By richyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 9th, 2012, 09:31 AM