Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone please help me to complete this code? This is Java Programming. I'm stuck and can not complete it. Can someone explain it to me ASAP?!?!?
Part 3: Anagram Arranger (20 pts)

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")
					
		}
		
	}
	
}
Posted
Updated 9-Oct-20 20:07pm

1 solution

Start by stopping and thinking about what you are doing - so far, you have grabbed your homework, created a source file and jumped straight into coding! That's not the way to go.

Start by reading the question carefully, and working out the data flow - what data you need to be handling, and where does it it to go?
You need to read a file and break it into words: you have created a method to do that and called it, but ... make it return the data. Yes, you have a global called wordList but you don't do anything with it, because the readInput method "hides" it behind a local variable of the same name, and returns no value at all.
Throw the global version away, and have readInput return the collection of words.
Then you can start working on making readInput do what it is supposed to: handle words.

Are words the same as lines? Is this line one word? I think not!

You need to have a collection of collections: each of the inner collection holds each character of the word as a separate item, but only holds one word, not a whole line. For the line above that would be:
'A','r','e'
'w','o','r','d','s'
't','h','e'
's','a','m','e'
'a','s'
'l','i','n','e','s','?'
'I','s'
't','h','i','s'
'l','i','n','e'
'o','n','e'
'w','o','r','d','?'
'I'
't','h','i','n','k'
'n','o','t','!'
With each word in a separate collection. So yo do that, you need to read lines, and then break them into individual words.

Your instructions are pretty well refined already, but have a look here: If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^] it may help you to start working out how to think about a problem and solve it!
Give it a try!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900