This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to format object array" Watch "Trying to format object array" New topic
Author

Trying to format object array

Katherine Rose
Greenhorn

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
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
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: http://aspose.com/file-tools
 
subject: Trying to format object array
 
Similar Threads
NullPointerException
Java Array search and print
hashing problem
Help with array List
super class question