Javascript required
Skip to content Skip to sidebar Skip to footer

Java Using Scanner to Read Dat File

#ane

  • New D.I.C Caput

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


Is This A Good Question/Topic? 0

  • +

#2 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

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 jaxxy89 User is offline

  • New D.I.C Caput

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 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 jaxxy89 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 - 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 pbl User is offline

Reputation: 8381

  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

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 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • 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 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/