Results 1 to 3 of 3

Thread: Cleaning up

  1. #1
    DGates is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Post Cleaning up

    Hi,
    This is sort of related to an earlier post that I made.
    The only way I could think of using an array in multiple places in my program was to repeat code. I was lead to believe this is bad practice however.
    If you look below you can see what I have done.
    Does anyone know of a way to reduce this code re-usage?
    Any help is appreciated,
    Thanks,
    Dan



    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            String [] a = new String [17];
            String [] b = new String [17];
            String [] c = new String [17];
            String [] d = new String[17];
            String [] e = new String[17];
            String[] f = new String[17];
            String [] g = new String [17];
            String[] h = new String[17];
            String[] i = new String[17];
            String[] j = new String[17];
            String[] k = new String[17];
            String[] l = new String[17];
            String[] m = new String [17]; 
    
            String [] input = new String [17];
            File file = new File("Data.txt");
    
            int line = 0; 
            
        try {     
               Scanner in = new Scanner(file);
               
               in.useDelimiter("\\t|\\n");
            int i;
            
             for (line =0; line<17; line++){
                
                 for (i=0; i < 13; i++) {
                        input[i] = in.next();
                        //System.out.println(input[i]);
                 }
    
                        a [line] = input [0];
                        b [line] = input [1];
                        c [line] = input [2]; 
                        d [line] = input [3];
                        e [line] = input [4];
                        f [line] = input [5];
                        g [line] = input [6];
                        h [line] = input [7];
                        i [line] = input [8];
                        j [line] = input [9];
                        k [line] = input [10];
                        l [line] = input [11];
                        m [line] = input [12];
                    
                
            }
        }
           catch (FileNotFoundException e) {
               System.out.println("No File");
                e.printStackTrace();
           }
            catch (NullPointerException npe){
                System.out.println("A number may not have been initialized");
            }
        System.out.println(a[4]);
        
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            
        
            String [] a = new String [17];
            String [] b = new String [17];
            String [] c = new String [17];
            String [] d = new String[17];
            String [] e = new String[17];
            String[] f = new String[17];
            String [] g = new String [17];
            String[] h = new String[17];
            String[] i = new String[17];
            String[] j = new String[17];
            String[] k = new String[17];
            String[] l = new String[17];
            String[] m = new String [17]; 
    
            String [] input = new String [17];
            File file = new File("Data.txt");
    
            int line = 0; 
            
        try {     
               Scanner in = new Scanner(file);
               
               in.useDelimiter("\\t|\\n");
            int i;
            
             for (line =0; line<17; line++){
                
                 for (i=0; i < 13; i++) {
                        input[i] = in.next();
                        //System.out.println(input[i]);
                 }
    
                        a [line] = input [0];
                        b [line] = input [1];
                        c [line] = input [2]; 
                        d [line] = input [3];
                        e [line] = input [4];
                        f [line] = input [5];
                        g [line] = input [6];
                        h [line] = input [7];
                        i [line] = input [8];
                        j [line] = input [9];
                        k [line] = input [10];
                        l [line] = input [11];
                        m [line] = input [12];
                    
                
            }
        }
           catch (FileNotFoundException e) {
               System.out.println("No File");
                e.printStackTrace();
           }
           catch (NumberFormatException nfe){
               System.out.println("A number is of wrong type");
               nfe.getCause();
           }
            catch (NullPointerException npe){
                System.out.println("A number may not have been initialized");
            }
        
                    System.out.println(a[1]);
                    
                    
                    Arrays.sort(a);
                    for (int i = 0; i < x.length; i++){
                        System.out.println(a[i]);
             }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    DGates is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Cleaning up

    It reads a tab delimited file, and stores the information in arrays. It works fine.
    The System.out functions are just testing it's output they are sort of irrelevant.

Similar Threads

  1. Cleaning up my linked list
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 03-07-2009, 07:53 AM
  2. Cleaning out the closet
    By Norm in forum Java Software
    Replies: 6
    Last Post: 10-31-2008, 01:18 PM
  3. Cleaning txt file
    By ONS in forum New To Java
    Replies: 9
    Last Post: 10-10-2008, 11:50 PM
  4. code for cleaning variables?
    By Ak-Emm in forum New To Java
    Replies: 13
    Last Post: 08-22-2008, 03:40 AM

Posting Permissions

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