Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Using an Input File and array

So I'm supposed to write a program using an input file (it contains a student name and 2 test scores) and having it write out that input using an array(the info varies with what key i press). I'm almost there but for example, every time I push p for it to print, it list the same name and score rather than listing the other students in the list. Help???

import java.util.Scanner;
import java.awt.FileDialog;
import java.io.*;


public class lab5 {

@SuppressWarnings("resource")
public static void main(String[] args) throws FileNotFoundException { 
    // TODO Auto-generated method stub

    FileDialog fd = null;
    fd = new FileDialog(fd, "Give me Exam Scores", FileDialog.LOAD);
    fd.setVisible(true);

    String fname = fd.getDirectory() + fd.getFile();
    Scanner reader = new Scanner(new File(fname)); 

/** try
    {
        reader = new Scanner(fileReader);       
    }
    catch (FileNotFoundException fnfe)
    {
        return;
    } **/


    Student student = new Student(reader);
    String lineInfo = student.toString();

    final int LENGTH = 10000;
    int currentSize = 0;
    String[] array = new String[LENGTH];

    while (reader.hasNext())
        {
            String nextName = reader.nextLine();

            if( currentSize < array.length)
            {
                array[currentSize] = nextName;
                currentSize++;
            }
        } 
    /**
    for (int i = 0; i < currentSize; i++)
    {
        System.out.println(array[i]);
    }
    **/

    Scanner inputForFile = new Scanner(System.in);
    boolean askAgain = true;
    System.out.println("Choose one of the following; Sort by name(n) or Sort by average(a) or Print(p) or Quit(q)");
    while(askAgain)
    {   
        String input = inputForFile.nextLine();
        switch(input)
        {
        case "n":
        {
            break;
        }
        case "a":
        {
            break;
        }
        case "p":
        {
            for (int i = 0; i < currentSize; i++)
            {
                System.out.println(lineInfo);
            }
            break;
        }
        case "q":
        {
            System.out.println("All Done! Thank you for using the Student Sorter!");
            break;
        }
        default:
            askAgain = false;

        }
    }

}

}

import java.util.Scanner;

public class Student {

private String name;  // the student's name
private int exam1;    // student's score on exam 1
private int exam2;    // student's score on exam 2

/**
 * COnstructs a student by reading their data from an open Scanner
 * 
 * @param s the scanner to read one student's data from
 */
public Student(Scanner s)
{
    // read in anem and exam scores from Scanner. 
    name = s.next();
    exam1 = s.nextInt();
    exam2 = s.nextInt();
}

/**
 * Gets the student's name
 * @return the student's name
 */
public String getName() 
{
    return name;
}

/**
 * Gets the student's score on the first exam
 * @return the student's first exam score 
 */
public int getExam1()
{
    return exam1;
}

/**
 * Gets the student's score on the second exam
 * @return the student's second exam score 
 */
public int getExam2()
{
    return exam2;
}

/**
 * Gets the student's average exam score
 * @return the student's average exam score 
 */
public double getAvg()
{
    return (exam1 + exam2)/2.0; // average is just the average of the two scores
}

/**
 * a string value representing the student
 */
public String toString()
{
    // add all student data to a string
    String result = String.format("%-20s exam1:%3d exam2:%3d avg:%5.1f",
                                   name, exam1, exam2, getAvg() );

    // return the string with all the data in it. 
    return result;
}

}

Comments