Question
This is my JAVA program and I want the final favorites list to print out the user's rankings in order.... the txt file is a
This is my JAVA program and I want the final favorites list to print out the user's rankings in order.... the txt file is a list of movies:
Spider Man
Iron Man
The Avengers
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class program {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList fileOfMovies = new ArrayList<>();
//calls from original file
ArrayList movieList = new ArrayList<>();
//add and remove from list
ArrayList rankings = new ArrayList<>();
//array list for rankings
ArrayList comments = new ArrayList<>();
//array list for comments
int choice = 0;
Scanner keyboard = new Scanner(System.in);
try {
Scanner fileReader = new Scanner(new File("favorites.txt"));
while(fileReader.hasNextLine())
{
String movieDetail = fileReader.nextLine();
if(!movieDetail.equals(""))
fileOfMovies.add(movieDetail);
String tokens[] = movieDetail.split("\\|");
movieList.add(tokens[0].trim());
rankings.add(tokens[0].trim());
comments.add(tokens[0].trim());
}
fileReader.close();
//print all favorites to "Show what's coming"
System.out.println("List of movies : ");
System.out.print("--------------- ");
for(String string: movieList)
System.out.println(string);
String fav;
while(true)
{
System.out.print("--------------- ");
int enteredChoice = 0;
Scanner myChoiceScan = new Scanner(System.in);
boolean choiceError = false;
String choiceString = "";
do {
try {
System.out.print("Please select one of the following:");
System.out.print(" 1. Add a favorite 2. Remove a favorite 3. Add ratings and comments Enter your choice: ");
choiceString = myChoiceScan.next();
enteredChoice = Integer.parseInt(choiceString.trim());
choiceError = false;
} catch (Exception e) {
System.out.println("Your entry: \"" + choiceString + "\" is invalid...Please try again");
choiceError = true; //Uh-Oh...We have a problem.
}
} while (choiceError == true);
//if(enteredChoice != 1 && enteredChoice != 2)
// break;
if(enteredChoice == 1)
{
System.out.print(" Enter a favorite: ");
fav = keyboard.nextLine();
while(fav.length() <20)
fav+= " ";
movieList.add(fav);
rankings.add(" ");
comments.add(" ");
fileOfMovies.add(fav);
System.out.println("Added favorite: " + fav);
}
else if(enteredChoice ==2)
{
System.out.print(" Enter a favorite: ");
fav = keyboard.nextLine();
boolean check = false;
for(int i=0; i
{
if(movieList.get(i).contains(fav))
{
movieList.remove(i);
rankings.remove(i);
comments.remove(i);
fileOfMovies.remove(i);
check = true;
System.out.println("Movie removed successfully!");
}
}
if(!check)
System.out.println("Entered movie does not exist.");
}
else if(enteredChoice == 3) {
boolean valid=true;
for(int i=0; i
System.out.println("Favorite: " + movieList.get(i));
System.out.print("Rank this movie 1 through " + movieList.size() +" ");
String rating = keyboard.nextLine();
try {
int r =Integer.parseInt(rating);
valid=true;
}
catch(Exception e) {
System.out.println("Enter a valid rating value");
--i;
valid=false;
}
if(valid == true) {
while(rating.length()<5)
rating += " ";
System.out.print("Enter your comment: ");
String comment = keyboard.nextLine();
rankings.set(i, " " + rating);
comments.set(i, "" + comment);
}
}
Collections.sort(fileOfMovies);
for(int i=0; i
fileOfMovies.set(i, movieList.get(i) + "|" + rankings.get(i) + "|" + comments.get(i));
PrintWriter writer = new PrintWriter(new File("favorites.txt"));
System.out.println(" Final favorite list ");
for(String string: fileOfMovies)
{
writer.write(string + " ");
System.out.println(string);
}
writer.close();
break;
}
}
}
catch(Exception e) {
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started