Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

If anyone could fix the following code below, it would be highly appreciated. The top works fine, but the middle is where the problem lies.

If anyone could fix the following code below, it would be highly appreciated. The top works fine, but the middle is where the problem lies.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.PrintWriter;

import java.util.Collections;

import java.util.Scanner;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

public class Flavors {

private static ArrayList list;

private static boolean rankError;

private static int rank;

private static String name;

//=====================================================================================

public static void main(String[] args) {

BufferedReader reader = null;

ArrayList myFlavorsList = new ArrayList (); //myFlavorsList is the name my array used to store my favorite flavors.

try {

String sCurrentLine;

reader = new BufferedReader(new FileReader("C:\\Users\\......\\Desktop\\Flavors.txt"));

//windows: c:\\users\\mlevi10\\Desktop\\Mary.txt (sample set up of file locator.)

while ((sCurrentLine = reader.readLine()) != null) {

//System.out.println(sCurrentLine);

myFlavorsList.add(sCurrentLine); // used to get array

}

} catch (IOException e) {

e.printStackTrace(); //<<<< Explains in plain English what is wrong.

System.out.print(e.getMessage());

} finally {

try {

if (reader != null)reader.close();

} catch (IOException ex) {

System.out.println(ex.getMessage());

ex.printStackTrace();

}

}

for(int i = 0; i< myFlavorsList.size(); i++){ // this will print the file backwards. Last line first, 1st line last.

// change the last part (increment) to change the outcome.

System.out.println((i+1)+". " + myFlavorsList.get(i)); // this will print the flavors with a number.

}

String userInput;

Scanner sc = new Scanner(System.in);

do{

System.out.println("Would you like to add or delete anything to this list?");

userInput = sc.next();

try{

if(userInput.equals("add")){ // Note to self: in Java dot ".equals" be used to test condition of a String, or my code will not continue.

System.out.println("Enter String you want to add.");

myFlavorsList.add( sc.next()); // used to add input additional items/flavors to my existing list. (3, represents the index location.)

// myFileLines.add((userInput));

for(int i = 0; i< myFlavorsList.size(); i++){ // this will print the file backwards. Last line first, 1st line last.

// change the last part (increment) to change the outcome.

System.out.println((i+1)+". " + myFlavorsList.get(i)); // this will print the flavors with a number.

}

}else if(userInput.equals("delete")){

System.out.println("Which position do you want to remove the item?");

myFlavorsList.remove(sc.nextInt()-1); // minus 1 will remove one less than the index. In other words the exact number you put in, as indicated on you list.

for(int i = 0; i< myFlavorsList.size(); i++){ // this for loop is so it goes through the while ist.

System.out.println((i+1)+". " + myFlavorsList.get(i));

}

} //Note: This is ending my if/elseif statement.

}catch (Exception e){

}

}

while(!userInput.equalsIgnoreCase("Proceed"));

System.out.println("I'm sorry, your entered value of "+ userInput + " is invalid.... Please try again."); //keep the printing outside the exception.

//====================================================================================================

//Trying to rank the list, to prioritize them

int[] rank = new int[list.size()];

for (int a = 0; a < list.size(); a++) {

rank[a] = 0;

}

Collections.sort(list);

int currentRanking = 0;

for(int a = 0; a < list.size(); a++){

currentRanking = list.get(a).rank;

currentRanking++;

//System.out.println("Your ranked as been placed in " + currentRanking);

}

for (int a = 0; a < list.size(); a++) {

//System.out.println("Flavor " + "Rank " + "Comments");

// System.out.println(list.get(a).name + " " + list.get(a).rank + " " + list.get(a).comment);

}

for (int a = 0; a < list.size(); a++) {

Scanner user = new Scanner(System.in);

System.out.println("Enter rank for " + list.get(a).name);

do{

try{

int r = sc.nextInt();

if (r > 0 && r <= list.size()) {

addRank(list.get(a).name, r, rank);

rankError = false;

} else {

System.out.println("Number to rank is out of bounds! Try again!");

rankError = true;

}

}catch(Exception e){

e.printStackTrace();

rankError = true;

}

}while(rankError == true);

//to write back

//==================================================================================

// trying write back to a file.

try {

String content = "This is the content to write into file";

File outfile = new File("c://san120/Desktop/changesmade.txt");

// if file doesn't exists, then create it

if (!outfile.exists()) {

outfile.createNewFile();

}

FileWriter fw = new FileWriter(outfile.getAbsoluteFile()); //Open the file for writing

BufferedWriter bw = new BufferedWriter(fw);

bw.write(content);

bw.flush(); //Always a good habit to flush when you are through

bw.close();

System.out.println("Done");

} catch (IOException e) {

e.printStackTrace();

}

//=========================================================================================================

} // This closes my main method

} }

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

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions

Question

Do you currently have a team agreement?

Answered: 1 week ago