Results 1 to 6 of 6

Thread: MobyDick Words

  1. #1
    kevorski is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Could you please post the complete error message here to see. And, are you sure that you've specify the file path properly?

  3. #3
    kevorski is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    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

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You mean want to count words which contain letter 'a', then 'b', after that 'c', and so on... ?

  5. #5
    kevorski is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    yeah that....that is currently what I'm working on. got any ideas? without using an arraylist?

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Duplicating words
    By huntrguy102 in forum New To Java
    Replies: 1
    Last Post: 11-01-2010, 01:00 AM
  2. Help: Num to words
    By MyOnlineChurva in forum New To Java
    Replies: 16
    Last Post: 01-08-2010, 02:41 AM
  3. Money to Words
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-30-2008, 05:27 AM
  4. Scrambling Words
    By Shadow22202 in forum New To Java
    Replies: 9
    Last Post: 04-30-2008, 04:51 AM
  5. help w words
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 11-21-2007, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •