#PayStub Assignment - formatting

133 messages · Page 1 of 1 (latest)

atomic fable
#

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

still narwhalBOT
#

⌛ This post has been reserved for your question.

Hey @atomic fable! Please use /close or the Close Post button 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.

jovial hinge
#

would help to show the errors...

#

@atomic fable

atomic fable
#

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]: ""

jovial hinge
#

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 thonk

atomic fable
#

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

jovial hinge
#

are you sure you don't need an extra \n after the inputs

still narwhalBOT
#
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]: ""
atomic fable
#

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

still narwhalBOT
#
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"
jovial hinge
#

this seems like the same one?

atomic fable
#

yea it didn't affect it ig

#

it doesnt sense the double line ======== goes right to gekko & co

jovial hinge
#

you didn't add what i told you to though

jovial hinge
atomic fable
#

I added \n in my main method after inputs

jovial hinge
#

you didn't though

#

did you forget to save before submitting/sending here? or are you maybe sending the wrong file?

atomic fable
jovial hinge
#

also you have double newlines, println already puts a \n on the end of the strings

atomic fable
#

i couldve just put println ig

#

bc I've just been trying to debug it

jovial hinge
atomic fable
#

Ive made no progress with gradescope

jovial hinge
#

just do System.out.println();, remove all the double newline stuff, submit it, show your current code and the new output

atomic fable
#

how do I have double new lines?

#

are you talking abt my main method

jovial hinge
#

println("\n") is 2 \n

#

println already puts a \n at the end of your strings

atomic fable
#

what are you referring to

#

I know

jovial hinge
#

everything

atomic fable
#

so you think I should add println to all my statements in main?

jovial hinge
#

you already have println bruh

#

you have both println and \n

atomic fable
#

I dont

jovial hinge
#

that's double newlines

atomic fable
jovial hinge
#

that's not the part im talking about 💀

atomic fable
#

so you are talking about my printInfo method

jovial hinge
#

no, that's in your main method

atomic fable
#

what I sent you is in my main

#

what you sent me in in my printInfo method

jovial hinge
#

you sent your entire class file

jovial hinge
#

did you even read your file?

atomic fable
#

my code?

#

I wrote it...

#

The assignment file?

jovial hinge
#

your printInfo is completely different, it doesn't have \n, so it's just 1 newline

atomic fable
#

yea thats not the right code...

jovial hinge
#

💀

#

ok let's restart lol

atomic fable
#

I'm not sure why the file wasn't updating. Yes please yikes

jovial hinge
#

uh, were you maybe submitting the old file too

#

just check rq lol

atomic fable
#

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

jovial hinge
#

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

atomic fable
#

i dont know how to properly apply it...

jovial hinge
#

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

atomic fable
#

So the %7.2f is discarded?

jovial hinge
jovial hinge
#

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");
tulip terraceBOT
#

Here is your java(15.0.2) output @jovial hinge

342.34 %7.2f```
jovial hinge
#

that's shown as is, to the user, rather than being processed as a format

atomic fable
#

o

#

so how can I go about using two arguments

jovial hinge
#

you don't

#

you only have 1 value to show here

atomic fable
#

but I believe that value needs to be rounded with %.2f

jovial hinge
#

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?

jovial hinge
atomic fable
#

I'm confused what the 7 does

#

yes..

jovial hinge
#
                1234567
Retirement:    $ 372.34
                     12
```it's 7 wide with 2 digits of precision
atomic fable
#

and how does it know to be right bound?

jovial hinge
#

the .2 is the precision modifier in the format, that's not changed so you still get 2 digits of precision

jovial hinge
atomic fable
#

ah okay. I ran it on intellij testing it on gradescope

jovial hinge
#

anyways there is quite a few places you could improve, so once you get it passing we can work on those

atomic fable
#

Okay sounds good. Only getting checkstyle errors the code is passing

jovial hinge
#

do you want to deal with those first or should i just get into it

atomic fable
#

just get into it

jovial hinge
#

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

atomic fable
#

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());

jovial hinge
#

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

atomic fable
#

System.out.printf("%.2f", "Vacation hours: " + vacationHours()); like this?

#

and what excessive empty lines?

jovial hinge
atomic fable
#

ah I usually clean those up at the end. Still not sure of the printf benefit

#

less code to write?

jovial hinge
# jovial hinge .

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
jovial hinge
# atomic fable less code to write?

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

atomic fable
#

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?

jovial hinge
atomic fable
#

I had to take an important call... Yea I already read that thread