• Post Reply Bookmark Topic Watch Topic
  • New Topic

formatting with printf  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i print a variable in parenthesis.
i have a string variable gpa
passed it into a constructor
created a getter and setter for the gpa and a to string , now i have an object which is initialized... i think its easier to explain if i post my code.

in main i have
private static ArrayList<Student> students = new ArrayList<>();
students.add(new Student("CS",3.4));

now when i output the student object i get the expected output CS 3.4
but i want to have the literal string "CS" printed inside a parenthesis (CS) i will be adding more students to my array list and thought it might be cool if i dont have to worry about formatting the literal string
like this every time "(CS)"

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!
If you want to use printf to do the formatting, you will need to read the documentation of Formatter class: https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again Somebody seems to have corrected the code tags for you, and doesn't it look better.

You may find it easier to go through the Java™ Tutorials and search for formatting. I know of two pages which come up: 1 and 2 (look at second part).
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply, i figured it out. if i want a variable to be enclosed with a parenthesis, i can do this:
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forgot the concatenation on previous post


(10)

----jGRASP: operation complete.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick pine wrote:thanks for the reply, i figured it out. if i want a variable to be enclosed with a parenthesis, i can do this:


Really?!?!? Are you sure that that example even compiles?

[EDIT: Okay, so you fixed it. Never mind.]

Henry
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("(%6d)", i);
If you had problems you may have put the ( after the %, in which case it becomes one of the less well‑known options which you can read about in the Formatter documentation: try ctrl‑F→'('. I don't think that option is described in the Java™ Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!