This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to format object array

 
Katherine Rose
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic