Question: This is my JAVA program and I want to implement this in the ranking: If the rank is not available, we need the functionality to:
This is my JAVA program and I want to implement this in the ranking:
If the rank is not available, we need the functionality to:
1. Attempt to push the element down- if able then
2. Place current element at the requested rank position - if not able then
3. Place the current element at the lowest available rank location
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class progress {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList
//calls from original file
ArrayList
//add and remove from list
ArrayList
//array list for rankings
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;
//go to rating & comments
System.out.print(" Enter a favorite: ");
fav = keyboard.nextLine();
if(enteredChoice == 1)
{
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)
{
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."); } } for(int i=0; i { System.out.println("Favorite: " + movieList.get(i)); System.out.print("Enter your rating: "); String rating = keyboard.nextLine(); while(rating.length()<5) rating += " "; System.out.print("Enter your comment: "); String comment = keyboard.nextLine(); rankings.set(i, " " + rating); comments.set(i, " " + comment); 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(); } catch(Exception e) { } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
