I need a code written in the java language and it has to meet the requirements a
ID: 3819804 • Letter: I
Question
I need a code written in the java language and it has to meet the requirements and rubric written below. I have code posted below but it doesn't follow the requirements for the code at all. I seriously need help, I need this code or another code to follow the requirements correctly.
List of n favorites:
Knicks
Heat
Lakers
Celtics
Jazz
Requirements:
Decide on a list of n favorites (songs, bands, movies, video games, etc.) (between 5 and 10 favorites is fine).
Write a program that lists the n favorites but with a small twist.
Read in a file of n favorites (sample file in the Resources/Sample File area)...Please make your own.
Print a list of all n favorites to the user (so they know what is coming)
Ask if the user wishes to add or delete elements to/from the list.
If they wish to add an element, read in the element from the console
If they wish to remove an element, prompt for the item to be removed.
Print the next favorite from the list and for each favorite....
1st prompt for the user’s ranking of the favorite
2nd prompt for the user’s comments (for this round)
Slot the favorite into the final list using the supplied flowchart
Inform the user of the placement of the latest element.
After all n favorites, print the list to the screen
When complete, write back to the input file, adding the new comments to the previous rounds of comments.
The file should be able to be consumed by your program for the next round of play.
CONSOLE OUTPUT:
‘Favorite’ | Rank | Comments from this round (plus all previous comments)
RUBRIC
Expert Answer
Was this answer helpful?
0
0
7 answers
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class favorites
{
static File FILENAME = new File("listofFavorites.txt");
public static void main(String[] args) throws IOException
{
while(true)
{
System.out.println("Please provide number to perform operation");
System.out.println("1:Print list of favorites");
System.out.println("2:Add into list of favorites");
System.out.println("3:Delete from the list");
System.out.println("4:Exit");
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
if(input==1)
{
readlist();
}
else if(input==2)
{
System.out.println("Enter Item");
Scanner sc1=new Scanner(System.in);
String item=sc1.next();
addintolist(" "+item);
}
else if(input==3)
{
// File inputFile = new File("myFile.txt");
File tempFile = new File("myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(FILENAME));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = "";
try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {
String sCurrentLine;
// while ((sCurrentLine = br.readLine()) != null) {
// System.out.println(sCurrentLine);
String currentLine;
while((currentLine = reader.readLine()) != null) {
System.out.println(currentLine);
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
System.out.println("want to remove (y/n)");
Scanner sc2=new Scanner(System.in);
String flag=sc2.next();
if(flag.equalsIgnoreCase("y"))
lineToRemove = currentLine;
}
} catch (IOException e) {
e.printStackTrace();
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(FILENAME);
}
else if(input==4)
{
System.out.println("exiting from program");
return;
}
else
{
System.out.println("invalid entry");
}
}
}
public static void readlist()
{
try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void addintolist(String item)
{
try(FileWriter fw = new FileWriter(FILENAME, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println(item);
//more code
//out.println("more text");
//more code
} catch (IOException e) {
//exception handling left as an exercise for the reader
}
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class favorites
{
static File FILENAME = new File("listofFavorites.txt");
public static void main(String[] args) throws IOException
{
while(true)
{
System.out.println("Please provide number to perform operation");
System.out.println("1:Print list of favorites");
System.out.println("2:Add into list of favorites");
System.out.println("3:Delete from the list");
System.out.println("4:Exit");
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
if(input==1)
{
readlist();
}
else if(input==2)
{
System.out.println("Enter Item");
Scanner sc1=new Scanner(System.in);
String item=sc1.next();
addintolist(" "+item);
}
else if(input==3)
{
// File inputFile = new File("myFile.txt");
File tempFile = new File("myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(FILENAME));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = "";
try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {
String sCurrentLine;
// while ((sCurrentLine = br.readLine()) != null) {
// System.out.println(sCurrentLine);
String currentLine;
while((currentLine = reader.readLine()) != null) {
System.out.println(currentLine);
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
System.out.println("want to remove (y/n)");
Scanner sc2=new Scanner(System.in);
String flag=sc2.next();
if(flag.equalsIgnoreCase("y"))
lineToRemove = currentLine;
}
} catch (IOException e) {
e.printStackTrace();
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(FILENAME);
}
else if(input==4)
{
System.out.println("exiting from program");
return;
}
else
{
System.out.println("invalid entry");
}
}
}
public static void readlist()
{
try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void addintolist(String item)
{
try(FileWriter fw = new FileWriter(FILENAME, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println(item);
//more code
//out.println("more text");
//more code
} catch (IOException e) {
//exception handling left as an exercise for the reader
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.