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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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");
}

}

}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply