Page 1 of 1

Hi I am looking for help with my Java Program. The goal is to open db.txt which contains text data in the following form

Posted: Sun May 15, 2022 10:23 am
by answerhappygod
Hi I am looking for help with my Java Program. The goal is to
open db.txt which contains text data in the following format:
movie title: actor1: actor2: director: year: runtime
movie title: actor1: actor2: director: year: runtime
etc...there are 50 lines.
I have successfully completed the program, but when reviewing
the criteria for the program I realized that I needed to use
StringTokenizer instead of split to read the file. In
fileReader.java how do I change this to utilize StringTokenizer
instead of split? Will this change how this class interacts with
the rest of my program? I have included the fileReader.java
where I would like to tokenize the file and my driver.java so you
can see the overall run of the program.
fileReader.java:
package Main;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class fileReader{
private BufferedReader inputFile;
public fileReader(String filename) throws
FileNotFoundException{
inputFile = new
BufferedReader(new FileReader(filename));
}
public void readFile(movieInfo db) throws
IOException{
String raw;
while((raw =
inputFile.readLine())!= null){
String []
movieAttributes = raw.split(":");

db.insert(movieAttributes[0], movieAttributes[1],
movieAttributes[2], movieAttributes[3],
Integer.parseInt(movieAttributes[4]),
Integer.parseInt(movieAttributes[5]));
}
inputFile.close();
}

}
driver.java:
package Main;
import java.io.Console;
import java.util.ArrayList;
import java.util.Scanner;
public class driver {
public static void
display(ArrayList<String> arr){
for(int
i=0;i<arr.size();i++){

System.out.print(arr.get(i)+" ");
}

System.out.println();
}
public static void main(String[] args){
try{
fileReader
reader = new fileReader("db.txt");

writeToFile writer = new writeToFile("db.text");
movieInfo
database = new movieInfo();

reader.readFile(database);
char
userSelection;
Scanner
scIn = new Scanner(System.in);
Console
cnslIn = System.console();
do{

database.mainMenu();

System.out.println("Enter a selection from
the menu");

userSelection = (char)
System.in.read();

switch(userSelection){

case 'a':

case 'A':


database.insert(writer);

break;

case 'b':

case 'B':


System.out.println("Enter movie title");

String title =
cnslIn.readLine();


database.searchForMovieTitle(title);

break;

case 'c':

case 'C':


System.out.println("Enter actor's name");

String actor =
cnslIn.readLine();

ArrayList<String>
movies = database.searchForActor(actor);

display(movies);

case 'd':

case 'D':


System.out.println("Enter director's name");

String director =
cnslIn.readLine();

ArrayList<String>
movies1 = database.searchForDirector(director);

display(movies1);

break;

case 'e':

case 'E':


System.out.println("Enter the movie's year of
release");

int year =
scIn.nextInt();

ArrayList<String>
movies2 = database.searchForYear(year);

display(movies2);

break;

case 'f':

case 'F':


System.out.println("Enter the movie's runtime in
minutes");

int runtime =
scIn.nextInt();

ArrayList<String>
movies3 = database.searchForRuntimeMinutes(runtime);

display(movies3);

break;

case 'g':

case 'G':


System.out.println("Exiting database");

break;


}

scIn.close();
}

while(userSelection != 'g');
}
catch(Exception e){

System.out.println("An error occured");
}

}

}