Как мне заполнить этот код?
Может ли кто - нибудь помочь мне завершить этот код? Это программирование на Java. Я застрял и не могу завершить его. Может ли кто-нибудь объяснить мне это как можно скорее?!?!?
Часть 3: аранжировщик анаграмм (20 очков)
Specifications The program should welcome the user with the message Welcome to the Anagram Arranger! It should then prompt the user to enter the name of a file. Provided the user enters a correct file name (see section regarding error checking below), then the program will read in all of the words in the file, one-by-one. Each word should be stored in a List of Characters or Strings (each String being one letter). It will display the word, along with its corresponding number, with the message: Word #<count> is <word> See below for examples. The user will then be prompted to enter the position of two different letters in the word that the user wants to be swapped. The program will verify the user choice by re-printing the word with carrots beneath the selected letters. The user will then be required to confirm his or her choice with the message: Enter the position numbers of the two letters you wish to swap: The program should accept four different input options from the user -- y, Y, yes, and Yes -- to indicate consent. If the user indicates "no", the word will be reprinted with a prompt to enter the position of two different letters. If the user enters yes, then the program should swap the two selected letters, and then prompt the user to indicate whether he or she would like to swap additional letters in the word. The program should accept four different input options from the user -- y, Y, yes, and Yes -- to indicate consent. Examples: Welcome to the Anagram Arranger! Please enter the name of your input file: spooky.txt Word #1 is zombie 1: z 2: o 3: m 4: b 5: i 6: e Enter the position numbers of the two letters you wish to swap: 1 2 z o m b i e ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: o z m b i e Want to keep rearranging? (y/n): y 1: o 2: z 3: m 4: b 5: i 6: e Welcome to the Anagram Arranger! Please enter the name of your input file: abc.txt Sorry. I cannot find a file by that name! Please enter the name of a valid input file: 123.txt Sorry. I cannot find a file by that name! Please enter the name of a valid input file: words.txt Word #1 is Spring 1: S 2: p 3: r 4: i 5: n 6: g Enter the position numbers of the two letters you wish to swap: 3 5 S p r i n g ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: S p n i r g Want to keep rearranging? (y/n): yes 1: S 2: p 3: n 4: i 5: r 6: g Enter the position numbers of the two letters you wish to swap: 1 6 S p n i r g ^ ^ Are these the letters you wish to swap? (y/n): yes The new word is: g p n i r S What I have tried: <pre lang="java">
This is my code: <pre><pre lang="java">
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.NoSuchElementException; import java.util.Scanner; /** * Class for Anagram * * @author Brandon Zhang * @author Melisa Sever */ public class Anagram { private static ArrayList<List<Character>> wordList = null; public static void main(String[] args) { stuff(); } public static void stuff() { Scanner scanner = new Scanner(System.in); String filename; boolean validName = false; System.out.println("Welcome to the Anagram Arranger!\n"); while(!validName) { System.out.print("Please enter the name of your input file: \n"); filename = "input.txt";// = scanner.next(); try { readInput(filename); validName = true; }catch(Exception e) { System.out.println("\nSorry. I cannot find a file by that name!"); } } for(int i=0;i<wordList.size();i++) { rearranger(i); } } /** * Returns a String array of the input file separated by line * * @precondition filename must be a valid filename * @param String filename */ public static void readInput(String filename){ try { File file = new File(filename); FileReader fr = new FileReader(file); BufferedReader br=new BufferedReader(fr); String line; int lineIndex = 0; ArrayList<List<Character>> wordList = new ArrayList<List<Character>>(); while((line = br.readLine()) != null) { wordList.add(new List<Character>()); for(int i = 0;i<line.length();i++) { wordList.get(lineIndex).addLast(line.charAt(i)); } lineIndex++; } fr.close(); br.close(); //debugging for(List<Character> list : wordList) { System.out.println(list); } }catch(Exception e) { //e.printStackTrace(); } } public static void rearranger(int index) { List<Character> list = wordList.get(index); String word = list.toString().replaceAll ("\\s, ", word); System.out.println("\nWord #" + (index+1)"is"+ word); System.out.println(list.printNumberedList()); public static getPosition() { Scanner scanner = new Scanner (System.in); int index1,index2; System.out.print("Enter the position numbers of the letter you wish to swap: "); index= position.replaceAll("\\s, replacement") } } }