Hello. I'm currently working on a lab and my logic is correct but I am having trouble with formatting errors through gradescope. I know I need to correctly implement in "%7.2f\n" for my void printInfo method but otherwise I'm not sure. I have attached my code, the assignment, and my codes output
#PayStub Assignment - formatting
133 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @atomic fable! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Line 0 [ERROR]: Expected: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: ==========================================", Actual: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: Gekko & Co."
Line 1 [OK]: ""
Line 0 [OK]: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: Gekko & Co."
Line 1 [ERROR]: Expected: " Gekko & Co.", Actual: ""
Line 2 [OK]: ""
Line 2 [OK]: ""
Line 3 [ERROR]: Expected: "", Actual: ""
Line 4 [OK]: ""
Line 3 [OK]: ""
Line 4 [ERROR]: Expected: "", Actual: ""
Line 5 [OK]: " "$""
Line 5 [OK]: " "$""
Line 6 [ERROR]: Expected: "", Actual: ""
Line 7 [OK]: " ~~~"
Line 7 [OK]: " ~~~"
Line 8 [ERROR]: Expected: "", Actual: ""
Line 9 [OK]: " / ." Line 9 [OK]: " / ."
Line 10 [ERROR]: Expected: "", Actual: ""
Line 11 [OK]: " / /"
Line 11 [OK]: " / /"
Line 12 [ERROR]: Expected: "", Actual: ""
Line 13 [OK]: " /_ _ _ /"
Line 14 [OK]: ""
codeblock would probably help
seems like you're just missing a linebreak though?
the errors there are all in the top logo portion
why are the lines not sequential 
if you open the document attached and scroll to the output you can see that mine matches aside from the "%7.2f\n" expected in the void printInfo method
I'm not sure how to debug using whats said with gradescope its greek to me
that's not what's being said in the error output though
are you sure you don't need an extra \n after the inputs
Line 0 [ERROR]: Expected: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: ==========================================", Actual: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: Gekko & Co."
Line 1 [OK]: ""
Line 0 [OK]: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: Gekko & Co."
Line 1 [ERROR]: Expected: " Gekko & Co.", Actual: ""
Line 2 [OK]: ""
Line 2 [OK]: ""
Line 3 [ERROR]: Expected: "", Actual: ""
Line 4 [OK]: ""
Line 3 [OK]: ""
Line 4 [ERROR]: Expected: "", Actual: ""
Line 5 [OK]: " "$""
Line 5 [OK]: " "$""
Line 6 [ERROR]: Expected: "", Actual: ""
Line 7 [OK]: " ~~~"
Line 7 [OK]: " ~~~"
Line 8 [ERROR]: Expected: "", Actual: ""
Line 9 [OK]: " / ."
Line 9 [OK]: " / ."
Line 10 [ERROR]: Expected: "", Actual: ""
Line 11 [OK]: " / /"
Line 11 [OK]: " / /"
Line 12 [ERROR]: Expected: "", Actual: ""
Line 13 [OK]: " /_ _ _ /"
Line 14 [OK]: ""
I'm running it now to see
Line 0 [ERROR]: Expected: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: ==========================================", Actual: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: Gekko & Co."
Line 1 [OK]: ""
Line 0 [OK]: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: Gekko & Co."
Line 1 [ERROR]: Expected: " Gekko & Co.", Actual: ""
Line 2 [OK]: ""
Line 2 [OK]: ""
Line 3 [ERROR]: Expected: "", Actual: ""
Line 4 [OK]: ""
Line 3 [OK]: ""
Line 4 [ERROR]: Expected: "", Actual: ""
Line 5 [OK]: " "$""
Line 5 [OK]: " "$""
Line 6 [ERROR]: Expected: "", Actual: ""
Line 7 [OK]: " ~~~"
Line 7 [OK]: " ~~~"
Line 8 [ERROR]: Expected: "", Actual: ""
Line 9 [OK]: " / ." Line 9 [OK]: " / ."
Line 10 [ERROR]: Expected: "", Actual: ""
Line 11 [OK]: " / /"
Line 11 [OK]: " / /"
Line 12 [ERROR]: Expected: "", Actual: ""
Line 13 [OK]: " /_ _ _ /"
Line 14 [OK]: ""
Line 15 [ERROR]: Expected: "------------------------------------------", Actual: "Employee Name: Karin Nadya"
Line 16 [OK]: "Anniversary Month: 4"
Line 15 [OK]: "Employee Name: Karin Nadya"
Line 16 [ERROR]: Expected: "Pay Period: 1/2019", Actual: "Anniversary Month: 4"
Line 17 [OK]: "Anniversary Year: 2015"
Line 16 [OK]: "Anniversary Month: 4"
Line 17 [ERROR]: Expected: "Name: Karin Nadya", Actual: "Anniversary Year: 2015"
Line 18 [OK]: "Job Title: Computer System Analyst"
Line 17 [OK]: "Anniversary Year: 2015"
Line 18 [ERROR]: Expected: "Title: Computer System Analyst", Actual: "Job Title: Computer System Analyst"
Line 19 [OK]: "Hours Worked: 170"
Line 18 [OK]: "Job Title: Computer System Analyst"
Line 19 [ERROR]: Expected: "Anniversary: 4/2015", Actual: "Hours Worked: 170"
Line 20 [OK]: "Vacation Hours Earned: 371.25"
Line 19 [OK]: "Hours Worked: 170"
Line 20 [ERROR]: Expected: "Months Worked: 45 months", Actual: "Vacation Hours Earned: 371.25"
Line 21 [OK]: ""
Line 20 [OK]: "Vacation Hours Earned: 371.25"
Line 21 [ERROR]: Expected: "Vacation hours: 371.25", Actual: ""
Line 22 [OK]: ""
Line 21 [OK]: ""
Line 22 [ERROR]: Expected: "------------------------------------------", Actual: ""
Line 23 [OK]: "Gross Pay: $7160.40"
Line 23 [OK]: "Gross Pay: $7160.40"
Line 24 [ERROR]: Expected: "Retirement: $ 372.34", Actual: "Retirement Withholding: $372.34"
Line 25 [OK]: "Tax Withholding: $1900.66"
Line 24 [OK]: "Retirement Withholding: $372.34"
Line 25 [ERROR]: Expected: "Tax: $1900.66", Actual: "Tax Withholding: $1900.66"
Line 26 [OK]: ""
Line 25 [OK]: "Tax Withholding: $1900.66"
Line 26 [ERROR]: Expected: "------------------------", Actual: ""
Line 27 [OK]: "Net Pay: $4887.40"
Line 27 [OK]: "Net Pay: $4887.40"
Line 28 [ERROR]: Expected: "==========================================", Actual: ""
Test testProduceCorrectOutput2() FAILED
Line 0 [ERROR]: Expected: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: ==========================================", Actual: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: Gekko & Co."
Line 1 [OK]: ""
Line 0 [OK]: "Enter your Fullname: Enter your Anniversary Month(1-12): Enter your Anniversary Year: Enter your hours worked this pay period(0-350): Enter your Job Title: Enter your pay rate: Gekko & Co."
Line 1 [ERROR]: Expected: " Gekko & Co.", Actual: ""
Line 2 [OK]: ""
Line 2 [OK]: ""
Line 3 [ERROR]: Expected: "", Actual: ""
Line 4 [OK]: ""
Line 3 [OK]: ""
Line 4 [ERROR]: Expected: "", Actual: ""
Line 5 [OK]: " "$""
Line 5 [OK]: " "$""
Line 6 [ERROR]: Expected: "", Actual: ""
Line 7 [OK]: " ~~~"
Line 7 [OK]: " ~~~"
Line 8 [ERROR]: Expected: "", Actual: ""
Line 9 [OK]: " / ."
Line 9 [OK]: " / ."
Line 10 [ERROR]: Expected: "", Actual: ""
Line 11 [OK]: " / /"
Line 11 [OK]: " / /"
Line 12 [ERROR]: Expected: "", Actual: ""
Line 13 [OK]: " /_ _ _ /"
Line 14 [OK]: ""
Line 15 [ERROR]: Expected: "------------------------------------------", Actual: "Employee Name: Karin Nadya"
this seems like the same one?
yea it didn't affect it ig
it doesnt sense the double line ======== goes right to gekko & co
you didn't add what i told you to though
you aren't printing that line though?
I added \n in my main method after inputs
you didn't though
did you forget to save before submitting/sending here? or are you maybe sending the wrong file?
also you have double newlines, println already puts a \n on the end of the strings
that is also a double newline
Ive made no progress with gradescope
just do System.out.println();, remove all the double newline stuff, submit it, show your current code and the new output
everything
so you think I should add println to all my statements in main?
I dont
that's not the part im talking about 💀
so you are talking about my printInfo method
no, that's in your main method
you sent your entire class file
it isn't though...
did you even read your file?
I'm not sure why the file wasn't updating. Yes please yikes
okayyy lol
gimme 5
okay so dunno why theres one file that wont update and one that does automatically but sorry...
Test Case input:
Karin Nadya
4
2015
170
Computer System Analyst
42.12
Line 17 [OK]: "Gross Pay: $7160.40"
Line 18 [ERROR]: Expected: "Retirement: $ 372.34", Actual: "Retirement: $372.34"
Line 19 [OK]: "Tax: $1900.66"
Test testProduceCorrectOutput2() FAILED
org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
at app//PayStubTest.testProduceCorrectOutput2(PayStubTest.java:227)
so I am getting the error I referred to in my post
why is it so cryptic
oh it's just the formatting
cool
wait is %7.2f not working?
oh you don't have it inside the format
%f and stuff only work inside format strings
i dont know how to properly apply it...
right now you have the format %.2f, and you're applying 2 arguments to it (but it only uses one, so the latter is just discarded)
you need to use the format %7.2f
So the %7.2f is discarded?
afaik, java has 3 main kinds of strings (based on use, not any syntax stuff)
- literal strings (messages and whatever)
- regex strings, for regex
- format strings, for formatting (split into the multiple types)
yeah, but you don't have it in the right place to begin with; you're using it as a literal, rather than a format
only the first string in String.format or System.out.printf are format strings, the rest are applied as arguments to that format, so any other strings will be literal
/run
System.out.printf("%.2f %s", 342.34, "%7.2f");
Here is your java(15.0.2) output @jovial hinge
342.34 %7.2f```
that's shown as is, to the user, rather than being processed as a format
but I believe that value needs to be rounded with %.2f
in the above example, %.2f %s is the format, with %.2f and %s being the format specifiers
342.34 and "%7.2f" are applied as the arguments
you instead need "%7.2f" to be used as the format, rather than "%.2f" (in your original task)
how can you do that?
you don't notice any similarities between %.2f and %7.2f?
1234567
Retirement: $ 372.34
12
```it's 7 wide with 2 digits of precision
and how does it know to be right bound?
the .2 is the precision modifier in the format, that's not changed so you still get 2 digits of precision
that's just the default, adding a - will make it left-justified
ah okay. I ran it on intellij testing it on gradescope
anyways there is quite a few places you could improve, so once you get it passing we can work on those
Okay sounds good. Only getting checkstyle errors the code is passing
do you want to deal with those first or should i just get into it
just get into it
so as i mentioned, System.out.printf is a thing
you can use it to print stuff directly formatted, instead of having to do concatenation with String.format;
formatting is like a replacement to concatenation, so you shouldn't be mixing them.
- System.out.print(String.format("format", ...args...));
+ System.out.printf("format", ...args...);
````printf` is like `print`, so it won't add a `\n`, so to replace `println` you can add a `\n`, or preferably, `%n` to the format
```diff
- System.out.println(String.format("format", ...args...));
+ System.out.printf("format%n", ...args...);
as shown before, format strings can also take other text that isn't part of format specifiers, like the space between the 2 arguments in my example
that extends to any text excluding % itself, which must be escaped by another %; other than that, you can just put other stuff in the format string
System.out.printf("text %s words", "arg"); -> "text arg words"
print with a \n at the end can be replaced by println of course, doing so lets you focus on the content of the string itself more clearly
you have quite a few excessive fields in your class, all the stuff that doesn't match a parameter of the constructor there is unnecessary; they can be computed on-demand instead, like in your printInfo method
so you only need 6 fields for the 6 parameters given
your 6-argument constructor takes all possible values that it needs, so it should be considered the canonical, main constructor of the class
your 0-argument constructor should instead point to that canonical constructor with the default values rather than assigning each field itself
[style] you have quite excessive empty lines
can you show an example from a line of my code?
System.out.println("Vacation hours: " + String.format("%.2f", vacationHours()));
would be
System.out.printf("%.2f%n", "Vacation hours: ", vacationHours());
no, only one format string
and you would maintain the order
anything that's a format specifier is changed, anything that isn't is left as-is
remember, the first string is a format string, any others are just literal strings
Vacation hours: %.2f%n as a single string would work as that format string
System.out.printf("%.2f", "Vacation hours: " + vacationHours()); like this?
and what excessive empty lines?
.
you have 3 empty lines at the end of your main, also 2 empty lines before your constructor
usually just 1 empty line is used for separation
ah I usually clean those up at the end. Still not sure of the printf benefit
less code to write?
as mentioned, the first argument of printf/format is the format string, this is used as the base for the output
Vacation hours: %.2f%n
^ ^ format specifiers
```only those will be changed into the respective values
it's basically a more primitive string interpolation, which would look like "Vacation hours: {vacationHours()}"
java doesn't have this though
concatenation gets really messy with more values or more complicated strings
compare
"Point(" + x + ", " + y + ")"
vs
String.format("Point(%d, %d)", x, y)
you can see the bigger picture much more easily
printf is just a shortcut to formatting
yea I can see how it'd be a shortcut
So I haven't received this error before /autograder/source/1P/src/main/PayStub.java:57:5: Constructor definition in wrong order.
public PayStub(String name, int anniversaryMonth, int anniversaryYear, int hours, String title, double payRate) {
this.name = name;
this.anniversaryMonth = anniversaryMonth;
this.anniversaryYear = anniversaryYear;
this.hoursWorked = hours;
this.jobTitle = title;
this.hourlyPayRate = payRate;
this.monthsWorked = numMonthsWorked();
this.vacationHours = vacationHours();
this.grossPay = grossPay();
this.retirementWithholding = retHold();
this.taxWithholding = tax();
this.netPay = netPay();
}
does it want this.name, this.title, this.payRate?
I had to take an important call... Yea I already read that thread