Results 1 to 3 of 3

Thread: ending a csv

  1. #1
    paul1024's Avatar
    paul1024 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default ending a csv

    My problem with my CSV file is that the last entry has a comma and nothing after it. So when my scanner reads the file it is expecting an element after the comma.

    file:

    Java Code:
    John,Doe,123 way dr,rocky mount,sc,29526,
    output:

    Java Code:
    First Name          Last Name           Address             City                State               Zip                 
    John                Doe                 123 way dr          rocky mount         sc                  29526               
    Exception in thread "main" java.util.NoSuchElementException
    	at java.util.Scanner.throwFor(Scanner.java:855)
    	at java.util.Scanner.next(Scanner.java:1364)
    	at AddressBook.printAddrBk(AddressBook.java:181)
    	at Main.main(Main.java:29)
    Here is the print method:

    Java Code:
    public void printAddrBk() throws FileNotFoundException
    	{
                    Scanner input = new Scanner( new File("addresses.txt"));
    		input.useDelimiter(","); //changes default delimiter from whitespace to commas
    		
    		System.out.printf("%-20s%-20s%-20s%-20s%-20s%-20s\n", "First Name", "Last Name", "Address", "City", "State", "Zip");
    		while(input.hasNext())
    		{
    	       
    			//parse the first record
    			setFname(input.next());
    			setLname(input.next());
    			setAddress(input.next());
    			setCity(input.next());
    			setState(input.next());
    			setZip(input.next());
    			
    			
    			
    			//display record
    			System.out.printf("%-20s%-20s%-20s%-20s%-20s%-20s\n", getFname(), getLname(), getAddr(), getCity(), getState(), getZip());
    			
    			
    		}
    }
    Write method:

    Java Code:
    public void writeToDisk() throws IOException
    	{
    		//write the address information to a file in CSV format. Param1 is the path and Param2 means we want to append to the end of the file (in case something is already there)
    		FileWriter write = new FileWriter("./addresses.txt", true);
    		
    		//this will print characters instead of bytes
    		PrintWriter print_line = new PrintWriter(write);
    		
    	   //printing to file as a csv
    	    print_line.print(fName + ",");
    	    print_line.print(lName + ",");
    	    print_line.print(address + ",");
    	    print_line.print(city + ",");
    	    print_line.print(state + ",");
    	    print_line.println(zip + ","); 
    	   
    	   
    	    //close the file
    		write.close();
    		print_line.close();
    	}
    Last edited by paul1024; 05-03-2011 at 06:24 PM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Sooooo... remove the last comma from your writeToDisk method?

  3. #3
    paul1024's Avatar
    paul1024 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    That was obvious, but I wasnt sure how to do it. This is what I ended up doing:

    Java Code:
     //printing to file as a csv
    	    print_line.print(","+fName + ",");
    	    print_line.print(lName + ",");
    	    print_line.print(address + ",");
    	    print_line.print(city + ",");
    	    print_line.print(state + ",");
    	    print_line.println(zip);

Similar Threads

  1. Problem with Ending Craps game
    By Ebayer in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 08:17 PM
  2. ending a graphics class
    By imorio in forum AWT / Swing
    Replies: 11
    Last Post: 08-16-2010, 06:41 PM
  3. [SOLVED] Ending for loop with a String
    By jdiesel in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 03:11 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
  •