java help

Discussion in 'OT Technology' started by jason612, Nov 24, 2008.

  1. jason612

    jason612 OT Supporter

    Joined:
    Sep 7, 2007
    Messages:
    1,257
    Likes Received:
    0
    Location:
    NJ
    I have to make a program that reads in a file of a grade book
    Code:
    Berra 90 76 54 89 98 93 100
    Blanchard 78 98 67 87 89 98 88
    Boyer 100 98 94
    Howard 99 97 96 90 91 92 93
    Kubek 83 85 83 89 88 99 60
    Mantle 60 70 80 90 100 100 100
    Maris 100 99 98 97 96 95 94
    Richardson 75 77 79 83 85 99 81
    Skowron 68 78 88 98 98 99 93
    
    like this one here where it has a last name and then the grades the students got on the assignment.
    The output is just an output of the grades only and if a student does not have a grade for every assignment like how boyer only has 3 grades while everyone else has 7 gradse, it just ignores their grades in the output:
    Code:
    90 78 99 83 60 100 75 68
    76 98 97 85 70 99 77 78
    54 67 96 83 80 98 79 88
    89 87 90 89 90 97 83 98
    98 89 91 88 100 96 85 98
    93 98 92 99 100 95 99 99
    100 88 93 60 100 94 81 93
    
    Well basically im having trouble how to first read each line of the file in order to count what the max amount of grades there is so i could create an array of that size and how to read how many lines there are so i could figure out how many students there are

    this is what i have so far
    Code:
    import java.util.Scanner;
    import java.io.*;
    import java.io.FileNotFoundException;
    
    public class PA5 {
      public static void main(String args[]){
        //Creates Scanner
        Scanner inputfile = null;
        try {
          inputfile = new Scanner(new FileInputStream("PA5input.txt"));
        }
        catch (FileNotFoundException e) {
          System.out.println ("Could not open the file");
          System.exit (0);
        }
        PrintWriter outputStream = null;
        try {
          outputStream = new PrintWriter (new FileOutputStream ("PA5output.txt"));
        }
        catch (FileNotFoundException e) {
          System.out.println ("Could not open the file");
          System.exit (0);
        }
        
        String numb[][];
        int row = 0, col = 0, maxstud = 10, maxassign = 10;
        for(row = 0, row < maxstud, row++){
          for(col = 0, col < maxassign, col++){
            numb[row][col] = inputfile.nextLine();
            if(inputfile.hasNext() == false){
              maxassign = col + 1;
              maxstud = row + 1;
            }
          }
        }
        
        
      }
    }
    
     
    Last edited: Nov 24, 2008
  2. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    Each line read in is a string... Then use a string splitter and split on the spaces for your array.
     
  3. jason612

    jason612 OT Supporter

    Joined:
    Sep 7, 2007
    Messages:
    1,257
    Likes Received:
    0
    Location:
    NJ
    thanks that helps me find out how many students there are and assignments there are, but im having trouble assigning the values from string splitting to a 2d array. this is what i have so far
    Code:
    import java.util.Scanner;
    import java.io.*;
    import java.io.FileNotFoundException;
    
    public class PA5 {
      public static void main(String args[]){
        //Creates Scanner
        Scanner inputfile = null;
        try {
          inputfile = new Scanner(new FileInputStream("PA5input.txt"));
        }
        catch (FileNotFoundException e) {
          System.out.println ("Could not open the file");
          System.exit (0);
        }
        PrintWriter outputStream = null;
        try {
          outputStream = new PrintWriter (new FileOutputStream ("PA5output.txt"));
        }
        catch (FileNotFoundException e) {
          System.out.println ("Could not open the file");
          System.exit (0);
        }
        String line;
        int stud = 0;//counter for how many students there are
        int assign, assign2 = 0;
        while(inputfile.hasNext()){
         line = inputfile.nextLine();
         String[] grades = line.split(" ", /t);
         assign = grades.length;
         if(assign > assign2)
           assign2 = assign;
         stud++;
        }
        int assign3 = assign2 - 1; // Amount of assignments there are excluding the last name
        for(int i = 0; i < stud; i++){
          for(int u = 0; u < assign3; u++){
            gradebook[i][u] = grades[u];
          }
        }
      }
    }
    
     
  4. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    whats he problem tho? Errors?
     

Share This Page