• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

formatting with printf

 
Ranch Hand
Posts: 90
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)"

 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79977
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
forgot the concatenation on previous post


(10)

----jGRASP: operation complete.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79977
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic