So I got it working with the file and adding to the list and writing back to the
ID: 3831898 • Letter: S
Question
So I got it working with the file and adding to the list and writing back to the file. The ranking portion is difficult and I can't figure out what to do. I have ideas but I haven't tried. Can you please help me add ranking algorithims to this code in the javal language. The group.txt is users/KillaQ/downloads/listoffavorites.txt . I dont have a sample output right now, but i feel im in the right direction.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
//import java.util.ListIterator;
import java.util.Scanner;
//import org.w3c.dom.Text;
public class GroupProjectH {
static ArrayList myFile = new ArrayList ();
static ArrayList finalList = new ArrayList ();
static ListIterator iter = finalList.listIterator();
public static void main(String[] args) {
BufferedReader reader = null;
// ArrayList myFile = new ArrayList ();
Boolean error = false;
String file = "";
Scanner input = new Scanner(System.in);
Boolean loop = true;
Boolean play = true;
String add = "";
do {
try {
System.out.println("Welcome to Micro$oft Ranking Pro v.1.3.3.7");
System.out.println("");
System.out.println("Please enter a file including the directory. e.g. C:\users\username\Desktop\mary.txt ");
file = "C:\users\tsvet\Desktop\group.txt"; //input.next();
String sCurrentLine = "";
reader = new BufferedReader(new FileReader(file)); //C:\users\tsvet\Desktop\mary.txt
System.out.println("");
System.out.println("Your list is currently:");
while ((sCurrentLine = reader.readLine()) != null) {
System.out.println(sCurrentLine);
myFile.add(sCurrentLine);
}
error = false;
} catch (IOException e) {
System.out.println("Directory or file |* "+ file + " *| not found, please try again.");
error = true;
} finally {
try {
if (reader != null)reader.close();
} catch (IOException ex) {
System.out.println("try again");
}
}
} while (error == true);
// ADDING NEW
boolean addDelete = false;
// do { // do while for looping
do {
System.out.println("Would you like to add anything else to the list? (Y/N)");
addDelete = verifyYorN();
if (addDelete == true) {
System.out.println("What do you want to add:");
add = input.next();
myFile.add(add);
}
} while (addDelete == true);
System.out.println("The current list is:");
for (int i = 0; i < myFile.size(); i++) {
System.out.println(myFile.get(i));
}
// DELETE
addDelete = false;
int fileSize = myFile.size();
do {
System.out.println("Would you like to delete anything else to the list? (Y/N)");
addDelete = verifyYorN();
if (addDelete == true) {
System.out.println("What do you want to delete:");
add = input.next();
for (int i = 0; i < fileSize; i++) {
if (myFile.get(i).equalsIgnoreCase(add)) {
myFile.remove(i);
}
}
}
} while (addDelete == true);
System.out.println("The final list is:");
for (int i = 0; i < myFile.size(); i++) {
System.out.println(myFile.get(i));
}
for (int i = 0; i < myFile.size(); i++) {
finalList.add("");
}
// BEGIN RANKING
boolean rankError = false;
int myFileSize = myFile.size();
String comment = "";
int[] rank = new int[myFile.size()]; // ranking array!
for (int i = 0; i < myFile.size(); i++) {
rank[i] = 0;
}
for (int i = 0; i < myFile.size(); i++) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter rank for " + myFile.get(i)); //list
do{
try{
int r = sc.nextInt();
r = r + 1;
if (r > 1 && r <= myFileSize + 1) {
if (finalList.listIterator().equals("")) {
finalList.listIterator().next();
finalList.listIterator().remove();
finalList.listIterator().add(myFile.get(i));
} else {
if (finalList.listIterator().next().equals("")) {
continue;
}
if (finalList.listIterator().equals("")) {
//finalList.listIterator().next();
finalList.listIterator().remove();
finalList.listIterator().add(myFile.get(i));
}
}
//
rankError = false;
} else {
System.out.println("Number to rank is out of bounds! Try again!");
rankError = true;
}
}catch(Exception e){
rankError = true;
System.out.println("Not a number! Please try again.");
//e.printStackTrace();
}
} while(rankError == true);
}
//Collections.sort(myFile);
System.out.println("The FINAL list is:");
for (int i = 0; i < finalList.size(); i++) {
System.out.println(finalList.get(i));
}
//loop = verifyYorN();
// } while (loop == true);
} // end main
public static boolean verifyYorN() {
Scanner input = new Scanner(System.in);
boolean yesorno = false;
while( yesorno == false ) {
// System.out.print("Do you wish to search file again? ( Y or N ): ");
String inputUser = input.next();
if(inputUser.equalsIgnoreCase("Y")) {
return true;
}
if(inputUser.equalsIgnoreCase("N")) {
System.out.println("");
// System.out.println("Thank you for ranking, have a great day!");
return false;
}
System.out.println("I'm sorry. Please use ( Y or N )");
}
return false;
}
} // end class
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
//import java.util.ListIterator;
import java.util.Scanner;
//import org.w3c.dom.Text;
public class GroupProjectH {
static ArrayList myFile = new ArrayList ();
static ArrayList finalList = new ArrayList ();
static ListIterator iter = finalList.listIterator();
public static void main(String[] args) {
BufferedReader reader = null;
// ArrayList myFile = new ArrayList ();
Boolean error = false;
String file = "";
Scanner input = new Scanner(System.in);
Boolean loop = true;
Boolean play = true;
String add = "";
do {
try {
System.out.println("Welcome to Micro$oft Ranking Pro v.1.3.3.7");
System.out.println("");
System.out.println("Please enter a file including the directory. e.g. C:\users\username\Desktop\mary.txt ");
file = "C:\users\tsvet\Desktop\group.txt"; //input.next();
String sCurrentLine = "";
reader = new BufferedReader(new FileReader(file)); //C:\users\tsvet\Desktop\mary.txt
System.out.println("");
System.out.println("Your list is currently:");
while ((sCurrentLine = reader.readLine()) != null) {
System.out.println(sCurrentLine);
myFile.add(sCurrentLine);
}
error = false;
} catch (IOException e) {
System.out.println("Directory or file |* "+ file + " *| not found, please try again.");
error = true;
} finally {
try {
if (reader != null)reader.close();
} catch (IOException ex) {
System.out.println("try again");
}
}
} while (error == true);
// ADDING NEW
boolean addDelete = false;
// do { // do while for looping
do {
System.out.println("Would you like to add anything else to the list? (Y/N)");
addDelete = verifyYorN();
if (addDelete == true) {
System.out.println("What do you want to add:");
add = input.next();
myFile.add(add);
}
} while (addDelete == true);
System.out.println("The current list is:");
for (int i = 0; i < myFile.size(); i++) {
System.out.println(myFile.get(i));
}
// DELETE
addDelete = false;
int fileSize = myFile.size();
do {
System.out.println("Would you like to delete anything else to the list? (Y/N)");
addDelete = verifyYorN();
if (addDelete == true) {
System.out.println("What do you want to delete:");
add = input.next();
for (int i = 0; i < fileSize; i++) {
if (myFile.get(i).equalsIgnoreCase(add)) {
myFile.remove(i);
}
}
}
} while (addDelete == true);
System.out.println("The final list is:");
for (int i = 0; i < myFile.size(); i++) {
System.out.println(myFile.get(i));
}
for (int i = 0; i < myFile.size(); i++) {
finalList.add("");
}
// BEGIN RANKING
boolean rankError = false;
int myFileSize = myFile.size();
String comment = "";
int[] rank = new int[myFile.size()]; // ranking array!
for (int i = 0; i < myFile.size(); i++) {
rank[i] = 0;
}
for (int i = 0; i < myFile.size(); i++) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter rank for " + myFile.get(i)); //list
do{
try{
int r = sc.nextInt();
r = r + 1;
if (r > 1 && r <= myFileSize + 1) {
if (finalList.listIterator().equals("")) {
finalList.listIterator().next();
finalList.listIterator().remove();
finalList.listIterator().add(myFile.get(i));
} else {
if (finalList.listIterator().next().equals("")) {
continue;
}
if (finalList.listIterator().equals("")) {
//finalList.listIterator().next();
finalList.listIterator().remove();
finalList.listIterator().add(myFile.get(i));
}
}
//
rankError = false;
} else {
System.out.println("Number to rank is out of bounds! Try again!");
rankError = true;
}
}catch(Exception e){
rankError = true;
System.out.println("Not a number! Please try again.");
//e.printStackTrace();
}
} while(rankError == true);
}
//Collections.sort(myFile);
System.out.println("The FINAL list is:");
for (int i = 0; i < finalList.size(); i++) {
System.out.println(finalList.get(i));
}
System.out.println("");
// System.out.println("Thank you for ranking, have a great day!");
return false;
}
System.out.println("I'm sorry. Please use ( Y or N )");
}
return false;
}
} // end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.