Results 1 to 6 of 6
  1. #1
    johnmergene is offline Member
    Join Date
    Jan 2011
    Location
    Baguio City, Philippines
    Posts
    21
    Rep Power
    0

    Unhappy Methods for reading file with an array

    can you help me, what is the possible methods, to read the .txt file that has an array...

    example of the .txt body.

    2113905,Arellano John,BSIT,1,Baguio City
    2113906,Joe Mark,BSIT,1,Baguio City
    2113907,Mark Kevin,BSIT,1,Baguio City

    -------------------------
    i would like to get the per-line, and then it will place to an array of temporary object.

    example

    String name = temp[1];
    String idNo = temp[2];
    String couse = temp[3];
    int year = temp[4];



    -----------------
    Thanks

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    What have you tried so far? I think Scanner and File will do.

  3. #3
    johnmergene is offline Member
    Join Date
    Jan 2011
    Location
    Baguio City, Philippines
    Posts
    21
    Rep Power
    0

    Default

    i used scanner, but the java said, SCANNER CLOSED...just give me a code...tnx...

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    this is not a give-me-a-code forum. You said have code, why dont you show it here. Everyone is
    willing to help here as long as we see you hard work.

    GoodLuck!

  5. #5
    johnmergene is offline Member
    Join Date
    Jan 2011
    Location
    Baguio City, Philippines
    Posts
    21
    Rep Power
    0

    Default

    ok..


    package sample1;

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

    public class SampleFileProcessingA {
    private Scanner reader;
    private StudentA[] studs;

    public SampleFileProcessingA() throws IOException{
    // To read the input file
    reader = new Scanner (new File("students.txt"));
    }

    // Read the first line of the text file as basis for the number
    // of elements for the array of Students to be declared
    public void run() throws Exception{
    Scanner scan = new Scanner(System.in);
    studs = new StudentA[Integer.parseInt(reader.nextLine())];
    readFile();
    byte choice=0;
    do{
    choice=readChoice();
    switch (choice){
    case 1:
    System.out.println("This program deals with students record.");
    System.out.println("You may choose to show on screen, \nsort and show, \ncheck if a name is included, or \nprint the records in a file.");
    break;
    case 2:
    showStudents(sortStudentsAccordingToNames(studs));
    break;
    case 3:
    showStudents(sortStudentsAccordingToAverage(studs) );
    break;
    case 4:
    System.out.println("Number of failing students: " +countFailures(studs));
    System.out.println("Number of Passing students: " + counts(studs, 75));
    break;
    case 5:
    System.out.print("Enter the name to be searched.");
    String searchKey = scan.nextLine();
    if (isFound(studs, searchKey))
    System.out.println(searchKey + " is in the database.");
    else
    System.out.println(searchKey + " is not in the database.");
    break;
    case 6:
    printDataToFile(studs);
    break;
    case 7: addingRecords();
    break;
    case 8: editRecords();
    break;
    case 9:
    System.out.println("Thank you.");
    System.exit(0);
    break;

    } //End of switch
    System.out.println("press enter to continue... ");
    scan.nextLine();
    } while (choice != 9);
    }

    // Read each of the records contained in the text file
    // and store them as elements of the array called studs




    public static void main(String[] args) throws Exception{
    SampleFileProcessingA test = new SampleFileProcessingA();
    test.run();
    }

    private int countFailures(StudentA[] s){
    int count=0;
    for (int x=0; x<s.length; x++){
    if (s[x].getAverage()< 75)
    count++;
    }
    return count;
    }

    private int counts(StudentA[] s, double limit){
    int count=0;
    for (int x=0; x<s.length; x++){
    if (s[x].getAverage()>= limit)
    count++;
    }
    return count;
    }

    private void showStudents(StudentA[] s){
    for (int x=0; x<s.length; x++) {
    System.out.println(s[x]); // toString() method is called automatically
    }
    return;
    }

    private StudentA[] copyArray(StudentA[] source){
    StudentA[] copiedArray = new StudentA[source.length];
    for (int x=0; x<source.length; x++){
    StudentA sCopy = new StudentA(source[x].getID(), source[x].getName(),source[x].getCourse(),source[x].getAverage(), source[x].getYear(), source[x].getAddress());
    copiedArray[x] = sCopy;
    }
    return copiedArray;
    }

    private StudentA[] sortStudentsAccordingToNames(StudentA[] array){
    StudentA[] s = copyArray(array);
    int minIndex=0;
    for (int x=0; x < s.length-1; x++){
    minIndex = x;
    for (int y=x+1; y<s.length; y++)
    if (s[minIndex].getName().compareToIgnoreCase(s[y].getName())>0)
    minIndex = y;

    if (minIndex != x){
    StudentA temp = s[x];
    s[x] = s[minIndex];
    s[minIndex] = temp;
    }
    }// end of first for
    return s;
    }

    private StudentA[] sortStudentsAccordingToAverage(StudentA[] array){
    StudentA[] s = copyArray(array);
    int minIndex=0;
    for (int x=0; x < s.length-1; x++){
    minIndex = x;
    for (int y=x+1; y<s.length; y++)
    if (s[minIndex].getAverage()<(s[y].getAverage()))
    minIndex = y;

    if (minIndex != x){
    StudentA temp = s[x];
    s[x] = s[minIndex];
    s[minIndex] = temp;
    }
    }// end of first for
    return s;
    }



    private void menu() throws Exception{
    System.out.println("------------MENU-------------");
    System.out.println("1. Show information about the program");
    System.out.println("2. Show Sorted Data Based on Names");
    System.out.println("3. Show Sorted Data According to Average");
    System.out.println("4. Show Number of Failing Students");
    System.out.println(" & Show Number of Passing Students");
    System.out.println("5. Check if a student is int the database");
    System.out.println("6. Print data into a file");
    System.out.println("7. Adding New Records");
    System.out.println("8. Editing fields of an existing record");
    System.out.println("9. Exit");
    }
    private byte readChoice() throws Exception{
    Scanner scan = new Scanner(System.in);
    byte choice = 0;
    while (choice < 1 || choice > 9) {
    menu();
    System.out.print("Enter the number corresponding to what you want to do. ");
    choice = Byte.parseByte(scan.nextLine());
    if (choice<1 || choice > 9){
    System.out.println("Invalid choice! You must integer an integer in the range 1 to 8.");
    } // end of if
    } // end of while
    return choice;
    }
    public void addingRecords(){

    }
    public void editRecords()throws Exception{
    Scanner reader = new Scanner(System.in);
    System.out.println("Enter Name of Student: ");
    String nameSearch = reader.next();
    int index = 0;
    String id = "";
    String name = "";
    String course = "";
    double average = 0;
    int year = 0;
    String address = "";
    while (reader.hasNextLine()){
    String[] temp = reader.nextLine().split(",");
    // Store the values to the following variables
    id = temp[0];
    name = temp[1];
    course = temp[2];
    average = Double.parseDouble(temp[4]);
    year = Integer.parseInt(temp[3]);
    address = temp[5];


    // Use the values in instantiating Student object
    studs[index++] = new StudentA(id, name, course, average, year, address);

    }
    reader.close();
    for (int x=0; x<studs.length; x++){
    if (studs[x].getName().equalsIgnoreCase(nameSearch))
    System.out.println("Need to Edit? ");
    System.out.println("1 - ID No.");
    System.out.println("2 - Name");
    System.out.println("3 - Course");
    System.out.println("4 - Year");
    System.out.println("5 - Grades");
    System.out.println("6 - Address");
    System.out.println("7 - Back");
    int choice2 = reader.nextInt();
    switch(choice2){
    case 1:
    System.out.println("Enter new Name: ");
    String newName = reader.next();
    studs[x] = new StudentA(id, newName, course, average, year, address);
    PrintWriter outFile = new PrintWriter(new FileWriter("outputA.txt"));
    for (int i = 0; i < studs.length; i++){
    outFile.println(studs[i]);
    }
    outFile.close();

    break;
    case 2:
    break;
    case 3:
    break;
    case 4:
    break;
    case 5:
    break;
    case 6:
    break;
    case 7:
    break;

    }

    }
    }
    public void printDataToFile(StudentA[] s) throws IOException{
    // To output records in an output file: output.txt
    PrintWriter outFile = new PrintWriter(new FileWriter("outputA.txt"));
    for (int i = 0; i < s.length; i++){
    outFile.println(s[i]);
    }
    outFile.close();
    }
    public void readFile(){
    int index = 0;
    while (reader.hasNextLine()){
    String[] temp = reader.nextLine().split(",");
    // Store the values to the following variables
    String id = temp[0];
    String name = temp[1];
    String course = temp[2];
    double average = Double.parseDouble(temp[4]);
    int year = Integer.parseInt(temp[3]);
    String address = temp[5];


    // Use the values in instantiating Student object
    studs[index++] = new StudentA(id, name, course, average, year, address);

    }
    reader.close();
    }
    private boolean isFound(StudentA[] s, String nameToSearch){
    boolean found = false;
    for (int x=0; x<s.length && !found; x++){
    if (s[x].getName().equalsIgnoreCase(nameToSearch))
    found = true;
    }

    return found;
    }

    }

  6. #6
    johnmergene is offline Member
    Join Date
    Jan 2011
    Location
    Baguio City, Philippines
    Posts
    21
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.IllegalStateException: Scanner closed
    at java.util.Scanner.ensureOpen(Scanner.java:1105)
    at java.util.Scanner.findWithinHorizon(Scanner.java:1 703)
    at java.util.Scanner.hasNextLine(Scanner.java:1535)
    at sample1.SampleFileProcessingA.run(SampleFileProces singA.java:54)
    at sample1.SampleFileProcessingA.main(SampleFileProce ssingA.java:151)

    - this is the one, that java said....can you fix it...plzzz///

Similar Threads

  1. Reading csv file into 2D array - HELP!!!
    By mikeg in forum New To Java
    Replies: 17
    Last Post: 04-12-2011, 09:36 AM
  2. Help with reading from file into an array
    By Trad in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 01:16 PM
  3. Reading a txt file and then storing it in a 2d array
    By blkshp1990 in forum New To Java
    Replies: 2
    Last Post: 11-05-2009, 01:31 AM
  4. Reading from a file to make an array
    By Bomber_Will in forum New To Java
    Replies: 11
    Last Post: 01-21-2009, 09:19 AM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •