Results 1 to 3 of 3
Thread: Need help with assignment.
- 04-26-2011, 01:15 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
Need help with assignment.
Well, once again I'm back and have an assignment due tonight and I am so lost. I am having a hard time figuring out how to really even get started with this code. Any help would be appreciated. The assignment reads:
Evey seven-letter phone number corresponds to many seven-letter words, but most of these words represent unrecognizable juxstapositions of letters. It's possible, however, that the owner of a barbershop would be pleased to know that the shop's telephone number, 424-7288, corresponds to "HAIRCUT." A veterinarian with the phone number 738-2273 would be pleased to know that the number corresponds to the letters "PETCARE."
Write a program that, given a seven digit (phone) number, uses a PrintStream object to write to a file every possible seven-letter word combination corresponding to that number. There are 2,187 (3^7) such combinations. Avoid phone numbers with the digits 0 and 1.
Some of the code was provided for me and I've started it but don't even know if I'm on the right track or if I'm way off base. Printing and reading from a file confuses me so much. Here is the code:
Any help anyone could give me I'd appreciate it. I've been looking at this assignment all weekend and haven't made heads or tails of it.Java Code:import java.util.Formatter; /** * * @author * @version Spring 2011 * */ public class PhoneNumber { /** * This method writes all possible character combinations represented by * 'phoneNumber' into the file 'filename'. The user is then responsible * for looking through the file to determine if any of these combinations * are real words * * @param phoneNumber is the 7 digit phone number in question * @param filename indicates where to store this file. */ /** * Wouldn't it be nice if you had a method that took an int and returned * the associated char[] ? */ public static void writeToFile(int phoneNumber, String filename){ //This is the code that I added but I'm not sure what I'm doing here. Formatter out = new Formatter("filename"); for (int i = 1; i < 7; i++){ System.out.print(i + " \n"); out.format(i+"\n"); } } /** * This function takes 7 arrays and prints all combinations of the characters to the screen * You may find this useful */ public static void writeHelper(String filename, char a1[], char a2[], char a3[], char a4[],char a5[],char a6[],char a7[]){ for (int i = 0; i < a1.length; i++){ for (int j = 0; j < a2.length; j++){ for (int k = 0; k < a3.length; k++){ for (int l = 0; l < a4.length; l++){ for (int m = 0; m < a5.length; m++){ for (int n = 0; n < a6.length; n++){ for (int o = 0; o < a7.length; o++){ System.out.println( "" + a1[i] + a2[j]+ a3[k]+ a4[l]+ a5[m]+ a6[n]+ a7[o]); } } } } } } } } public static void main(String[] args) throws Exception { writeToFile(6862377, "Numbers.txt"); writeToFile(8253688, "Takeout.txt"); } }
-
There's no direct answerable question in your post, just that your confused with file handling and a code dump, so about the best that I can do is point you to the file output tutorial. Perhaps someone can give you better help, but if not, you might do well to tell us what is wrong with your program, what errors you are having, and what specifically confuses you.
Edit: per your instructions: "uses a PrintStream object to write to a file",
You should create a PrintStream object before your loops and write to this object. The PrintStream API will tell what method(s) to use to output text with this object (hint, System.out is a PrintStream object and you know how to output with this object as your code demonstrates).Last edited by Fubarable; 04-26-2011 at 02:22 AM.
- 04-26-2011, 03:47 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
assignment
By mr.othman in forum New To JavaReplies: 4Last Post: 12-03-2010, 02:55 PM -
I am looking for help with an assignment
By nanoo51969 in forum New To JavaReplies: 1Last Post: 03-23-2009, 09:41 PM -
GUI First Assignment-DUE 8/1/08
By ljk8950 in forum AWT / SwingReplies: 2Last Post: 08-01-2008, 04:23 AM -
First GUI Assignment
By ljk8950 in forum New To JavaReplies: 1Last Post: 07-31-2008, 07:29 AM


LinkBack URL
About LinkBacks


Bookmarks