Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is my JAVA program below and I want to fix or implement the following: Error checking (strings vs. ints): either crashes or ignores wrong

This is my JAVA program below and I want to fix or implement the following:

Error checking (strings vs. ints): either crashes or ignores wrong inputs

Check rankings so that the user doesn't choose rankings already been used

Comments: get added each round, but something weird happens in later rounds (rank -1 appears before comment, deletes old comments- youll see what I mean)

Removing favorites: this is the biggest problem right now- when favorites are removed, it eventually creates an index error that I am not yet sure how to resolve, "Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4".

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;

public class groupAssignment {

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();

fileOfMovies.add(movieDetail);

String tokens[] = movieDetail.split("\\|");

movieList.add(tokens[0]);

rankings.add(tokens[0]);

comments.add(tokens[0]);

}

fileReader.close();

//print all favorites to "Show whats 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("--------------- ");

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: ");

choice = Integer.valueOf(keyboard.nextLine());

if(choice != 1 && choice != 2)

break;

//go to rating & comments

System.out.print(" Enter a favorite: ");

fav = keyboard.nextLine();

if(choice == 1)

{

while(fav.length() <20)

fav+= " ";

movieList.add(fav);

rankings.add(" ");

comments.add(" ");

fileOfMovies.add(fav);

System.out.println("Added favorite: " + fav);

}

else if(choice ==2)

{

for(int i=0; i

{

if(movieList.get(i).contains(fav))

{

movieList.remove(i);

fileOfMovies.remove(i);

rankings.remove(i);

comments.remove(i);

fileOfMovies.remove(i);

}

}

}

}

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, 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();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

LO14.2 Discuss how game theory relates to oligopoly.

Answered: 1 week ago