Trouble with input from text file!

Trouble getting input from text file in proper format

Page 1 of 1

4 Replies - 1470 Views - Last Post: 22 March 2009 - 02:51 PM Rate Topic: -----

#1 NerdPirate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-January 09

Trouble with input from text file!

Post icon  Posted 21 March 2009 - 11:01 PM

This is the section of code that is giving me trouble. Basically I have a text file that has a music CD inventory. Each line of the text file is a different CD. On each line is the title, year released, money value, and artist. I am storing each CD as an object of the CD class. I am storing the entire inventory as an array of CD objects. To create an object of the CD class I need to input the above info. Since parts of the line are strings and other parts need to be a double or int, how do I separate the line into the pieces I need and in the proper format? I am getting compile errors telling me about throwing ParseExceptions and such. I don't really know how to handle exceptions. I am at my whit's end and I'd appreciate some input. If you know an easier way to accomplish this could you please help me?



String name;
		int year;
		double price;
		String artist;

		Scanner scanCDS = new Scanner(new File(fileName));
		Scanner scanCDS1 = new Scanner(new File(fileName));

		scanCDS.useDelimiter("\n");
		scanCDS1.useDelimiter("\t");

		while(scanCDS.hasNext())
		{
			name = scanCDS1.next();
			year = fmt1.parse(scanCDS1.next()).intValue();
			price = fmt.parse(scanCDS1.next()).doubleValue();
			artist = scanCDS1.next();
			libraryCD[indexCDS] = new CD(name, year, price, artist);

			indexCDS++;
		}

This post has been edited by NerdPirate: 22 March 2009 - 02:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with input from text file!

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Trouble with input from text file!

Posted 21 March 2009 - 11:43 PM

See the String Class . Specifically, the split() method.

Instead of parsing the input from the file, read each line into a variable
and String[] str = variable.split("\t"); into a string array[]. Each element of the array will be one of the fields of information in the line if you split() the line using the tab character as the delimiter. (This assumes the file data lines use tabs between all the fields.)

Then you can parseDouble(), parseInt(), whatever on each field separately.

Instead of scanner(), try BufferedReader and it's readLine() method.

This post has been edited by n8wxs: 21 March 2009 - 11:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 cmaster  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 53
  • Joined: 18-November 08

Re: Trouble with input from text file!

Posted 21 March 2009 - 11:48 PM

Can you, please, show us the text file. Also, I see at least two possible inaccuracies:

1) You are using two Scanner objects. Why? Try to use one.
2) Instead of parsing, use build-in Scanner methods: nextInt(), nextDouble() and so on.

View Postn8wxs, on 21 Mar, 2009 - 10:43 PM, said:

Instead of scanner(), try BufferedReader and it's readLine() method.


I disagree! Scanner is much more simple in use, than manual parsing.
Was This Post Helpful? 0
  • +
  • -

#4 saumya9888  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 08-August 08

Re: Trouble with input from text file!

Posted 22 March 2009 - 12:13 AM

Why don't you use object input and output streams its really convenient and it does all parsing and stuff itself...
Don't forget to implement Serializable in the CD class
i mean
class CD implements Serializable

and then user objectinputstream and objectoutputstream to read and write objects...
This is one example i've implemented in one of my projects...
import java.io.Serializable;
import java.net.InetAddress;

/**
 *
 * @author Saumya
 */
public class ComLang implements Serializable{
	public int operation;
	public InetAddress addr;
	public int port;
	public String ufname;
	public long filesize;
	public ComLang(){
		operation=0;
		addr=null;
		port=0;
		ufname=null;
		filesize=0;
	}
	public ComLang(int operation,InetAddress addr,int port,String ufname){
		this.operation = operation;
		this.addr = addr;
		this.port = port;
		this.ufname = ufname;
	}
	public ComLang(int operation,InetAddress addr,int port,String ufname,long filesize){
		this.operation = operation;
		this.addr = addr;
		this.port = port;
		this.ufname = ufname;
		this.filesize = filesize;
	}
}

Was This Post Helpful? 0
  • +
  • -

#5 NerdPirate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-January 09

Re: Trouble with input from text file!

Posted 22 March 2009 - 02:51 PM

So I've revised my code a bit, and now I'm trying to figure out how to tell scan.next() that the token i want is between a set of double quotes. I'd like to include the double quotes in the string, but if that's too tricky then it's not a big deal. Anyone know how I can do this?


 do
		{
			name = scan.next("\"" + "\"");
			year = scan.nextInt();
			price = scan.nextDouble();
			artist = scan.next("\"" + "\"");
			libraryCD[indexCDS] = new CD(name, year, price, artist);
			indexCDS++;
		}while(scan.hasNextLine());

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1