Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-04-2009, 08:00 PM
Member
 
Join Date: Oct 2009
Posts: 7
Rep Power: 0
turnergirl24 is on a distinguished road
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

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);
		} 
		
	}

	
	
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-04-2009, 08:03 PM
Member
 
Join Date: Oct 2009
Posts: 7
Rep Power: 0
turnergirl24 is on a distinguished road
Default
Oh and here is my person class in case it is part of the problem:

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;
	}

}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-04-2009, 09:23 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,139
Rep Power: 3
JosAH is on a distinguished road
Default
Don't do this:


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

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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-04-2009, 10:32 PM
Member
 
Join Date: Oct 2009
Posts: 7
Rep Power: 0
turnergirl24 is on a distinguished road
Default
Ok, I got the files to import, but now it continues to say I have a Null Pointer Exception at
Code:
try {
				b1.readLine();
				for (int i = 0; i < lineCount; i++){
					line = b1.readLine();
					scan = new Scanner (line);
What is wrong with that? I will still try and trace it back to see what is wrong, but any help would be appreciated!!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-04-2009, 10:36 PM
Member
 
Join Date: Oct 2009
Posts: 7
Rep Power: 0
turnergirl24 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command Line Arguments Nakira NetBeans 10 02-04-2010 04:45 PM
[SOLVED] command line arguments using IDE sandeepsai39 New To Java 5 03-12-2009 08:19 AM
[SOLVED] Command Line Arguments and ParseInt Sophiie New To Java 4 11-16-2008 10:45 PM
Printing command line arguments Java Tip Java Tips 0 12-03-2007 10:27 AM
Java Command Line Arguments In Eclipse IDE JavaForums Eclipse 0 05-19-2007 10:45 AM


All times are GMT +2. The time now is 08:16 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org