Completely Lost with this (Java)

Discussion in 'Mac Programming' started by Hylekmi, May 10, 2012.

  1. Hylekmi, May 10, 2012
    Last edited: May 10, 2012

    macrumors regular

    Joined:
    Dec 8, 2010
    #1
    UPDATED

    I am trying to read in a file(which was in the wrong place before) and store the contents into an array.Nothing outputs for some reason. Im new to exceptions btw.

     
  2. elppa, May 10, 2012
    Last edited: May 10, 2012

    macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #2
    This will give a number format exception:
    Code:
    integerEquivalence=Integer.parseInt(line);
    You can print out exceptions by doing this:
    Code:
    }catch (Exception e) {
        System.out.print(e);
    }
    You need to read each integer in from the line. One way of doing this is using a scanner:

    Code:
    Scanner scanner = new Scanner(line);
    Then in the loop:
    Code:
    array[row][col] = Integer.parseInt(scanner.next());
    There is not much error checking (for example - what happens if there are only 5 scores on the line?).

    I will leave you to improve it as much as you see fit.

    Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.Scanner;
    
    public class AssignmentFive {
    
        public static void main(String[] args) {
            int numOfRows = 30;
            int numOfColumns = 6;
            int[][] array = new int[numOfRows][numOfColumns];//30 rows 6 columns
    
            int row, col;
            int integerEquivalence;
            String line = null;
            try {
                FileReader fin = new FileReader("howework_scores.txt");
                BufferedReader buff = new BufferedReader(fin);
    
    
                for (row = 0; row < array.length; row++) {
    
    
                    line = buff.readLine();
                    
                    // integerEquivalence = Integer.parseInt(line);
                    Scanner scanner = new Scanner(line);
                    
                    for (col = 0; col < array[row].length; col++) {
                        array[row][col] = Integer.parseInt(scanner.next());
                    }
                }//end outer for loop
    
                for (row = 0; row < array.length; row++) {
                    System.out.print("Student #" + (row + 1) + ":\t");
                    for (col = 0; col < array[row].length; col++) {
                        System.out.print(array[row][col] + " ");
                    }
                    System.out.println();
                }
            } catch (Exception e) {
                System.out.print(e);
            }
        }//End Main
    }//end AssignmentFive
    
     
  3. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #3
    My issue is that I need it to print out the numbers in the text file into the array. The numbers in the text file are separated by a tab. the issue is to get the compiler to read all of the numbers into the array then output the array. i know i need to use the split method but i have no idea where to start
     
  4. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #4
    Yes, I'm not stupid.

    The code I have pasted above solves your issue. Run it and you will see.
     
  5. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #5
    I did and it just printed out the exception. not the array

    ----------

    Wait its a file not found exception
    Im using eclipse and its not finding the file for some reason. you wouldnt know if there is a special way to save the file in eclipse would you?

    ----------

    It does it in TextWrangler too.

    ----------

    im a Friggen idiot for the last 2 hours i had a w instead of a M
     
  6. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #6
    This is because Eclipse will compile your class to a different directory.

    The simplest thing to do for now is to put the full path to the file in:

    Code:
    /Users/loginname/Documents/howework_scores.txt
     
  7. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #7
    I cant do that becuase im only turning in the sourcecode and that would not work with the teachers file name.
     
  8. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #8
    Check it works that way.

    If you are only turning in the source code then the teacher will probably make sure the file is in the same place as the compiled java class.

    Unless you have been given instructions to the contrary.

    Compiling and running a project as simple as this outside of Eclipse is easy:
    Code:
    javac AssignmentFive.java
    To run:
    Code:
    java AssignmentFive
     
  9. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #9
    I got it after screwing around with the directories for a little bit. you just have to put it between the SRC and BIN files in the project folder
     
  10. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #10
    Works now?
     
  11. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #11
    Yes, but I need to try to get the program to work when i use the split class. Im close but if need be ill just turn in the other one.
     
  12. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #12
    I presume you mean the split method on the String class.

    Instead of:
    Code:
    Scanner scanner = new Scanner(line);
    Do this:
    Code:
    String[] scores = line.split("\t");
    Instead of:
    Code:
    array[row][col] = Integer.parseInt(scanner.next());
    Do this:
    Code:
    array[row][col] = Integer.parseInt(scores[col]);
     

Share This Page