Results 1 to 5 of 5
  1. #1
    turnergirl24 is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default Multiple Command Line Arguments

    What I am trying to do is have the first argument pull from a file, the second argument is where the sorted file goes and the third argument lets you choose which criteria to sort by (1 = last name sort, 2 = first name sort, etc.). The problem is it continues to say File Not Found. I have the File located in my source path and it has worked with other programs. What is wrong that is it throwing this exception?? This is my run configuration arguments in eclipse: mysample.txt, sortedsample.txt, 2

    Java Code:
    import java.io.*;
    import java.util.*;
    
    
    public class Driver {
    
    	BufferedReader b1, b2;
    	PrintWriter p1;
    	Scanner scan;
    	Driver person;
    	Person [] personAry, outAry;
    	Person dude;
    
    	public static void main(String[] args) {
    		new Driver(args[0], args[1], args[2]);
    	}
    	
    	public Driver(String dbFile, String newFile, String sortBy) {
    		
    		String line, last, first, snum;
    		int lineCount = 1;
    		
    	try {
    			b2 = new BufferedReader(new FileReader(dbFile));
    			try {	
    				b2.readLine();
    				while (b2.readLine()!= null){
    					lineCount++;
    				}
    				b2.close();
    			
    			} catch (IOException e) {
    				System.out.println("Error reading from b2");
    				System.exit(1);
    			}  
    		
    			b1 = new BufferedReader(new FileReader(dbFile));
    			personAry = new Person [lineCount];
    			
    			try {
    				b1.readLine();
    				for (int i = 0; i < lineCount; i++){
    					line = b1.readLine();
    					scan = new Scanner (line);
    					last = scan.next();
    					first = scan.next();
    					snum = scan.next();
    					
    					dude = new Person(last,first,snum);
    					personAry[i] = dude;
    					
    					if (sortBy == "1"){
    						dude.setSortMethod(1);
    						//sort by last name
    					}else if (sortBy == "2"){
    						dude.setSortMethod(2);
    						//sort by first name
    					}else if (sortBy == "3"){
    						dude.setSortMethod(3);
    						//sort by ssn
    					}
    					
    				}
    				b1.close();
    				
    				} catch (IOException e) {
    					System.out.println("Error reading from b1");
    					System.exit(1);
    				} 
    
    			
    			
    		} catch (FileNotFoundException e) {
    			System.out.println("File Not Found");
    		    System.exit(1);
    		}
    		
    		//write the sorted personAry to a new file
    		try{
    		p1 = new PrintWriter (new FileWriter(newFile));
    		p1.flush();
    		p1.close();
    		} catch (IOException e) {
    			System.out.println("Error writing to p1");
    			System.exit(1);
    		} 
    		
    	}
    
    	
    	
    }

  2. #2
    turnergirl24 is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Oh and here is my person class in case it is part of the problem:

    Java Code:
    import java.util.*;
    
    
    
    class Person implements Comparable {
    	private String lastName, firstName, SSN;
    	public static final int last = 1, first = 2, snum = 3;
    	private static int sortBy = last;
    	
    	public Person(String l, String f, String s) {
    		lastName = l;
    		firstName = f;
    		SSN = s;
    	}
    	
    	public void setSortMethod(int method) {
    		if(method != last && method != first && method != snum) throw new IllegalArgumentException();
    		sortBy = method;
    	}
    	
    	public String getLast(){
    		return lastName;
    	}
    	
    	public String getFirst(){
    		return firstName;
    	}
    	
    	public String getSSN(){
    		return SSN;
    	}
    
    	public int compareTo(Object r) {
    		Person other = (Person)r;
    		int result = 0;
    		
    		if(sortBy == last) {
    			if (other.getLast().compareTo(((Person)r).getLast()) < 0) {
    				result = -1;
    			}else if (other.getLast().compareTo(((Person)r).getLast()) == 0){
    				result = 0;
    			}else if (other.getLast().compareTo(((Person)r).getLast()) > 0) {
    				result = 1;
    			}
    		}else if (sortBy == first) {
    			if (other.getFirst().compareTo(((Person)r).getFirst()) < 0) {
    				result = -1;
    			}else if (other.getFirst().compareTo(((Person)r).getFirst()) == 0){
    				result = 0;
    			}else if (other.getFirst().compareTo(((Person)r).getFirst()) > 0) {
    				result = 1;
    			}
    		}else if (sortBy == snum) {
    			if (other.getSSN().compareTo(((Person)r).getSSN()) < 0) {
    				result = -1;
    			}else if (other.getSSN().compareTo(((Person)r).getSSN()) == 0){
    				result = 0;
    			}else if (other.getSSN().compareTo(((Person)r).getSSN()) > 0) {
    				result = 1;
    			}
    		}
    		return result;
    	}
    
    }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    Don't do this:


    Java Code:
    } catch (FileNotFoundException e) {
        System.out.println("File Not Found");
        System.exit(1);
    }
    but do this instead:

    Java Code:
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.exit(1);
    }
    Then you'll see exactly what went wrong including the name of the file that could not be found.

    kind regards,

    Jos

  4. #4
    turnergirl24 is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Ok, I got the files to import, but now it continues to say I have a Null Pointer Exception at
    Java Code:
    try {
    				b1.readLine();
    				for (int i = 0; i < lineCount; i++){
    					line = b1.readLine();
    					[COLOR="Red"]scan = new Scanner (line);[/COLOR]
    What is wrong with that? I will still try and trace it back to see what is wrong, but any help would be appreciated!!

  5. #5
    turnergirl24 is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    I changed my i = 0 to i = 1 due to the fact that I already read one line and I think it goes past that and that is where the Null Pointer Exception came from. Now it goes through the process, but the new file is empty when I open it. Am I not writing to it?

Similar Threads

  1. Command Line Arguments
    By Nakira in forum NetBeans
    Replies: 10
    Last Post: 02-04-2010, 04:45 PM
  2. [SOLVED] command line arguments using IDE
    By sandeepsai39 in forum New To Java
    Replies: 5
    Last Post: 03-12-2009, 08:19 AM
  3. [SOLVED] Command Line Arguments and ParseInt
    By Sophiie in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 10:45 PM
  4. Printing command line arguments
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:27 AM
  5. Java Command Line Arguments In Eclipse IDE
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-19-2007, 10:45 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
  •