#Floating-point numbers (double) ((basic java))

1 messages · Page 1 of 1 (latest)

upbeat citrus
#

ive tried a few ways of going about this, im like a week into the class, dont even know enough to really phrase a good question. Short story: how on earth do i get this thing to work??

lone socket
#

Oh it's just a string formatting issue

#

You made a mistake. It should be %.2f and %.4f you wrote %.2 and %.4. you just forgot to put the f which indicates what type your are printing.

upbeat citrus
#

does printf only read the first bit?

lone socket
#

There's another thing

upbeat citrus
#

like scnr.next() does?

lone socket
#

You don't put the variables in the formating

#

Here's an example

double x = 12.1545268184;
System.out.printf("x = %.2f", x);
upbeat citrus
#

hmm so what would my line look like?

zinc mountainBOT
lone socket
upbeat citrus
#

its all one line right?

lone socket
#

Another note, you don't need to do the concatenation in your printf.

upbeat citrus
#

does that just mean the + or does that include the " " as well?

#

im really hoping i look back on this in a few months and think its funny

lone socket
#

What I means by that it's that you can write the string as one and don't need to add multiple strings together

upbeat citrus
#

im tapped

#

my output is just q =

#

serious head scratcher

#

i wont bother u anymore tho i appreciate it

lone socket
#

Don't abandon

#

Your almost there

#

And you are not bothering me. I if didn't want to answer I wouldn't

upbeat citrus
#

well that makes me feel better

#

okay

#

so heres where im at

#

System.out.printf("q = %.4f when u = %.2f");

lone socket
# upbeat citrus my output is just q =

I'll reiterate on the issues

  1. You forgot the format type which in your case is f. So %.2 should be %.2f
  2. You need to pass values to the printf method
  3. you don't need to concatenate the strings
upbeat citrus
#

so do i do them in sperate lines?

lone socket
#

Look at my example of earlier. Can you see what I did to replace %.2f by the value of x

#

;compile

double x = 12.1545268184;
System.out.printf("x = %.2f", x);
zinc mountainBOT
#
Program Output
x = 12.15
upbeat citrus
#

you assigned x the value

#

and then uhh

#

honestly feel like itd be easier to know what the line should be

#

so i can try and conceptualize why that is

#

i dont get why the complier stops at the whitespace after the =

lone socket
lone socket
#

Look at my example

#

What comes after the string

upbeat citrus
#

, x

#

?

lone socket
#

Yes exactly

#

Do you know what those do?

upbeat citrus
#

oh hold on...

#

okay i think im getting somewhere

lone socket
#

So what printf does is it takes a string and a bunch of values and replace the format specifier (ex. %f) by the values your are providing to the method

upbeat citrus
#

so far i just checked it as

#

System.out.printf("q = %.4f ", q);

#

and that got me the output "q = -10.0000

#

now to add the "when u = 1.00"

#

does that get added to a separate line?

lone socket
#

No

#

The string you had before is already good. Printf can have multiple values given to it

upbeat citrus
#

is there a special formatting for after the ,q?

#

like do i use a + for the second bit

#

or can it squeeze in there

lone socket
#

What do you mean by squeeze it in there?

upbeat citrus
#

i tried..

#

System.out.printf("q %.4f ", q "when u = %.2f ", u);

#

but naturally

#

that did not work

lone socket
#

System.out.printf("q = %.4f when u = %.2f"); this line is already good. The only missing things are the values which are q and u. You figured out how to do it for q. It would give this System.out.printf("q = %.4f when u = %.2f", q);.

Now if I'd tell you that you need to add another value how would you do it. (don't change the string)

upbeat citrus
#

i cant just comma u i assume

lone socket
#

Have you read this

So what printf does is it takes a string and a bunch of values and replace the format specifier (ex. %f) by the values your are providing to the method

lone socket
#

Try it, it's free

upbeat citrus
#

mfw now i need a new line

#

but this is wild

#

maybe i shouldve taken this one in person 💀

lone socket
#

Well you just add \n at the end of the string

lone socket
upbeat citrus
#

figured programming 1 couldn't be too bad

upbeat citrus
#

so i kept my work availability open

#

buyers remorse settling for that one

#

why is it saying that \n is illegal?

lone socket
#

It needs to be in the string

upbeat citrus
lone socket
#

Yea... It needs to be in the string

#

"... \n"

upbeat citrus
#

lol finally

#

man that was like pulling teeth for you wasn't it💀

lone socket
#

Here we go

lone socket
upbeat citrus
#

the professor said the beginning would be rough

#

pretty foreign concept at the start of it all

#

biology, chemistry, history, art... most people have an idea or 2 of what they're about

lone socket
#

They are right. It can take time before things clicks. The secret is lots of practice.

upbeat citrus
#

programming is usually extremely out there at the beginning

#

cause at no point are you really exposed to the ideas

#

man was right lol

upbeat citrus
#

i submitted this for a C

#

but im going back and practicing because i want to learn

#

and dont want to fall behind on concepts

lone socket
#

That's the smart thing to do

#

Can you re-submit it? Late can be (depending on how much time and also the teacher) better than incomplete

upbeat citrus
#

no late work unfortunately

#

just had to throw it in with what i had at the time

lone socket
#

Oh well, you'll have to do better on the next assignment.

upbeat citrus
#

yup

#

i worked until 6pm did not expect this unit to be so tedious

#

only the 2nd one

#

first one was a lot easier

#

print, scnr, some math

#

println was the craziest thing that happened in that one

lone socket
#

Yea next time you'll be able to judge better the time and effort it requires and also ask questions before it's too late

upbeat citrus
#

100%

#

im off work for the next few days gonna get on the upcoming one tmrw

#

hence me going back on this one

#

feel like starting that one without knowing all of this one is gonna be extra difficult

lone socket
#

Yea it totally make sense. It was a good idea to finish that one first