Eof fout

Status
Niet open voor verdere reacties.

Thomaat1

Gebruiker
Lid geworden
1 jun 2009
Berichten
83
beste allemaal,

Gisteren heb ik een programmaatje gemaakt, dat nummers uit een file leest, en per 3 getallen de average en de median aangeeft.

Het probleem is alleen dat als hij dat voor alle nummers gedaan heeft, er een fout komt: "end of file while reading from file "numbers.txt".

Hoe is het op te lossen?( dat hij op tijd stopt met het readen van file numbers.txt, right?)

Alvast bedankt!

Hieronder de code
PHP:
public class Try7
{
   public static void main (String args[]) 
   {	
   TextFile inName   = new TextFile (true, "numbers.txt");
   TextFile outName   = new TextFile(false, "awesome.txt");
   char ba;
   char bb;
   char bc;

   while(!inName.eof())
      {     
      inName.skipWhiteSpace();
      ba = inName.readChar();
      inName.skipWhiteSpace(); 	
      bb = inName.readChar();
      inName.skipWhiteSpace();
      bc = inName.readChar();


      System.out.println("The three numbers are: " + ba + " " + bb + " " + bc);
      System.out.println("Median of the three numbers = " + median(ba, bb, bc));
      System.out.println("Average of the three numbers = " + average(ba, bb, bc));
      System.out.println();

      outName.println("The three numbers are: " + ba + " " + bb + " " + bc);
      outName.println("Median of the three numbers = " + median(ba, bb, bc));
      outName.println("Average of the three numbers = " + average(ba, bb, bc));
      }
   inName.close();
   }

   public static char median(char ab, char ac, char ad)
   {
   if((ab > ac && ac > ad) || (ad > ac && ab < ac)) 
      return ac;

      else if((ac < ad && ab > ad) || (ac > ad && ab < ad))
      return ad;


      else if((ab > ac && ab < ad || ab < ac && ab > ad))
      return ab;

      else
      return ab;
   }

   public static double average(char ab, char ac, char ad)
   {
   String ab1 = String.valueOf(ab);
   Double ab2 = Double.valueOf(ab1).doubleValue(); 
   String ac1 = String.valueOf(ac);
   Double ac2 = Double.valueOf(ac1).doubleValue();
   String ad1 = String.valueOf(ad);
   Double ad2 = Double.valueOf(ad1).doubleValue();

   double average = (ab2 + ac2 + ad2) / 3;
   return average;
   }
   
}
 
Je moet echter goed weten waar de file vandaan komt dan ga je naar een inputstream die is door veel functies te gebruiken. Hieronder zie je wat voorbeeldjes van voor welke je wat gebruikt. Je moet ook eens volgende ook bekijken
http://www.javapassion.com/javase/javaiostream.pdf
en voor serialize moet je dit dan ook bekijken
http://www.tutorialspoint.com/java/java_serialization.htm

Code:
/**
* @author Lieven Roegiers
* @copyright 2007
* @from UpdateCV
*/
import java.io.BufferedReader;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.*;
import java.util.jar.Attributes;
//import java.util.ResourceBundle;

import javax.xml.transform.stream.StreamSource;

/**
* @author Lieven Roegiers
* @copyright 2007
* @from http://code.google.com/p/javamylibs/
*/
public class AResourceFile {
	private static final long serialVersionUID = 1L;
	private String fileName;
	private String pathName;
	private URL fileUrl ;
	private InputStream is;
	 /**
	 * @param fname
	 * @return the file in a stream
	 * @throws IOException
	 */
	public InputStream takeajarfilestream(String fname) throws IOException{
		 return AResourceFile.class.getResourceAsStream(fname);
	}
	//public ResourceBundle getresourceBundle(String fileName){
	//	return this.getresourceBundle(fileName);
	//}
	public InputStream takealocaltestfilestream(String fname) throws IOException{
		 return new FileInputStream("F:/Workspace/CV/lib/"+fname);
	}
	protected void getSourcestream(StreamSource src,String fileName){
		src = new StreamSource(this.getClass().getClassLoader().getResourceAsStream(fileName));
	}
	public InputStream getInputstream(String fileName) throws IOException{
		fileUrl = this.getClass().getClassLoader().getResource(fileName);
		return fileUrl.openConnection().getInputStream();
	}
	public InputStreamReader getInputStreamReader() throws IOException{
		 this.is = fileUrl.openConnection().getInputStream();
		DataInputStream   dis   = new DataInputStream( this.is );
		return new InputStreamReader(dis);
		//BufferedReader br = new BufferedReader( isr );
	}
	public boolean isFile() {
		return (fileUrl != null);
	}
	@Deprecated
	public void readftpInput() throws IOException{
        //String takNaam;
		//URL url = new URL("http://www.dezwaluw.org/3/extra/.txt");
	      //  BufferedReader input = new BufferedReader (new InputStreamReader ((url.openStream())));
	    //..   ....
	}
	@Deprecated
	public void saveftpOutput() throws IOException{
		// SimpleFTP ftp = new SimpleFTP();
		//ftp.connect("ftp://ftp.dezwaluw.org", 21, "***", "***"); //hier gaat het fout, de username en paswoord kloppen zeker en vast
		    //  ....
	}
	public void printInputStream() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
		String line;
		while (null != (line = br.readLine())) {
		   System.out.println(line);
		}
	}
	public void printInputStream(InputStream in) throws IOException{
		 BufferedInputStream bin = new BufferedInputStream(in);
		 int b;
		 while ( ( b = bin.read() ) != -1 )
		 {
		     char c = (char)b;         
		     System.out.print(""+(char)b); //This prints out content that is unreadable.
		                                   //Isn't it supposed to print out html tag?
		 }
	}
	/*
	 * InputStream testStream= new FileInputStream("F:/Workspace/srcaanloop/test.dat");
	 * OutputStream out = System.out;
	**/
	public void printInputStream(InputStream teststream,OutputStream out) throws IOException{
	    byte buf[]=new byte[1024];
	    int len;
		while((len=teststream.read(buf))>0)
			    out.write(buf,0,len);
			    out.close();
	}
	/*public void printinputstream(InputStream iss) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(iss));
		String line = br.readLine();
		while(line!=null){
			System.out.println(line);
			line = br.readLine();
		}
	}*/
	public void printInputStream2(InputStream input) throws IOException{
		int data = input.read();
		int charint;
		int i = 0;
		while ((charint = input.read()) != -1 ) {
				i++;
			  data = input.read();
			  System.out.print((char)charint);
		}
		System.out.println("InputStream = "+input);
		if((charint = input.read()) != -1){
		}else{
			System.out.println("iets mis met de stream");
		}
	}
	public void printOutputStream(OutputStream out) throws IOException{
		System.out.println(out.toString());
	}
}
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan