Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**Basic Java** I just need to add 2 METHODS onto this code. i need the methods t

ID: 3692479 • Letter: #

Question

**Basic Java** I just need to add 2 METHODS onto this code. i need the methods to ask the user if they would like to add in an element (to input file) before the program starts, and then I need a method towards the end of the program that overwrites the info back into the file using a Buffered File writer method. It will look like this:

Have the program ask the user if they want to add in a new element to the input file

(Y/N)

(Y) > use scanner to take in the element

    > do you want to add anymore elements?

(N) > run the program with the existing and new elements

Then overwrite the results to that same input file

________________________________________

I will be sure to leave a thumbs up! Thank you!

________________________________________

input file Mary.txt--

A

B

C

D

E

_____________________________________

CODE---

import java.io.*;

import java.util.*;

public class listgroup {

   public static void main(String[] args) throws Exception {

       //Filepath

       String filepath = "/Users/user/Desktop/mary.txt"; // Filepath will be different for windows users

       //Load File to Scanner

       BufferedReader br;

       try{

           br = new BufferedReader(new FileReader(filepath));

       } catch(Exception e){

           throw e;

       }

      

       ArrayList<String> elements = new ArrayList<String>();

       readElementsToArrayListAndPrint(br, elements);

       ArrayList<String> rankedElements = new ArrayList<String>(elements.size());

       for (int i = 0; i < elements.size(); i++){

           rankedElements.add("");

       }

       ArrayList<String> elementComments = new ArrayList<String>(elements.size());

       for (int i = 0; i < elements.size(); i++){

           elementComments.add("");

       }

       Scanner sc400 = new Scanner(System.in);

       //Main Loop

       for (int i = 0; i < elements.size(); i++){

           System.out.println("#" + (i+1) + ": " + elements.get(i));

           //Collect comment.

           System.out.println("Please write any comments you have about this element!");

           String comment;

           try {

               comment = sc400.nextLine();

           } catch (Exception e2) {

               comment = "No comment.";

           }

           //Collect Rank

           int rank = -1;

           while (rank == -1) {

               System.out.println("How would you like to rank this element?");

               try {

                   rank = sc400.nextInt();

                   if (!isRational(rank, elements.size())){

                       rank = -1;

                       System.out.println("Please enter an integer that is between 1 and "

                       + elements.size() + ".");

                   } else {

                       rank--;

                   }

               } catch (Exception e) {

                   System.out.println("Please enter a valid integer input.");

                   sc400.nextLine();

               }

               sc400.nextLine();

           }

           if (rankedElements.get(rank) == ""){ //Rank was unoccupied

               System.out.println("Rank accepted without incident!");

               placeElementAtPosition(rank, elements.get(i), comment, rankedElements, elementComments);

           } else { //Rank was occupied

               System.out.print("Rank already in use...");

               if (pushDown(rank, rankedElements, elementComments)){ //Elements can/were pushed

                   System.out.println("Elements at rank " + (rank+1) + " were pushed "

                           + "down to accommodate the current element.");

                   placeElementAtPosition(rank, elements.get(i), comment, rankedElements, elementComments);

               } else { //Elements could not be pushed

                   System.out.println("Elements beyond rank " + (rank+1) + " were all taken. "

                           + "Placing element at next lowest rank above " + (rank+1) + ".");

                   placeElementAtLowestRank(rank, elements.get(i), comment, rankedElements, elementComments);

               }

           }

       }

       sc400.close();

       //Display results

       System.out.println("Results!");

       for(int i = 0; i < rankedElements.size(); i++){

           System.out.println("'" + rankedElements.get(i) + "' | " + (i+1) + " | " + elementComments.get(i));

       }

       //Bye

       System.out.println("Thanks for running me! Have an awesome day!");

   }  

/**

   * SP - A

   * Pushes the elements in two parallel arrayLists down at a common index. Does nothing

   * if the elements beyond the rank are all already occupied.

   * @param rank Index at which to begin pushing.

   * @param ranked ArrayList of ranked elements

   * @param comments Parallel ArrayList of element comments.

   * @return True if elements were successfully pushed down. False if not.

   */

   public static boolean pushDown(int rank, ArrayList<String> ranked, ArrayList<String> comments){

       int i = rank;

       for (; i < ranked.size() ; i++){

           if (ranked.get(i) == ""){

               break;

           }

       }

       if (i == ranked.size()){ //for Loop traversed without finding an empty spot.

           return false;

       } else {

           ranked.remove(i);

           comments.remove(i);

           ranked.add(rank, ""); //Leaves rank slot as empty.

           comments.add(rank, "");

           return true;

       }

   }

/**

   * SP - B

   * Places an element and its comment in two parallel ArrayLists at a given position.

   * @param pos Position at which to insert element/comment.

   * @param element Element to be inserted.

   * @param comment Comment to be inserted.

   * @param ranked ArrayList of ranked elements.

   * @param comments Parallel ArrayList of element comments.

   */

   public static void placeElementAtPosition(int pos, String element, String comment,

           ArrayList<String> ranked, ArrayList<String> comments){

       ranked.set(pos, element);

       comments.set(pos, comment);

   }

/**

   * SP - C

   * Places an element an d its comment in the first available position above the given

   * one in two parallel ArrayLists.

   * @param pos Position above which to search for a spot.

   * @param element Element to be inserted.

   * @param comment Comment to be inserted.

   * @param ranked ArrayList of ranked elements.

   * @param comments Parallel ArrayList of element comments.

   */

   public static void placeElementAtLowestRank(int pos, String element, String comment,

           ArrayList<String> ranked, ArrayList<String> comments){

       for(pos--; pos >= 0; pos--){

           if(ranked.get(pos) == ""){

               ranked.set(pos, element);

               comments.set(pos, comment);

               System.out.println("Element was placed at rank " + (pos+1) + ".");

               break;

           }

       }

   }

/**

   * For each string in a given file, the string is trimmed, inserted into an arraylist,

   * and printed.

   * @param sc Scanner with preloaded element file.

   * @param elements Empty ArrayList of strings.

   */

   public static void readElementsToArrayListAndPrint(BufferedReader br, ArrayList<String> elements){

       String element = "";

       try {

           element = br.readLine();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           System.out.println("Oops! Something went wrong!");

           e.printStackTrace();

       }

       while (element != null) {

           element = element.trim(); //Just in case there are trailing/leading spaces.

           elements.add(element);

           System.out.println(element);

           try {

               element = br.readLine();

           } catch (IOException e) {

               // TODO Auto-generated catch block

               System.out.println("Oops! Something went wrong!");

               e.printStackTrace();

           }

       }

       try {

           br.close();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           System.out.println("Oops! Something went wrong!");

           e.printStackTrace();

       }

   }

/**

   * Checks to see if the input is between the bounds of 1 and n, where n is the number

   * of elements in an array.

   * @param input int value to be checked

   * @param size number of elements in the corresponding array

   * @return True if input is within bounds. Otherwise false.

   */

public static boolean isRational(int input, int size){

       if (input > 0 && input < size){

           return true;

       }

       return false;

   }

}

Explanation / Answer

Hi, I have added two methods as per your requirement. It is working fine now. Please have a look.

addMoreElements();
overWriteElements(String)

ListGroup.java


import java.io.*;
import java.util.*;
public class ListGroup {
   static String filepath = "D:\mary.txt";
public static void main(String[] args) throws Exception {
   addMoreElements();
//Filepath
// Filepath will be different for windows users
//Load File to Scanner
BufferedReader br;
try{
br = new BufferedReader(new FileReader(filepath));
} catch(Exception e){
throw e;
}
  
ArrayList<String> elements = new ArrayList<String>();
readElementsToArrayListAndPrint(br, elements);
ArrayList<String> rankedElements = new ArrayList<String>(elements.size());
for (int i = 0; i < elements.size(); i++){
rankedElements.add("");
}
ArrayList<String> elementComments = new ArrayList<String>(elements.size());
for (int i = 0; i < elements.size(); i++){
elementComments.add("");
}
Scanner sc400 = new Scanner(System.in);
//Main Loop
for (int i = 0; i < elements.size(); i++){
System.out.println("#" + (i+1) + ": " + elements.get(i));
//Collect comment.
System.out.println("Please write any comments you have about this element!");
String comment;
try {
comment = sc400.nextLine();
} catch (Exception e2) {
comment = "No comment.";
}
//Collect Rank
int rank = -1;
while (rank == -1) {
System.out.println("How would you like to rank this element?");
try {
rank = sc400.nextInt();
if (!isRational(rank, elements.size())){
rank = -1;
System.out.println("Please enter an integer that is between 1 and "
+ elements.size() + ".");
} else {
rank--;
}
} catch (Exception e) {
System.out.println("Please enter a valid integer input.");
sc400.nextLine();
}
sc400.nextLine();
}
if (rankedElements.get(rank) == ""){ //Rank was unoccupied
System.out.println("Rank accepted without incident!");
placeElementAtPosition(rank, elements.get(i), comment, rankedElements, elementComments);
} else { //Rank was occupied
System.out.print("Rank already in use...");
if (pushDown(rank, rankedElements, elementComments)){ //Elements can/were pushed
System.out.println("Elements at rank " + (rank+1) + " were pushed "
+ "down to accommodate the current element.");
placeElementAtPosition(rank, elements.get(i), comment, rankedElements, elementComments);
} else { //Elements could not be pushed
System.out.println("Elements beyond rank " + (rank+1) + " were all taken. "
+ "Placing element at next lowest rank above " + (rank+1) + ".");
placeElementAtLowestRank(rank, elements.get(i), comment, rankedElements, elementComments);
}
}
}
sc400.close();
//Display results
System.out.println("Results!");
for(int i = 0; i < rankedElements.size(); i++){
System.out.println("'" + rankedElements.get(i) + "' | " + (i+1) + " | " + elementComments.get(i));
}
//Bye
System.out.println("Thanks for running me! Have an awesome day!");
}
public static void addMoreElements() throws IOException{
   Scanner scan = new Scanner(System.in);
   String s = "";
   while(true){
   System.out.println("Please add an element");
   s = s + scan.next() + " ";
   System.out.println("Do you want to add anymore elements? y for yes and n for No");
   char c = scan.next().charAt(0);
   if(c == 'n' || c == 'N'){
       overWriteElements(s);
       break;
   }
     
   }
}
public static void overWriteElements(String s) throws IOException{
     
   BufferedWriter bw = new BufferedWriter(new FileWriter(filepath, true));
   bw.write(" "+s);
   bw.close();
}

/**
* SP - A
* Pushes the elements in two parallel arrayLists down at a common index. Does nothing
* if the elements beyond the rank are all already occupied.
* @param rank Index at which to begin pushing.
* @param ranked ArrayList of ranked elements
* @param comments Parallel ArrayList of element comments.
* @return True if elements were successfully pushed down. False if not.
*/
public static boolean pushDown(int rank, ArrayList<String> ranked, ArrayList<String> comments){
int i = rank;
for (; i < ranked.size() ; i++){
if (ranked.get(i) == ""){
break;
}
}
if (i == ranked.size()){ //for Loop traversed without finding an empty spot.
return false;
} else {
ranked.remove(i);
comments.remove(i);
ranked.add(rank, ""); //Leaves rank slot as empty.
comments.add(rank, "");
return true;
}
}
/**
* SP - B
* Places an element and its comment in two parallel ArrayLists at a given position.
* @param pos Position at which to insert element/comment.
* @param element Element to be inserted.
* @param comment Comment to be inserted.
* @param ranked ArrayList of ranked elements.
* @param comments Parallel ArrayList of element comments.
*/
public static void placeElementAtPosition(int pos, String element, String comment,
ArrayList<String> ranked, ArrayList<String> comments){
ranked.set(pos, element);
comments.set(pos, comment);
}
/**
* SP - C
* Places an element an d its comment in the first available position above the given
* one in two parallel ArrayLists.
* @param pos Position above which to search for a spot.
* @param element Element to be inserted.
* @param comment Comment to be inserted.
* @param ranked ArrayList of ranked elements.
* @param comments Parallel ArrayList of element comments.
*/
public static void placeElementAtLowestRank(int pos, String element, String comment,
ArrayList<String> ranked, ArrayList<String> comments){
for(pos--; pos >= 0; pos--){
if(ranked.get(pos) == ""){
ranked.set(pos, element);
comments.set(pos, comment);
System.out.println("Element was placed at rank " + (pos+1) + ".");
break;
}
}
}
/**
* For each string in a given file, the string is trimmed, inserted into an arraylist,
* and printed.
* @param sc Scanner with preloaded element file.
* @param elements Empty ArrayList of strings.
*/
public static void readElementsToArrayListAndPrint(BufferedReader br, ArrayList<String> elements){
String element = "";
try {
element = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Oops! Something went wrong!");
e.printStackTrace();
}
while (element != null) {
element = element.trim(); //Just in case there are trailing/leading spaces.
elements.add(element);
System.out.println(element);
try {
element = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Oops! Something went wrong!");
e.printStackTrace();
}
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Oops! Something went wrong!");
e.printStackTrace();
}
}
/**
* Checks to see if the input is between the bounds of 1 and n, where n is the number
* of elements in an array.
* @param input int value to be checked
* @param size number of elements in the corresponding array
* @return True if input is within bounds. Otherwise false.
*/
public static boolean isRational(int input, int size){
if (input > 0 && input < size){
return true;
}
return false;
}
}

Output:

Please add an element
F
Do you want to add anymore elements? y for yes and n for No
y
Please add an element
G
Do you want to add anymore elements? y for yes and n for No
y
Please add an element
H
Do you want to add anymore elements? y for yes and n for No
n
A
B
C
D
E
F
G
H
#1: A
Please write any comments you have about this element!
Comment1
How would you like to rank this element?
4
Rank accepted without incident!
#2: B
Please write any comments you have about this element!
Comment2
How would you like to rank this element?
4
Rank already in use...Elements at rank 4 were pushed down to accommodate the current element.
#3: C
Please write any comments you have about this element!
Comment3
How would you like to rank this element?
4
Rank already in use...Elements at rank 4 were pushed down to accommodate the current element.
#4: D
Please write any comments you have about this element!
Comment4
How would you like to rank this element?
5
Rank already in use...Elements at rank 5 were pushed down to accommodate the current element.
#5: E
Please write any comments you have about this element!
Comment6
How would you like to rank this element?
6
Rank already in use...Elements at rank 6 were pushed down to accommodate the current element.
#6: F
Please write any comments you have about this element!
Comment7
How would you like to rank this element?
7
Rank already in use...Elements beyond rank 7 were all taken. Placing element at next lowest rank above 7.
Element was placed at rank 3.
#7: G
Please write any comments you have about this element!
Comment
How would you like to rank this element?
8
Please enter an integer that is between 1 and 8.
How would you like to rank this element?
2
Rank accepted without incident!
#8: H
Please write any comments you have about this element!
Comment9
How would you like to rank this element?
2
Rank already in use...Elements beyond rank 2 were all taken. Placing element at next lowest rank above 2.
Element was placed at rank 1.
Results!
'H' | 1 | Comment9
'G' | 2 | Comment
'F' | 3 | Comment7
'C' | 4 | Comment3
'D' | 5 | Comment4
'E' | 6 | Comment6
'B' | 7 | Comment2
'A' | 8 | Comment1
Thanks for running me! Have an awesome day!