Results 1 to 5 of 5
  1. #1
    MindFlyer is offline Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Default Java, Number of n-words in a text

    The purpose of this class is to count the number of occurrences of every n-character words in a text entered by the user. I think there are some mistakes in the method occurrenceNumber because I obtain 228 for every n-character word. Where is the error ?

    Java Code:
    import java.io.*;
    import java.util.*;
    
    
    public class Test {
    
    	
    	public static void main(String[] args) {
    		
    		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("Enter the integer n");
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		char[] array = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', ' '}; // our alphabet inlcudes the blank too
    		StringBuffer[] table = createTotalTable(n, array);
    		
    		System.out.println("Enter the text ");
    		StringBuffer text = new StringBuffer("");
    		try {
    			text = new StringBuffer(in.readLine());
    		}catch(IOException a){
    			System.out.println("Input-Output problem");
    		}
    		StringBuffer text_formatted = format(text.toString());
    		System.out.println("The formatted text is \n" + text_formatted.toString() + "\n");
    		
    		System.out.println("Now we print all the n-character words in alphabetic order. Press enter to proceed. ");
    		try {
    			in.readLine();
    		}catch(IOException b){
    			System.out.println("Input-Output problem");
    		}	
    		for (StringBuffer word : table)
                System.out.println(word.toString());
    		
    		int[] occurrenceTable = createList(text_formatted, table, n);
    		
    		System.out.println("Now we print all the n-words contained in the text with the number of occurrences. Press enter to proceed.");
    		try {
    			in.readLine();
    		}catch(IOException c){
    			System.out.println("Input-Output problem");
    		}	
    		
    		for(int u = 0; u < pow(27, n); u++) 
        		System.out.println(table[u].toString() + ", " + occurrenceTable[u]);
    		
    		
    		sc.close();
    		
    	
    		
    	}
    
    	
    	public static StringBuffer[] createTotalTable(int n, char[] a) {  // this method create an array containing all the n-words in alphabetic order 
    	    
    		 
    		 StringBuffer[] table = new StringBuffer[ pow(27, n)];
    		 for(int w =0; w < pow(27, n); w++)
    			 table[w] = new StringBuffer("");
    		 
    		 for(int h = 1; h <= n ; h++){
    			for(int u =0; u < pow ( 27, h-1); u++){
    				
    			for (int j = 0; j<27; j++){
    				
    				for( int x =pow(27, n-h+1)*u + pow(27, n-h)*j; x< pow(27, n-h+1)*u + pow(27, n-h)*(j+1); x++)
    					table[x] = table[x].append(a[j]);
    			}
    				
    			}
    			
    		 }
    		 
    		 return table;
    	}
    
    	
    	public static int pow(int a, int b){  // the method Math.pow modified
       	 
       	 int tot = 1;
       	 for(int i =0; i<b ; i++)
       		 tot = a * tot ;
       	 
       	 return tot;
        }
    	
    	
    	public static int occurrenceNumber ( StringBuffer testo, StringBuffer parola, int n){  // this method is aimed to calculate the number of occurrences of a word of length n in a text
            int tot =0;
            
    		 if(n > testo.length())
               System.out.println("The integer is bigger than the text's length ");
    		 else{
    			for(int i = 0; i <= testo.length() - n; i++ ){
    				if((testo.substring(i,i+n)).equals(parola.toString()));
    					tot += 1;
    			
    			}	
    			
    		 }
    
    		 return tot;
           }
    	
    
    	
    	public static int[] createList (StringBuffer str , StringBuffer[] tabella, int n){   // this method is aimed to create an array containing for every position the number of occurrences of the corresponding word in the text 
    		
    		int[] occurrenceTable = new int[pow(27,n)];
    		
    		for(int i =0; i < pow(27, n); i++ ) 
    		occurrenceTable[i] = occurrenceNumber(str, tabella[i], n);
    		
    		
    		
    		return occurrenceTable;
    		
    	}
    
    
    	
    	public static StringBuffer  format (String s){  // this method is aimed to eliminate non-alphabetic characters and multiple spaces
           
    
    s = s.toLowerCase();
    StringBuffer b = new StringBuffer();
    int m = s.length();
    int conta_spazi = 0;
    StringBuffer h = new StringBuffer(s);
    for (int i = 0; i < m ; i++ ){
    switch(h.charAt(i))	{
    case 'a':
    break;
    
    case 'A':
    break;
    
    case 'b':
    break;
    
    case 'B':
    break;
    
    case 'c':
    break;
    
    case 'C':
    break;
    
    
    case 'd':
    break;
    
    case 'D':
    break;
    
    case 'e':
    break;
    
    case 'E':
    break;
    
    case 'f':
    break;
    
    case 'F':
    break;
    
    case 'g':
    break;
    
    case 'G':
    break;
    
    case 'h':
    break;
    
    case 'H':
    break;
    
    case 'i':
    break;
    
    case 'I':
    break;
    
    case 'j':
    break;
    
    case 'J':
    break;
    
    case 'k':
    break;
    
    case 'K':
    break;
    
    case 'l':
    break;
    
    case 'L':
    break;
    
    case 'm':
    break;
    
    case 'M':
    break;
    
    case 'n':
    break;
    
    case 'N':
    break;
    
    case 'o':
    break;
    
    case 'O':
    break;
    
    case 'p':
    break;
    
    case 'P':
    break;
    
    case 'q':
    break;
    
    case 'Q':
    break;
    
    case 'r':
    break;
    
    case 'R':
    break;
    
    case 's':
    break;
    
    case 'S':
    break;
    
    case 't':
    break;
    
    case 'T':
    break;
    
    case 'u':
    break;
    
    case 'U':
    break;
    
    case 'v':
    break;
    
    case 'V':
    break;
    
    case 'w':
    break;
    
    case 'W':
    break;
    
    case 'x':
    break;
    
    case 'X':
    break;
    
    case 'y':
    break;
    
    case 'Y':
    break;
    
    case 'z':
    break;
    
    case 'Z':
    break;
    
    default:
    h.setCharAt(i, ' ');
    
    
    }
    }
    for (int i = 0; i < m ; i++ ){
    
    if ( h.charAt(i) == ' ' )
    conta_spazi++;
    else
    conta_spazi = 0;
    
    if ( conta_spazi <= 1)
    b = b.append(h.charAt(i));
    
    }
    
    return b;
    
    }
    	
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Java, Number of n-words in a text

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Java, Number of n-words in a text


  4. #4
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Java, Number of n-words in a text

    I think you should review your logic, and should think about it again .

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,876
    Rep Power
    5

    Default Re: Java, Number of n-words in a text

    Quote Originally Posted by allaudin View Post
    I think you should review your logic, and should think about it again .
    its not bad advice at all, but generally the trigger to give it is when the OP stops thinking for him/herself and goes into a "I changed it, and now what? and now what? and now what?" cycle ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Number of words in file. Help.
    By itskeith in forum New To Java
    Replies: 1
    Last Post: 12-02-2012, 11:14 PM
  2. Adding words to a text file using java
    By Andie in forum New To Java
    Replies: 0
    Last Post: 04-21-2012, 08:14 PM
  3. total number of words starting with a vowel
    By somnath6088 in forum New To Java
    Replies: 4
    Last Post: 11-08-2010, 09:52 AM
  4. Total number of reserved words in java
    By vivvy in forum New To Java
    Replies: 1
    Last Post: 02-06-2010, 03:18 PM
  5. Replies: 1
    Last Post: 07-02-2008, 01:58 PM

Tags for this Thread

Posting Permissions

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