File I/O

I need huge help with Java homeworks

Page 1 of 1

4 Replies - 1054 Views - Last Post: 02 January 2011 - 10:04 AM Rate Topic: -----

#1 Guest_beginner*


Reputation:

File I/O

Posted 31 December 2010 - 05:01 AM

I need huge help with Java homeworks. If some1 can help i can pay few $ .

info.txt
//years / field / culture / how many /
2004 field1 grain 14
2004 field1 barley 45
2004 field2 rye 5
2005 field1 wheat 77
2005 field1 grain 45
2004 field2 rye 5
2003 field1 barley 48
2003 field1 rye 45
2003 field2 wheat 5

In another file, crop storage cost and selling price:
/years / culture / storage cost and selling price:
2004 rye 600 700
2005 wheat 400 500


The result of a text file to the estimated profitability of the enterprise in all fields, if you read that a ton of leaves every other culture in the sowing and the remainder sold. Structure: (sort by year and field) : year field1 profit

And here what i`ve done alredy :
import java.io.*;

public class Ukininkas {
	int metai;
	String laukas;
	String rusis;
	int kiekis;

	public static void main(String[] args) throws IOException  {

		Veiksmai ns = new Veiksmai();
		//ns.itraukti(2012, "laukas12", "rusis22", 13);
		
	}

}

class Veiksmai extends Ukininkas {
	static File fDerlius = new File("...","derlius.txt");
	static File temp = new File("...","temp.txt");
	static File fKainos = new File("...","kainos.txt");
	static File fRez = new File("...","rezultatai.txt");
	String eil;
	int nr=1;
	//String[] buff;
	void skaityti() throws IOException {
		FileReader input = new FileReader(fDerlius);
		BufferedReader bufRead = new BufferedReader(input);
		//buff = new String[4];
		eil = bufRead.readLine();
		while(eil != null){
			String buff[] = eil.split(" ");
			System.out.println("Metai: "+buff[0]+" Laukas: "+buff[1]+" Rusis: "+buff[2]+" Kiekis: "+buff[3]);
			eil = bufRead.readLine();
		}
		input.close();
	}
	void irasyti() throws IOException{	

		FileWriter fW = new FileWriter(temp);
		BufferedWriter bW = new BufferedWriter(fW);
		bW.write(metai+" "+laukas+" "+rusis+" "+kiekis);
		bW.newLine();
		bW.close();
		fW.close();
	}
	void itraukti(int metai, String laukas, String rusis, int kiekis) throws IOException{	
		this.metai = metai;
		this.laukas = laukas;
		this.rusis = rusis;
		this.kiekis = kiekis;
		FileReader input = new FileReader(fDerlius);
		BufferedReader bufRead = new BufferedReader(input);
		FileWriter output = new FileWriter(temp,true);
		BufferedWriter out = new BufferedWriter(output);
		irasyti();
		eil = bufRead.readLine();
		while(eil!=null){			
			String buff[] = eil.split(" ");
			output.write(buff[0]+" "+buff[1]+" "+buff[2]+" "+buff[3]+" \r\n");
			eil = bufRead.readLine();
		}
		output.close();
		out.close();
		bufRead.close();
		input.close();
		fDerlius.delete();
		perrasyti();
		skaityti();
		temp.delete();
	}
	void perrasyti() throws IOException{	
		FileReader input = new FileReader(temp);
		BufferedReader bufRead = new BufferedReader(input);
		FileWriter output = new FileWriter(fDerlius,true);
		BufferedWriter out = new BufferedWriter(output);
		eil = bufRead.readLine();
		while(eil!=null){			
			String buff[] = eil.split(" ");
			output.write(buff[0]+" "+buff[1]+" "+buff[2]+" "+buff[3]+" \r\n");
			eil = bufRead.readLine();
		}
		output.close();
		out.close();
		bufRead.close();
		input.close();
	}
	void result() throws IOException{
		FileReader file = new FileReader(fDerlius);
		BufferedReader bufRead = new BufferedReader(file);
		FileWriter output = new FileWriter(fRez,true);
		BufferedWriter out = new BufferedWriter(output);
		
		eil = bufRead.readLine();
		while(eil!=null){
			String met = null;
			String lauk = null;
			String rus = null;
			String kiek = null;
			String buff[] = eil.split(" ");
			buff[0] = met;
			buff[1] = lauk;
			buff[2] = rus;
			buff[3] = kiek;
			int rez = 0;
			
			
			output.write(buff[0]+" "+buff[1]+" "+buff[2]+" "+buff[3]+" \r\n");
			eil = bufRead.readLine();
		}
		output.close();
		out.close();
		bufRead.close();
		file.close();
	}
	void eil_sk() throws IOException{
		int sk = 1;
		FileReader file = new FileReader(fDerlius);
		BufferedReader bufRead = new BufferedReader(file);
		eil = bufRead.readLine();
		while(eil!=null){
			sk++;
		}
		file.close();
		bufRead.close();
	}
	
}

This post has been edited by macosxnerd101: 31 December 2010 - 09:03 AM
Reason for edit:: Title renamed to be more descriptive. "Help" and "I have homework" aren't descriptive titles.


Is This A Good Question/Topic? 0

Replies To: File I/O

#2 pbl  Icon User is offline

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

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

Re: File I/O

Posted 31 December 2010 - 07:18 PM

Which OS do you use so that ... is a valid directory path

static File fDerlius = new File("...","derlius.txt");

by the way no reason for all your File to be static

You commented the code that //ns.itraukti(2012, "laukas12", "rusis22", 13);

what error(s) do you get when you run the program with it

Also, as your methods name are completly ununderstable for the majority of us a few comments (in English) would help
Was This Post Helpful? 0
  • +
  • -

#3 Guest_plesul*


Reputation:

Re: File I/O

Posted 01 January 2011 - 03:25 AM

no u dont stand me. This is my program but i still need to add what i sayed. What i alredy writed it works but i still need help with algoritm.
How can i read 2 files and take what i need?
Was This Post Helpful? 0

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: File I/O

Posted 01 January 2011 - 01:58 PM

Create a BufferedReader, Scanner, or other File Reading class for each File, then use that to process the two different Files.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,348
  • Joined: 20-September 08

Re: File I/O

Posted 02 January 2011 - 10:04 AM

As pbl says, using English would help. Also post some actual input files
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1