# Enum types, how does he know %s value?

• July 18th, 2011, 01:53 PM
beer-in-box
Enum types, how does he know %s value?
Code :

```public enum Planet { MERCURY (3.303e+23, 2.4397e6), VENUS (4.869e+24, 6.0518e6), EARTH (5.976e+24, 6.37814e6), MARS (6.421e+23, 3.3972e6), JUPITER (1.9e+27, 7.1492e7), SATURN (5.688e+26, 6.0268e7), URANUS (8.686e+25, 2.5559e7), NEPTUNE (1.024e+26, 2.4746e7);   private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } private double mass() { return mass; } private double radius() { return radius; }   // universal gravitational constant (m3 kg-1 s-2) public static final double G = 6.67300E-11;   double surfaceGravity() { return G * mass / (radius * radius); } double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); } public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: java Planet <earth_weight>"); System.exit(-1); } double earthWeight = Double.parseDouble(args[0]); double mass = earthWeight/EARTH.surfaceGravity(); for (Planet p : Planet.values()) System.out.printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass)); } }```
This code is taken from Oracle's own web site. My question is about the last of the code. I mean this part:

Code :

``` for (Planet p : Planet.values()) System.out.printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass));```

%s and %d%n are used in this code. How does he know where to use %s and where to use %d%n? I mean he did not define these anywhere.

What am I missing?
• July 18th, 2011, 02:15 PM
Norm
Re: Enum types, how does he know %s value?
The printf() method uses the arguments following the initial String to fill in the values for the %x places.
Given: %s is %f%n",
%s <- p,
%f <- p.surfaceWeight(mass)
%n ???

Are you sure you copied this correctly
• July 18th, 2011, 02:44 PM
beer-in-box
Re: Enum types, how does he know %s value?
Well, the code is taken exactly from this link: Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
So, a second question then: How does he decide that p = %s and p.surfaceWeight(mass) = %f? Taking a look at the link and lesson, I couldn't see any information.
• July 18th, 2011, 02:47 PM
Norm
Re: Enum types, how does he know %s value?
Quote:

does he decide that p = %s and p.surfaceWeight(mass) = %f?
It's positional: the first % gets the value of first arg after the string(p), second % get value of second arg(p.surfaceWeight(mass)) ...
The value of p replaces the %s and the value of p.surfaceWeight(mass) replaces the %f%n
• July 18th, 2011, 02:50 PM
beer-in-box
Re: Enum types, how does he know %s value?
Actually why is it %s instead of %a?

I don't want to give you a headache :D
• July 18th, 2011, 02:55 PM
Norm
Re: Enum types, how does he know %s value?
Quote:

why is it %s instead of %a?
Did you look up what the s and what the a are used for? I haven't so I can't tell you.
I know there are letters for the different data types and they are all listed with the printf doc.

Here is how you find a method if you don't know its class.
Go to the API doc page. At the top of the page is a light blue bar. In that bar is the word Index.
Click on that to go to the index page. Click on the first letter of the method, here p.
Find the name of the method in the list on that page.
• July 18th, 2011, 03:04 PM
beer-in-box
Re: Enum types, how does he know %s value?
Oh, I think I got what I need. You say the letters are used for only one type of data.

But I couldn't understand how to find a method if I don't know its class.

(I am new to the whole thing, I figured I should read and comprehend the whole lessons more or less before I really get into the action. I will read all the lessons again once I am done for the first time. Reading once and practicing once doesn't seem to help. Am I right?)
• July 18th, 2011, 03:09 PM
Norm
Re: Enum types, how does he know %s value?
Quote:

But I couldn't understand how to find a method if I don't know its class.
Well now you know.

System.out.printf(...

Also You could find the class by looking in the System class for the out variable.
• July 18th, 2011, 03:11 PM
beer-in-box
Re: Enum types, how does he know %s value?
Actually, no. I don't :) I couldn't understand your explanation. But I guess I should read more before asking more questions about it.
By the way, thank you for your help.
• July 18th, 2011, 03:13 PM
Norm
Re: Enum types, how does he know %s value?
Here is a link to the API doc: Java Platform SE 6
• July 18th, 2011, 03:14 PM
beer-in-box
Re: Enum types, how does he know %s value?
I knew I overlooked something. Thank you for your patience and help :) I guess that solved my question completely now.
• September 17th, 2011, 12:47 PM
beer-in-box
Re: Enum types, how does he know %s value?
I learnt more about my question now. Those %s, f, d and n are formatters. You can use them in a Formatter object or in printf() method. %n creates a new line, and there is %% which adds a percentage sign.
To sum up,
%s for strings, %f for decimal floating numbers and %d for decimal integers.
They are used respectively. First %s matches with first argument.
Of course that is not all and if one wants to learn about it, he should read about Formatter.

Just wanted to add if someone needs to learn :)