I have an assignment at school in which i have to make a program that reads a "Product number" and outputs the "Product Name". Those two pieces of info is located in a txt file, separated by a "," like this:
10002,iron
10003,gold
10003,cobber
Right now I'm able to read the txt file and store it in an array and output the entire array. This is my 2 classes:
FileData (main)
package textfiles; import java.io.IOException; public class FileData { public static void main(String[] args) throws IOException { String file_name = "/Users/jack_sogaard/Desktop/store.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() ); } } }
And ReadFile
package textfiles; import java.io.IOException; import java.io.FileReader; import java.io.BufferedReader; 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(); 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 br = new BufferedReader(file_to_read); String aLine; int numberOfLines =0; while (( aLine = br.readLine()) !=null) { numberOfLines++; } br.close(); return numberOfLines; } }
Any help would be appreciated
/jaze