9 Replies - 691 Views - Last Post: 30 October 2012 - 01:13 PM Rate Topic: -----

#1 Bigredaltoid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-October 12

Method for reading Student information into an array?

Posted 30 October 2012 - 11:26 AM

Hi all - first time here. I am working on an assignment and I can't seem to get my head around how to do. I have a program which opens a file as the main method, a second method creates an array with null values based on an integer that it reads in from the first line of a file using the scanner class. The third method is supposed to supposed to skip the first line, and read in the First Name, Last Name, and the Average of three test scores from the file.

An example of the file would look like so:
2

Joe Shmo
90
85
74

Bob Bobby
67
99
87

The method that creates the null array is so:

public static Student[] createStudentArray(Scanner sc) {
		int arraySize = sc.nextInt();
		Student[] studentArray = new Student[arraySize];
		for(int i = 0; i < studentArray.length; i++){
			studentArray[i] = null;
			}
		
		
		return studentArray;
		}


A small snippet of the Student object is:

public Student(String firstName, String lastName, double average) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.average = average;


What I have thus far is..
public static void populateStudentArray(Student[] studentArray, Scanner sc) {
		Student student = studentArray[0];
		for(int i = 0; i < studentArray.length; i++){
			
		}
		
		
	}


I can't get how to skip the first line, and to read the first and last name in as Strings while calculating the average of their test scores into the array. Any advice would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Method for reading Student information into an array?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Method for reading Student information into an array?

Posted 30 October 2012 - 12:18 PM

Array of Object have all their elements initialized to null so need to use a for() loop in your first method to init to null all the elements of the Student array

You already have created your Sudent array filled with null. So this statement will assign null to student ???
Student student = studentArray[0];
you simply need

for(int i = 0; i < studentArray.length; i++){  
    ... read name form file
    ... read data from file
    ... compute avearag
    studentArray[i] = new Student(name, ....
         }  


This post has been edited by pbl: 30 October 2012 - 12:25 PM

Was This Post Helpful? 1
  • +
  • -

#3 Bigredaltoid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-October 12

Re: Method for reading Student information into an array?

Posted 30 October 2012 - 12:36 PM

Am I on the right track with this?

for(int i = 0; i < studentArray.length; i++){
			String firstName = sc.nextLine();
			String lastName = sc.nextLine();
			double testscore1 = sc.nextDouble();
			double testscore2 = sc.nextDouble();
			double testscore3 = sc.nextDouble();
			double average = (testscore1 + testscore2 + testscore3)/3;
			studentArray[i] = new Student(firstName, lastName, average);
			}


Also, will the blank lines between the student data groups effect the looping process? Do they need to be ignored in someway?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Method for reading Student information into an array?

Posted 30 October 2012 - 12:42 PM

First name and Last name are on the same line. You will have to use scanner.next() rather than scanner.nextLine()

After having read your 3 scores, you will have to scanner.nextLine() twice to read the end of line after the last double and to read the blank line in your file
Was This Post Helpful? 1
  • +
  • -

#5 Bigredaltoid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-October 12

Re: Method for reading Student information into an array?

Posted 30 October 2012 - 12:56 PM

My apologies, after reviewing the file the first name and last name are on different lines.

This is what I have:

for(int i = 0; i < studentArray.length; i++){
			String firstName = sc.nextLine();
			String lastName = sc.nextLine();
			double testscore1 = sc.nextDouble();
			double testscore2 = sc.nextDouble();
			double testscore3 = sc.nextDouble();
			sc.nextLine();
			sc.nextLine();
			double average = (testscore1 + testscore2 + testscore3)/3;
			studentArray[i] = new Student(firstName, lastName, average);
			}
			


I am still concerned about skipping the first line in the program. Is there something I need to code to skip the first line with the integer that gets read in with the other method and to skip the first blank line, or does using String firstName = sc.nextLine(); take care of skipping the integer and blank line?

Sorry, I meant skipping the first line in the file, not program.

Actually, would this work for skipping the first two lines and than scanning through the information? I think this way it skip to the end of the double and the blank line at the beginning of the next iteration..is that correct?

for(int i = 0; i < studentArray.length; i++){
			sc.nextLine();
			sc.nextLine();
			String firstName = sc.nextLine();
			String lastName = sc.nextLine();
			double testscore1 = sc.nextDouble();
			double testscore2 = sc.nextDouble();
			double testscore3 = sc.nextDouble();
			double average = (testscore1 + testscore2 + testscore3)/3;
			studentArray[i] = new Student(firstName, lastName, average);
			}

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2640
  • View blog
  • Posts: 11,155
  • Joined: 20-September 08

Re: Method for reading Student information into an array?

Posted 30 October 2012 - 12:58 PM

Personally i would store all three scores for each Student and average them afterwards. It's unfortunate that you have blank lines in the file - it makes it error-prone and harder to parse
Was This Post Helpful? 0
  • +
  • -

#7 Bigredaltoid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-October 12

Re: Method for reading Student information into an array?

Posted 30 October 2012 - 01:06 PM

I figured it out and the output it printing what it supposed to. I am probably missing something small here, but how can I get the output to be on separate lines?

The method is simple enough...

public static void printArray(Student[] studentArray) {
		for(int i = 0; i < studentArray.length; i++){
			System.out.println(studentArray[i]);
		}


But the output is:
firstname
lastname
average
firstname
lastname
average


I would like to make it
firstname lastname average
firstname lastname average
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Method for reading Student information into an array?

Posted 30 October 2012 - 01:06 PM

add a scanner.nextLine(); after having read the number of Student

put a scanner.nextLine() just after the for() statement before reading the name
and just one scanner.nextLine() after hafing read the last double
Was This Post Helpful? 1
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2640
  • View blog
  • Posts: 11,155
  • Joined: 20-September 08

Re: Method for reading Student information into an array?

Posted 30 October 2012 - 01:08 PM

Make Student.toString return the info in one line


public String toString() {
  return String.format("%s %s %.2f", firstName, lastName, average);
}

This post has been edited by g00se: 30 October 2012 - 01:10 PM
Reason for edit:: code

Was This Post Helpful? 1
  • +
  • -

#10 Bigredaltoid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-October 12

Re: Method for reading Student information into an array?

Posted 30 October 2012 - 01:13 PM

I truly do appreciate the helpfulness of this community. I am new to Java and am struggling a little bit with methods and arrays it really helps me to understand more about them and further my own knowledge. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1