Reputation: 351
I'm a little confused. Previously I posted a question: Conventional Programming: How do you return two Types of primitive ('int and 'string') from "get" method?
The answer it got was to use use String.format() with format specifiers (%s, %d, %f...)
.
And although the answer was a bit confusing, I understand that %f means to convert (?)
But how do you print out age
and name
using this method. My attempt:
// DISPLAY Person name and age
public String displayProfile() {
return String.format(%fgetAge(), %fgetName())
}
Main method from which above method gets executed:
if (optionSelected == 2) {
System.out.println(zac.displayProfile());
}
I'm all over the place with this, any help would be appreciated.
UPDATE: As requested,I'm posting the get and set methods used with the String.format() method. Because although I'm not getting any errors after the correcting the String.format method, it not printing out the 'name' and 'age' after I return the getAge() and GetName() from String.format(). What is the mistake I'm making any ideas?
// SET NAME
public void setName(String name) {
this.name = name;
}
// GET NAME
public String getName() {
return this.name;
}
// SET AGE
public void setAge(int age) {
this.age = age;
}
// GET AGE
public int getAge() {
return this.age;
}
Upvotes: 1
Views: 7238
Reputation: 1
String birinchison = JOptionPane.showInputDialog("Birinchi sonni kiriti");
String ikkinchiSon = JOptionPane.showInputDialog("Birinchi sonni kiriti");
int raqam1= Integer.parseInt(birinchison);
int raqam2 =Integer.parseInt(ikkinchiSon);
int yigindi = raqam1+raqam2;
JOptionPane.showMessageDialog(null ,"ularni yigindisi" + yigindi , "Sarlavha" , JOptionPane.INFORMATION_MESSAGE);
enter code here
Upvotes: 0
Reputation:
The format specifiers and functions would need to be separate so the specifiers are in the string and the functions need to be called after the string:
public String displayProfile() {
return String.format("%d, %s", getAge(), getName());
}
Upvotes: 2
Reputation: 5236
You can use String.Format()
as:
public String displayProfile() {
return String.format("Age: %f, Name: %s", getAge(), getName());
}
Where %f
stands for a floating-point type and %s
for String type. I don't think you will need floating points for representing Age
. I'd rather use int and use %d
in order to format.
You can read further on formatting here
Upvotes: 0
Reputation: 966
You'd need to make that:
public String displayProfile() {
return String.format("%d %s", getAge(), getName())
}
The format method takes several arguments, the first is always the string to be formatted. All other are the variables which replace the placeholders in the first string. So what you are basically doing here is telling java to print you a string ("%d %s"
) which has two placeholders (one for integers, one for strings) separated by a single space character. Then you supply the values for your placeholders, from left to right in the same order.
Also I'd strongly suggest that you start browsing through http://docs.oracle.com/javase/tutorial/ or some other tutorial.
Upvotes: 0