Results 1 to 2 of 2
  1. #1
    ZivonHunter's Avatar
    ZivonHunter is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Question Problem with DataInputStream

    I am writing a program to read in the following information from a text file:
    GLG 42.46 44.10 1,827,000
    FTO 29.00 29.40 1,388,700
    CELG 52.10 53.44 12,419,966
    WFR 36.45 35.50 4,806,400
    AMX 41.25 42.87 6,133,600
    ADM 39.25 38.50 6,264,200
    GRMN 52.99 53.41 3,478,047
    GILD 69.20 68.90 2,789,505
    TXU 63.10 63.13 2,953,200
    HOC 47.74 47.71 970,000
    GYMB 46.82 46.46 631,598
    XTO 45.95 46.66 4,968,200
    CCJ 34.75 35.13 1,746,900
    NITE 18.26 18.65 1,202,794
    WNR 23.45 23.56 314,400
    TS 38.85 38.59 1,464,400
    LVS 74.60 76.20 1,285,300
    LIFC 22.75 23.43 1,236,279
    AAUK 22.55 22.68 476,948
    FORM 37.93 38.18 614,417
    STLD 60.47 60.11 1,522,391
    HOLX 48.45 48.15 689,105
    WCC 65.32 65.27 500,200
    CTSH 77.65 75.28 2,067,837
    HLX 31.49 32.24 856,200
    ICE 85.00 84.42 1,995,400
    TTI 26.13 25.90 962,500
    BOOM 32.76 32.24 474,225
    DRIV 56.70 57.85 1,762,669
    COG 52.10 52.91 322,500
    GOL 31.50 31.15 1,100,000
    GI 80.87 80.98 190,200
    FMD 65.52 67.45 597,800
    ALY 15.17 15.07 396,700
    RRC 26.98 27.15 1,150,900
    What I'm trying to do is read the information into an array in the Stocks class which represents all of the information being read in. And the Stock class represents the stocks individually. All information is then to be printed out, one line for each set of information.

    Java Code:
    import java.io.*;
    import stock.Stocks;
    
    public class TestStocks {
    	static DataInputStream readIn;
    	
    	public static void main(String[] args) throws IOException {
    		readIn = new DataInputStream(new FileInputStream("StockData.txt"));
    		Stocks s = new Stocks();
    		String name; double open, close; int volume;
    		
    		name = readIn.readUTF();
    		readIn.readChar();	//removes the tab before next input
    		open = readIn.readDouble();
    		readIn.readChar();	
    		close = readIn.readDouble();
    		readIn.readChar();	
    		volume = getVolume();
    		readIn.readChar();
    		
    		s.addStock(name, open, close, volume);
    	}
    	
    	public static int getVolume() throws IOException{
    		String r = "(?<=\\d),(?=\\d),(?=\\d)", r1, s = readIn.readUTF();
    		r1 = s.replaceAll(r, "");
    		int i = Integer.parseInt(r1);
    		return i;
    	}
    }
    Java Code:
    package stock;
    
    import java.io.*;
    
    public class Stocks implements Number {
    	private int num, numStocks;
    	private Stock[] totalstocks; 
    	BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    	
    	public Stocks() throws IOException {
    		numStocks = getNumStocks();
    		num = 0;
    		totalstocks = new Stock[numStocks];
    	}
    	
    	public int getNumStocks() throws IOException{
    		System.out.print("Enter the total number of Stocks:");
    		numStocks = Integer.parseInt(dataIn.readLine());
    		return numStocks;
    	}
    
    	public void addStock(String n, double o, double c, int v) throws IOException {
    		totalstocks[num] = new Stock(n, o, c, v);
    		System.out.println(totalstocks[num].toString());
    		num++;
    	}
    	
    	public String toString() {
    		return totalstocks[num].toString();
    	}
    }
    Java Code:
    package stock;
    
    public class Stock {
    
    	private String name; 
    	private double opening, closing; 
    	private int volume;
    	
    	public Stock(String n, double o, double c, int v) {
    		name = n;
    		opening = o;
    		closing = c;
    		volume = v;
    	}
    	
    	public String toString() {
    		return (name + "\t\t" + opening + "\t\t" + closing + "\t\t" + volume + "\n");
    	}
    The problem starts where I am trying to read the information in using the TestStocks class. An EOFException is returned when reading in the information for the variable name. I also need to remove the commas from the integer values being read into volume. I already have a method for this in the TestStocks class, but I would like to know if it correctly removes the commas.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Once you read a line before posting check that the read line is null or not. If it's null that means you comes to the end of files. That's why you comes with an EOFException in your application.

Similar Threads

  1. Replies: 0
    Last Post: 06-26-2008, 07:34 PM
  2. DataInputStream readLine()
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 10:44 PM
  3. Replies: 1
    Last Post: 08-07-2007, 07:16 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
  •