5 Replies - 26119 Views - Last Post: 23 October 2008 - 04:37 AM Rate Topic: -----

#1 jaxxy89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-September 08

reading a .dat file

Post icon  Posted 22 October 2008 - 01:49 AM

Hey guys, I just needed a bit of help reading a .dat file in java, basically I have a class with a constructor that needs to read information in a .dat file and put it into an arrayList. This is what I've done so far, I know its far from right and just need help getting it to work, I'm gathering from the compiler errors I'm getting I need to throw an exception, however I have the faintest idea on how to do that.

import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
/**
 * Write a description of class bank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bank
{
	public ArrayList accounts;
	
	public Bank()
	{
		ArrayList<String> accounts = new ArrayList<String>();
			Scanner sc = new Scanner(new File("bank"));
	} 
}



This file should always be with the java files so should i need to worry about user intervention when it cant find a file (which should happen)

Is This A Good Question/Topic? 0
  • +

Replies To: reading a .dat file

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: reading a .dat file

Posted 22 October 2008 - 01:58 AM

You don't need to throw an exception - you need to catch one

In fact a constructor should not have to handle exceptions, so you should make a read() method or some such, e.g.

public void read(String filename) throws IOException {
	// Read it
	accounts = new ArrayList<String>();  
}



You can then catch the exception in the calling block. Note how the ArrayList should be initialized

This post has been edited by g00se: 22 October 2008 - 02:01 AM

Was This Post Helpful? 1
  • +
  • -

#3 jaxxy89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-September 08

Re: reading a .dat file

Posted 22 October 2008 - 02:51 AM

View Postg00se, on 22 Oct, 2008 - 01:58 AM, said:

You don't need to throw an exception - you need to catch one

In fact a constructor should not have to handle exceptions, so you should make a read() method or some such, e.g.

public void read(String filename) throws IOException {
	// Read it
	accounts = new ArrayList<String>();  
}



You can then catch the exception in the calling block. Note how the ArrayList should be initialized


Thats what I was thinking of, thanks.
Was This Post Helpful? 0
  • +
  • -

#4 jaxxy89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-September 08

Re: reading a .dat file

Posted 22 October 2008 - 11:17 PM

Hey I was working on this and the try/catch thing started making sense to me so I implimented that into the program, would probably be better than just throwing the exception.

public class Bank
{
	public ArrayList accounts;
	
	public Bank()//throws IOException
	{
		try {
			accounts = new ArrayList<String>();
			Scanner sc = new Scanner(new File("bank"));
			while (sc.hasNextLine())	{
				accounts.add(sc.nextLine());
			}
		} catch (IOException) {
			System.out.println("Data file not found, check if bank.dat is in the program directory")
		}			 
	}	 
}


However the catch statement is wrong, whats the right way to do it?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: reading a .dat file

Posted 23 October 2008 - 04:28 AM

public class Bank
{
	public ArrayList accounts;
	
	public Bank()//throws IOException
	{
		try {
			accounts = new ArrayList<String>();
			Scanner sc = new Scanner(new File("bank"));
			while (sc.hasNextLine())	{
				accounts.add(sc.nextLine());
			}
	} catch (IOException e) {
		System.out.println("Error: " + e)
		}			 
	}	 
}

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: reading a .dat file

Posted 23 October 2008 - 04:37 AM

Your ctor should only initialize the collection and then complete. You don't want a ctor to be in any way problematic. Supply a load() or read() method to load the file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1