3 Replies - 293 Views - Last Post: 26 March 2012 - 09:17 AM Rate Topic: -----

#1 ca7747  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-October 11

length() for a string keeps turning out zero

Posted 26 March 2012 - 06:11 AM

Hi, in the printToScreen()/printToFile(PrintWriter out) methods, the text.length() keeps turning out zero and making the percents zero. Can anyone tell me why?
import java.io.*; 
 import java.io.PrintWriter; 
 
 public class FrequencyAnalysis 
 { 
    private int[] frequency; 
    private String text; 
 
    public FrequencyAnalysis(String str) 
    { 
    text = str.toUpperCase(); // has ascii value 65-90 
    frequency = new int[26]; // counting array 
   } 
   // this method is complete 
    public void findFrequency() 
    { 
        for(int x = 65; x < 91; x++) 
        { 
            for(int y = 0; y < text.length(); y++) 
            { 
                if(text.substring(y, y+1).equals(Character.toString((char)x)) )
                frequency[x - 65]++; 
 
            } 

        } 
    } 
 
    public void printToScreen() 
    { 
 
        
       System.out.printf("%15s%15s%n", "Frequency", "Percent"); 
        for(int x = 65; x < 91; x++) 
        { 
       String letter = Character.toString((char)x); 
       double percentage = frequency[x - 65] / text.length(); 
       System.out.printf("%3s%10d%15.1f%n", letter, frequency[x-65], percentage); 
 
      } 
    
    } 
    
    public void printToFile(PrintWriter out)
    {
        out.printf("%15s%15s%n", "Frequency", "Percent"); 
        for(int x = 65; x < 91; x++) 
        { 
       String letter = Character.toString((char)x); 
       double percentage = frequency[x - 65] / text.length(); 
       out.printf("%3s%10d%15.1f%n", letter, frequency[x-65], percentage); 
 
      } 
    }
 
} 



here is the tester class

import java.util.*;
import java.io.*;
public class FrequencyAnalysisTester 
{ 
    public static void main(String args[]) throws IOException 
    { 
        Scanner input = new Scanner(new File("plaintext.txt")); 
        PrintWriter output = new PrintWriter("plaintextfreq.txt"); 
        String message = ""; 
        while (input.hasNext()) { 
            message += input.next(); 
        } 
        input.close(); 

        FrequencyAnalysis fa = new FrequencyAnalysis(message); 
        fa.findFrequency(); 
        fa.printToScreen(); 
        fa.printToFile(output); 
        output.close(); 
    } 
} 




Is This A Good Question/Topic? 0
  • +

Replies To: length() for a string keeps turning out zero

#2 farrell2k  Icon User is offline

  • 1.21 Jiggawatts!
  • member icon

Reputation: 566
  • View blog
  • Posts: 1,736
  • Joined: 29-July 11

Re: length() for a string keeps turning out zero

Posted 26 March 2012 - 06:26 AM

In yout tester class, before you create your FrequencyAnalysis object, do a println(message.length); I bet it's empty.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,788
  • Joined: 20-September 08

Re: length() for a string keeps turning out zero

Posted 26 March 2012 - 07:26 AM

You need to do decimal calculations with double, not int operands. You can also try

double percentage = 1.0 * frequency[x - 65] / text.length();


Also the body of findFrequency only need be



for(int y = 0; y < text.length(); y++) {
	frequency[text.charAt(y) - 'A']++;
}

This post has been edited by g00se: 26 March 2012 - 07:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5427
  • View blog
  • Posts: 8,727
  • Joined: 19-March 11

Re: length() for a string keeps turning out zero

Posted 26 March 2012 - 09:17 AM

double percentage = frequency[x - 65] / text.length();



If text.length() is zero, this would kick you out a divide by zero exception. Have you checked to see what the value of text.length() actually is at this point? System.out.println is your friend!

goose is right, if percentage is coming out zero, then this is a problem of integer arithmetic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1