Java Using Scanner to Read Dat File
#ane
reading a .dat file
Posted 22 October 2008 - 01:49 AM
Hey guys, I just needed a fleck of help reading a .dat file in java, basically I accept a grade with a constructor that needs to read information in a .dat file and put information technology into an arrayList. This is what I've done so far, I know its far from correct and only need assist getting it to work, I'thousand gathering from the compiler errors I'g getting I need to throw an exception, even so I have the faintest idea on how to do that.
import coffee.util.Scanner; import coffee.util.ArrayList; import coffee.io.*; /** * Write a description of class banking concern here. * * @author (your proper noun) * @version (a version number or a date) */ public class Depository financial institution { public ArrayList accounts; public Banking company() { ArrayList<Cord> accounts = new ArrayList<Cord>(); Scanner sc = new Scanner(new File("bank")); } }
This file should always exist with the java files so should i need to worry about user intervention when it deceit find a file (which should happen)
#2
Re: reading a .dat file
Posted 22 October 2008 - 01:58 AM
Yous don't demand to throw an exception - you need to catch one
In fact a constructor should not have to handle exceptions, so yous should brand a read() method or some such, e.thou.
public void read(String filename) throws IOException { // Read it accounts = new ArrayList<String>(); }
You can and then catch the exception in the calling cake. Note how the ArrayList should be initialized
This post has been edited by g00se: 22 October 2008 - 02:01 AM
#3
Re: reading a .dat file
Posted 22 October 2008 - 02:51 AM
g00se, on 22 Oct, 2008 - 01:58 AM, said:
You don't need to throw an exception - you need to catch 1
In fact a constructor should not have to handle exceptions, then you should make a read() method or some such, e.one thousand.
public void read(String filename) throws IOException { // Read it accounts = new ArrayList<Cord>(); }
You can then catch the exception in the calling block. Note how the ArrayList should exist initialized
Thats what I was thinking of, thanks.
#4
Re: reading a .dat file
Posted 22 October 2008 - eleven:17 PM
Hey I was working on this and the try/catch thing started making sense to me so I implimented that into the programme, would probably exist better than merely throwing the exception.
public class Banking company { public ArrayList accounts; public Banking concern()//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 institute, cheque if bank.dat is in the programme directory") } } }
However the take hold of statement is wrong, whats the right way to exercise it?
#v
Re: reading a .dat file
Posted 23 October 2008 - 04:28 AM
public course Bank { public ArrayList accounts; public Bank()//throws IOException { attempt { accounts = new ArrayList<String>(); Scanner sc = new Scanner(new File("depository financial institution")); while (sc.hasNextLine()) { accounts.add together(sc.nextLine()); } } catch (IOException east) { System.out.println("Error: " + east) } } }
#6
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 desire a ctor to exist in whatever style problematic. Supply a load() or read() method to load the file.
kinrosspressita74.blogspot.com
Source: https://www.dreamincode.net/forums/topic/68567-reading-a-dat-file/