Need help reading from an external text file
in
Programming Questions
•
5 months ago
I am in need of some help with my processing program I am developing for a college assignment.
I have three classes in total. I have my main class which draws the program and runs it. I then have my client class which is used for the printing of the text and finally I have a read file which reads the input from the external text file.
I have the program running perfectly and have been playing around with it and I did get the program to print out numbers(coordinates) from an external file. However the document specification is text from an external file.
Also if anyone has any good ideas what I can do in regards to taking input from the keyboard as that is part of the specification
I have basic knowledge of Java and have started using processing with it recently so I would appreciate some help with this please.
This is my main class called SoccerCode.
- import processing.core.PApplet;
- public class SoccerCode extends PApplet
- {
- PApplet parent;
- int posX,posY, velX, velY;
- float mida =6;
- float mida1 =10;
- public void setup ()
- {
- size (300,150);
- posX = width /2;
- posY = height/2;
- velX = 2;
- velY =2;
- }
- public void draw ()
- {
- background (0,190,0);
- line (0,0,300,0);
- line (300,0,300,150);
- line (300,150,0,150);
- line (0,150,0,0);
- line (150,0,150,150);
- noFill();
- ellipse (150 , 75 , 6,6 );
- ellipse (150 , 75 , 35,35 );
- ellipse (0 , 75 , 100 , 100 );
- ellipse (300 , 75 , 100 , 100 );
- stroke (220);
- strokeWeight ( 5);
- posX = posX+velX;
- posY = posY+velY;
- ellipse(posX,posY,mida,mida);
- if((posX<6)||(posX>width))
- {
- velX = -velX;
- }
- if((posY<6)||(posY>height-6))
- {
- velY = -velY;
- }
- if(mousePressed==true)
- {
- rect (0,mouseY,mida1,20);
- rect (290,mouseY,mida1,20);
- }
- }
Here is my client class
- import java.io.File;
- import java.io.IOException;
- import processing.core.*;
- import java.io.BufferedReader;
- public class Client extends PApplet
- {
- public void main() throws IOException
- {
- String file_name = "text.txt";
- try
- {
- ReadFile file = new ReadFile(file_name);
- String[] aryLines = file.OpenFile();
- int i;
- for(i=0; i<aryLines.length; i++)
- {
- System.out.println(aryLines[i]);
- }
- }
- catch (IOException e)
- {
- System.out.println(e.getMessage());
- }
- }
- }
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class ReadFile
- {
- private String path;
- public ReadFile(String file_path)
- {
- path = file_path;
- }
- public String[] OpenFile() throws IOException
- {
- FileReader fr = new FileReader(path);
- BufferedReader textReader = new BufferedReader(fr);
- int numberOfLines = readLines(); //Uses the readLines method to fill the data.
- String[] textData = new String[numberOfLines];
- int i;
- for (i=0; i<numberOfLines; i++)
- {
- textData[i] = textReader.readLine();
- }
- textReader.close();
- return textData;
- }
- int readLines() throws IOException
- {
- FileReader file_to_read = new FileReader(path);
- BufferedReader bf = new BufferedReader(file_to_read);
- String aLine;
- int numberOfLines = 0;
- while((aLine = bf.readLine()) != null) //Reads the lines of text and if the next is null loop ends.
- {
- numberOfLines++;
- }
- bf.close();
- return numberOfLines;
- }
- }
Kind Regards,
Jason
1