Results 1 to 2 of 2
  1. #1
    crazycaitlin is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Angry Trying to add string to class- new to OOP

    I have the following class with the main program.. then at the bottom is the class I am having trouble implementing. I reading in values from a text file, then assigning their values to fields, but I need the file strings to be added to the Files constructor. Please advise!
    Java Code:
    import java.lang.Object;
    import java.util.*;
    import java.io.*;
    import java.util.Scanner;
    import java.lang.Integer;
    
    public class Allocation {
    
    	public static void main (String[] args) throws Exception {
    		
    		//read in HW_List.txt, will be duplicates of many sections
    		//<Student Name> <Section number> <Date submitted> <Number of files> <List of file names>
    		//student name= <Last Name> <First Name>
    		//section number = integer between 1 and 100.
    		//int dueDate=15;
    		//number of files integer between 0 and 100
    		Scanner sc = new Scanner(new File("HW_List.txt"));
    		
    		
    		String str;
    	    while ((str = sc.nextLine()) != null) 
    	    {
    			String lastName = sc.next();
    			String firstName = sc.next();
    			int sectionNumber = sc.nextInt();
    			int date = sc.nextInt();
    			int numberFiles = sc.nextInt();
    			
    			//Files[] filesArray= new Files[numberFiles];
    			String[] filesArray= new String[numberFiles];
    			for (int x=0; x<numberFiles; x++)
    			{
    				//String astring=sc.next();
    				filesArray[x]=sc.next();
    			}
    			Files[] newFiles= new Files[]{String[] filesArray};
    			
    			//print line describing hw to file HW_Allocation.txt Specifically, you should note whether each homework was or was not added to
    			//the queue for a given section
    		}
    
    	}
    
    }
    The Files class:

    Java Code:
    import java.util.*;
    
    /**
     * <p>A File class, representing the files submitted by a student as part of a homework submission.
     *  File submissions are <b>mutable</b> in the sense that after their creation, the files associated
     *  with a submission can be altered by adding files (however, files cannot be removed).</p>
     * 
     * <p>Multiple files of the same name can be kept in this list of files, and files are always kept
     *  in sorted order. An unbounded number of files may be added to this object.</p>
     *  
     *  @author Stephen Fulwider
     */
    public class Files implements Comparable<Files> {
    	
    	// ArrayList of files. Implemented as an ArrayList so the list can grow. Notice this member is
    	//	private, so the details of its implementation are hidden from the outside user.
    	private ArrayList<String> files;
    	
    	/**
    	 * Construct a new list of files. Initially constructs an empty list of files. You can add
    	 *  files with the the addFile() method.
    	 */
    	public Files() {
    		files=new ArrayList<String>();
    	}
    	
    	/**
    	 * Construct a new list of files. Initially the files contained are the files contained in the 
    	 *  array passed in.
    	 *  
    	 *  @param initFiles The initial files to construct for this list of files.
    	 */
    	public Files(String[] initFiles) {
    		files=new ArrayList<String>(initFiles.length);
    		for (String f : initFiles) {
    			files.add(f);
    		}
    	}
    	
    	/**
    	 * Add a supplied file name to this list of files. The list of files is kept in sorted order
    	 *  after this call.
    	 *  
    	 *  @param file The file to add to this list of files.
    	 */
    	public void addFile(String file) {
    		files.add(file);
    		Collections.sort(files);
    	}
    	
    	/**
    	 * Get the number of files contained in this files object.
    	 * 
    	 * @return The number of files in this Files object.
    	 */
    	public int getNumberOfFile() {
    		return files.size();
    	}
    	
    	/**
    	 * compareTo method that defines the <i>natural ordering</i> of Files. Fewer files are ranked
    	 *  before more files, and when the number of files are equal, the lists are compared
    	 *  lexicographically, file by file until a difference is found.
    	 *  
    	 *  @param other The other Files object to compare this Files object to.
    	 *  @return A negative value if this Files object should come first in sorted order, 0 if they
    	 *   are the same Files object, or a positive if this Files object should come after other in
    	 *   sorted order.
    	 */
    	public int compareTo(Files other) {
    		int z=files.size()-other.files.size();
    		for (int i=0; i<files.size() && z==0; ++i)
    			z=files.get(i).compareTo(other.files.get(i));
    		return z;
    	}
    	
    	/**
    	 * Return a String representation of these Files.
    	 * 
    	 * @return Returns the list of files, separated by spaces, enclosed in []'s.
    	 */
    	public String toString() {
    		String res="[";
    		for (int i=0; i<files.size(); ++i) {
    			if (i>0) res+=" ";
    			res+=files.get(i);
    		}
    		return res+"]";
    	}
    
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Trying to add string to class- new to OOP

    Your line 36:
    Java Code:
    Files[] newFiles= new Files[]{String[] filesArray};
    is pretty messed up.

    Why is Files an array? Shouldn't it be an object? Also, why are you using curly braces after it? Shouldn't you be using parenthesis? Finally, if you're trying to pass in a reference to the filesArray, why are you re-declaring it as a String[] array - haven't you done that above?

Similar Threads

  1. Help with passing String[] from GUI class to GUI class
    By X75TIGER75X in forum New To Java
    Replies: 9
    Last Post: 05-01-2012, 06:55 PM
  2. Replies: 3
    Last Post: 04-27-2012, 02:41 PM
  3. The String Class #2
    By Everyman in forum New To Java
    Replies: 3
    Last Post: 02-24-2012, 06:39 PM
  4. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  5. My own string class
    By viperlasson in forum New To Java
    Replies: 8
    Last Post: 01-25-2010, 05:55 PM

Posting Permissions

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