File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Trying to format object array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to format object array" Watch "Trying to format object array" New topic

Trying to format object array

Katherine Rose

Joined: Oct 03, 2012
Posts: 1
I'm trying to format the output where the name will occupy 25 columns and the GPA will occupy 10 and be displayed with one digit of precision in the system console in addition to outputting each student info in a message dialogue box. I am also trying to generate a random that will chose information for a student to be excused for a final exam and output it to the console.

Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

Please UseCodeTags when posting your code.

I must ask why would you "mix" your application, so part of it uses Swing and part of it interacts with user over console? That's very bad approach in this case. Either choose first one and develop GUI application, or another and interact with user over console.

First of all, your default constructor in Student class is wrong. You are just declaring new variables (though with the same name as class fields) that are local to the constructor block, and your fields are left uninitialized.
On the other hand, to achieve output formatted as you want it to be, take a look at this tutorial. Also, check the API of Random class that you need for your last requirement.

Edit: And welcome to the Ranch!

The quieter you are, the more you are able to hear.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
Agree with KS. I would say you ought to get rid of that no‑arguments constructor altogether. There is no such thing as a Student called Last. First, and even if there were they would never get a 4.0gpa. So you don’t want that constructor. You want to insist every Student have a name and gpa? All, right, only supply a constructor which takes name and gpa parameters.
And welcome again

I added code tags, which makes your code look a lot better, but highlights some indenting mistakes. Remind yourself about how to indent here or here (but not both; they are not mutually compatible).
I agree. Here's the link:
subject: Trying to format object array
It's not a secret anymore!