Results 1 to 6 of 6
Thread: MobyDick Words
- 11-02-2010, 02:37 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
MobyDick Words
Hello everyone,
so I have been doing an assignment the last couple hours and I have come to a block in the road.
Right now I can accept the file and display how many words are in the file. I can also get it to exit...hahahaha I'm working on getting the part where I need it to display a histogram of the letters that have a/A from a-z can anyone help me out? This is what I get so far, it compiles only error I get right now is the fact that I try to read in another file and it will ask for input then say "file not found" then ask for input again and I can only get it to error out 2 times if they enter the wrong file.
So if the entered text.txt twice and there was no file it would error out and close. Any help would be awesome.....I'm using jGrasp
Java Code:import java.util.*; import java.io.*; public class histogram { public static void main(String[] args) { Scanner kb = new Scanner(System.in); Scanner fin = null; int count = 0; int[] histogram = new int[27]; String fn; fn = readFileName(kb, "input"); fin = openFile(fn); // System.out.println(histogram); while(fin.hasNext()){ fin.nextLine(); count++; } System.out.println("The file " + fn + " has " + count + " words."); System.out.println(); int choice = 0; do{ choice = menu(kb); System.out.println(); if(choice == 1){ fn = readFileName(kb, "input"); fin = openFile(fn); System.out.println("The file " + fn + " has " + count + " words."); System.out.println(); } else if(choice == 2){ System.out.println("Working on this part"); System.out.println(); } else System.out.println("Bye Bye"); }while(choice != 3); }//end main public static int menu(Scanner kb) { int menuChoice = 0; do{ System.out.println("1) Read in a new set of words"); System.out.println("2) Display a histogram of all words where the first letter is from a-z or A-Z"); System.out.println("3) Quit"); System.out.print("Choice: "); menuChoice = kb.nextInt(); }while(menuChoice < 1 || menuChoice > 3); return menuChoice; } public static String readFileName(Scanner kb, String type) { System.out.print("Please enter the name of the file: "); return kb.nextLine(); } public static Scanner openFile(String filename) { Scanner kb = new Scanner(System.in); PrintStream fout = null; try{ return new Scanner(new File(filename)); } catch(FileNotFoundException e){ System.out.println("File was not found"); readFileName(kb, "input"); System.out.flush(); return null; } } public static Scanner openFile(Scanner kb) { Scanner fin = null; String fn; boolean okay = false; do{ fn = readFileName(kb, "input"); fin = openFile(fn); }while(fin == null); return fin; } /* public static int[] addWord(int[] array) { return 1; } */ }//end histogram
- 11-02-2010, 03:45 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Could you please post the complete error message here to see. And, are you sure that you've specify the file path properly?
- 11-02-2010, 03:47 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
i have gotten that working just need to create the arrays that will count the number of words that have the letter a, b, c, etc... all the way to z
- 11-02-2010, 03:59 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You mean want to count words which contain letter 'a', then 'b', after that 'c', and so on... ?
- 11-02-2010, 04:01 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
yeah that....that is currently what I'm working on. got any ideas? without using an arraylist?
- 11-02-2010, 05:03 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
ArrayList is the best option you've.
Similar Threads
-
Duplicating words
By huntrguy102 in forum New To JavaReplies: 1Last Post: 11-01-2010, 12:00 AM -
Help: Num to words
By MyOnlineChurva in forum New To JavaReplies: 16Last Post: 01-08-2010, 01:41 AM -
Money to Words
By javanewbie in forum New To JavaReplies: 2Last Post: 06-30-2008, 04:27 AM -
Scrambling Words
By Shadow22202 in forum New To JavaReplies: 9Last Post: 04-30-2008, 03:51 AM -
help w words
By Gilgamesh in forum New To JavaReplies: 5Last Post: 11-21-2007, 06:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks