Results 1 to 4 of 4
  1. #1
    bogojevicka is offline Member
    Join Date
    Jan 2012
    Location
    Florida
    Posts
    2
    Rep Power
    0

    Question Calling args from main method

    Java Code:
    class Main {
    	public static void main(String[] args)throws FileNotFoundException{
    	   try{
    		  FileHandler fileHandler = new FileHandler();
    	   }
    	   catch(Exception e){
    		System.out.println("Exiting program... " + e);
               }
    	}//end main
    }//end class Main
    /************ FileHandler ************/
    class FileHandler{
    	private final int ARRSIZE = 100;
    	private HashFun[] hashArr;
    	private int size; 
    	
    	public FileHandler(){
    	   this.hashArr = new HashFun[ARRSIZE];
    		this.size = 0;
    	}
    	
    	public void createArray() throws IOException, NullPointerException{
    		 	BufferedReader reader;  
    			File file = new File(args[0]); //  <--- doesn't recognize args(offcourse) :smash:
    			reader = new BufferedReader(new FileReader(file));
                            String line = reader.readLine();//read in lines from file
            	
    			while ((line)!= null){
    				int index;
    				String name, number;
    				boolean hasBoth = false;
    				
    				try{
    					if((index = line.indexOf(" ")) != -1){
    						name = line.substring(0, index);
    						number = line.substring(index, line.length());
    						hasBoth = true;
    						HashFun hashObj = new HashFun(name, number);
    					}
    					else{
    						name = line;
    						HashFun hashObj = new HashFun(name);}
    					
    					hashArr[this.size] =hashObj;
    					size ++;
    				}//end try
    				catch(Exception e){
    					System.out.println("Following error occurred "+e);
    				}
    				line = reader.readLine();		
    			}//end while
    			reader.close();
    	}//end createArray()
    
    //aditional info: the text file suppose to contain names and numbers separated by space.
    Last edited by Norm; 01-16-2012 at 06:45 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Calling args from main method

    Do you have a question?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Calling args from main method

    Whatever the question, it certainly isn't 'advanced'.

    Moving to 'New to Java'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Calling args from main method

    You can pass args about like any other variable.

    It may well be that the FileHandler constructor should be passed this value so that when (if?) createArray() is called it will have access to the desired filename.

    There are alternatives: createArray() is public and if you intend calling it from outside the FileHandler then createArray() itself could be passed args. Or - and this is the most straightforward - pass createArray() the desired filename string.

Similar Threads

  1. Calling into the main method
    By vcardell in forum New To Java
    Replies: 4
    Last Post: 11-02-2011, 12:16 AM
  2. jump to args-field in Main method
    By argus in forum New To Java
    Replies: 7
    Last Post: 10-07-2010, 04:14 PM
  3. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  4. Calling main method
    By eva in forum New To Java
    Replies: 7
    Last Post: 11-06-2009, 01:37 PM
  5. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 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
  •