• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

help with array of objects

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need help this is what the question is asking:

Write a program that reads in (possibly from a file) a set of Students name and GPA and outputs the name and GPA of the student having the highest GPA. The number of students will be given as an input.
For example, for the following input data:
5
Ali  3.5
Abdullah  2.3
Ahsan  4.0
Mohammed  3.8
Ahmed  2.8

The output is:
Ahsan  4.0
-
what i'm trying to do is create a class with array variables and then from the main get the values from the file and then send them to the class but it wont work
it is my first time doing array of objects so this is really bad

main:


class:




 
Saloon Keeper
Posts: 8578
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The 's' array defined on line 18 should be the ONLY array you need. You went a bit overboard turning all your variables into arrays. 'name' for instance is not an array it is a member of the Student class and each Student only has one name. 's' however does hold several Students.
 
Marshal
Posts: 74004
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is something familiar about that assignment; I think somebody else has done the same assignment recently on this forum.
You are not far off; you can fill the array with only sloght changes.
I am afraid, however, you aren't opening anc closing the file correctly. Also File is regarded as legacy code. This part of the Java™ Tutorials tells you the best way to close a Scanner (never close a Scanner reading System.in), to make absolutely sure the file is closed.Notice you don't have to call close(). I would prefer to use NIO2 code (import java.nio.file.Paths):-All this code should be in a class by itself, in separate methods. You have far too much code in the main() method.
You can populate the array in a loop; you should create a Student object and put that into the array; you should most definitely not create parallel arrays, which are very error‑prone. You only need a small change in your loop to get everything working.
 
reply
    Bookmark Topic Watch Topic
  • New Topic