Percentage

Hi I'm having problems with my program I'm trying to return a percentage of vowel A and E but i keep on getting

Exception in thread "main" java.lang.ArithmeticException: / by zero

Heres the program

/**********************************************************************

Text Analyser Program

Author: 0602471

**********************************************************************/

import java.util.Scanner;

import java.io.*;

public class TextAnalyse {

private int[] count = { 0, 0, 0, 0, 0, 0 };//This part of the code sets up an array to hold the values of whats been counted

private final int VOWEL = 4,

LETTER = 0,

DIGIT = 1,

WHITESPACE = 2,

OTHER = 3,

CONSONANT = 5;

private int[] number = {0, 0, 0, 0, 0};

private final int VOWELA = 0,

VOWELE = 1,//These are seperate from the other array

VOWELI = 2,

VOWELO = 3,//This part of the code sets up an array to hold the values for the individual vowel count

VOWELU = 4;

double PercentageA = 0, PercentageE =0;

public TextAnalyse ( String fileName ) {//open TextAnalyse Method

String inStr;

try {

File file = new File(fileName);//set up new file

Scanner fileScan = new Scanner(file);//set scanner to scan new file

while (fileScan.hasNextLine()) {//While filescan has a new line to read

inStr = fileScan.nextLine();//Read in the next line as a string

inStr = inStr.toLowerCase();// Convert to lower case

for (int i = 0; i < inStr.length(); i++) {//loop for the length of the string

char ch = inStr.charAt(i);//get the character at the current position

count[charCategory(ch)]++;//Increment the relavent number with the category that returns

}

}

fileScan.close();//If file not found send message to user

} catch (FileNotFoundException e) {

System.out.println("Text File was not found");

e.printStackTrace();

}

}//close textAnalyse Method

/********************************************************************************************************/

private int total(){ //open total method

// Adds LETTER, DIGIT, VOWEL, CONSONANT, WHITESPACE, OTHER, to give the total number of characters

return (count[DIGIT]+count[VOWEL]+count[CONSONANT]+count[WHITESPACE]+count[OTHER]);

}// close total method

/*******************************************************************************************************/

private double Percentage; {

PercentageA = (number[VOWELA] / count[VOWEL] * 100);

PercentageE = (number[VOWELE] / count[VOWEL] * 100);

}

/*******************************************************************************************************/

public void display () {// Display to user

System.out.println("Letters: "+count[LETTER]);//Printing total number of letters

System.out.println("");

System.out.println("Digits: "+count[DIGIT]);//printing total number of digits

System.out.println("");

System.out.println("Vowels: "+count[VOWEL]); //Printing total number of vowels

System.out.println("");

System.out.println("Number of A: "+number[VOWELA]);//Printing the number of A

System.out.println("");

System.out.println("Number of E: "+number[VOWELE]);//Printing the number of E

System.out.println("");

System.out.println("Number of I: "+number[VOWELI]);//Printing the number of I

System.out.println("");

System.out.println("Number of O: "+number[VOWELO]);//Printing the number of O

System.out.println("");

System.out.println("Number of U: "+number[VOWELU]);//Printing the number of U

System.out.println("");

System.out.println("Consonants: "+count[CONSONANT]);// Printing total number of Consonants

System.out.println("");

System.out.println("Whitespace: "+count[WHITESPACE]);//Printing total number of Whitespace

System.out.println("");

System.out.println("Other printing character: "+count[OTHER]);//Printing total number of other characters

System.out.println("");

System.out.println("Total Number of characters: "+total());//Total number of all characters

System.out.println("");

/*******************************************************************************************************************************************************************************/

int common = findcommon(VOWEL, CONSONANT); //calls method below to get largest common devisible number

System.out.println("Ratio of Vowels and Consonants: " +(count[VOWEL]/common) + ":" + (count[CONSONANT]/common)); //devide both by devisible number and print

/*******************************************************************************************************************************************************************************/

int common2 = findcommon(total(), WHITESPACE); //calls method below to get largest common devisible number

System.out.println("");

System.out.println("Ratio of Printing to Non- Printing Characters: " +(total()/common2) + ":" + (count[WHITESPACE]/common2)); //devide both by devisible number and print

/*******************************************************************************************************************************************************************************/

System.out.println("");

System.out.println("Percentage of VOWEL A: " +PercentageA+ " % ");

System.out.println("");

System.out.println("Percentage of VOWEL E: " +PercentageE+ " % ");

/******************************************************************************************************************************************************************************/

}// close display method

/*******************************************************************************************************************************************************************************/

private int charCategory ( char ch ) { //open charCategory

if (Character.isDigit(ch))//If character is a digit return digit

return DIGIT;

else if (Character.isLetter(ch)){ //open character.isLetter method

if (Vowel(ch)){ //if vowel then record changes

count[VOWEL]++;

vowelcount(ch); // if its gonna be a vowel then send that char to the vowelcount method

}//close Vowel

else //else it'll be a consonant

count[CONSONANT]++;

return LETTER; //return letter anyway to keep track of letters counted

}//close character.isLetter method

else if (Character.isWhitespace(ch))//If theres whitespace return the amount of whitespace

return WHITESPACE;

else return OTHER;// else return other

} //close charCategory

/*********************************************************************************************************************/

// This method works out the ratio for the VOWELS and CONSONATS

// returns an int, takes in two ints

private static int findcommon(int VOW, int CON){//open findcommon method

// start from number two and work way down to 0

for (int x = CON; x > 0; x--){ //open if statement

//if both numbers devide by this number with no remainder then return it

if ( (VOW % x == 0) & (CON % x == 0) )

return x;

}//close the if statement

return 0; //only gets to this point if no number was found

}//close findcommon method

/**********************************************************************************************************************/

//This method works out the ratio for Non-Printing characters and Printing Characters

// returns an int, takes in two ints

private static int findcommon2(int TOT, int WHI) {//Open findcommon2 method

// start from number two and work way down to 0

for (int x = WHI; x > 0; x--){//open if statement

//if both numbers devide by this number with no remainder then return it

if ( (TOT % x == 0) & (WHI % x == 0) )

return x;

}//close if statement

return 0; //only gets to this point if no number was found

}//close findcommon2 method

/**********************************************************************************************************************/

private void vowelcount (char c) { //open vowelcount method

switch(c){ //open switch

//If character matches, then increment the relavent letter count and break away from the switch

case 'a': number[VOWELA]++; break;

case 'e': number[VOWELE]++; break;

case 'i': number[VOWELI]++; break;

case 'o': number[VOWELO]++; break;

case 'u': number[VOWELU]++; break;

}//close switch

}//close vowelcount Method

/********************************************************************************************************/

private static boolean Vowel ( char c ) {//If true return a Vowel

return c=='a' || c=='e' || c=='i' || c=='o' || c=='u';

}//close Vowel Method

}//close program

[9301 byte] By [shenmue232a] at [2007-11-26 22:41:52]
# 1
The error message tells you exactly what is wrong with your program: you are trying to divide by zero. I bet if you look at the error stack again, it will even tell you where.
ignignokt84a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 2
well i have tried adding rather than divide and i get 0.0
shenmue232a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 3
There is obviously an error in your algorithms then, try debugging.
ignignokt84a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 4

Why would you add to get a percentage?

double bottom = 0.0, percentage = 0.0, top = 0.0;

if (bottom == 0.0) {

percentage = 0.0;

} else {

percentage = top / bottom;

}

kevjavaa at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 5
well i was just trying to see if add did anything but it dont
shenmue232a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 6
> well i was just trying to see if add did anything but it dontOh good: programming by trial and error; how fascinating.kind regards,Jos ;-)
JosAHa at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 7

well now i get 0.0 i have changed the code + % gives a result but / does not

/**********************************************************************

Text Analyser Program

Author: 0602471

**********************************************************************/

import java.util.Scanner;

import java.io.*;

public class TextAnalyse {

private int[] count = { 0, 0, 0, 0, 0, 0 };//This part of the code sets up an array to hold the values of whats been counted

private final int VOWEL = 4,

LETTER = 0,

DIGIT = 1,

WHITESPACE = 2,

OTHER = 3,

CONSONANT = 5;

private int[] number = {0, 0, 0, 0, 0};

private final int VOWELA = 0,

VOWELE = 1,//These are seperate from the other array

VOWELI = 2,

VOWELO = 3,//This part of the code sets up an array to hold the values for the individual vowel count

VOWELU = 4;

double PercentageA = 0, PercentageE =0;

public TextAnalyse ( String fileName ) {//open TextAnalyse Method

String inStr;

try {

File file = new File(fileName);//set up new file

Scanner fileScan = new Scanner(file);//set scanner to scan new file

while (fileScan.hasNextLine()) {//While filescan has a new line to read

inStr = fileScan.nextLine();//Read in the next line as a string

inStr = inStr.toLowerCase();// Convert to lower case

for (int i = 0; i < inStr.length(); i++) {//loop for the length of the string

char ch = inStr.charAt(i);//get the character at the current position

count[charCategory(ch)]++;//Increment the relavent number with the category that returns

}

}

fileScan.close();//If file not found send message to user

} catch (FileNotFoundException e) {

System.out.println("Text File was not found");

e.printStackTrace();

}

}//close textAnalyse Method

/********************************************************************************************************/

private int total(){ //open total method

// Adds LETTER, DIGIT, VOWEL, CONSONANT, WHITESPACE, OTHER, to give the total number of characters

return (count[DIGIT]+count[VOWEL]+count[CONSONANT]+count[WHITESPACE]+count[OTHER]);

}// close total method

/*******************************************************************************************************/

public void display () {// Display to user

System.out.println("Letters: "+count[LETTER]);//Printing total number of letters

System.out.println("");

System.out.println("Digits: "+count[DIGIT]);//printing total number of digits

System.out.println("");

System.out.println("Vowels: "+count[VOWEL]); //Printing total number of vowels

System.out.println("");

System.out.println("Number of A: "+number[VOWELA]);//Printing the number of A

System.out.println("");

System.out.println("Number of E: "+number[VOWELE]);//Printing the number of E

System.out.println("");

System.out.println("Number of I: "+number[VOWELI]);//Printing the number of I

System.out.println("");

System.out.println("Number of O: "+number[VOWELO]);//Printing the number of O

System.out.println("");

System.out.println("Number of U: "+number[VOWELU]);//Printing the number of U

/*******************************************************************************************************************************************************************************/

PercentageA = number[VOWELA] / count[VOWEL] * 100;

PercentageE = number[VOWELE] / count[VOWEL] * 100;

System.out.println("");

System.out.println("Percentage of VOWEL A: " +PercentageA+ " % ");

System.out.println("");

System.out.println("Percentage of VOWEL E: " +PercentageE+ " % ");

/******************************************************************************************************************************************************************************/

System.out.println("");

System.out.println("Consonants: "+count[CONSONANT]);// Printing total number of Consonants

System.out.println("");

System.out.println("Whitespace: "+count[WHITESPACE]);//Printing total number of Whitespace

System.out.println("");

System.out.println("Other printing character: "+count[OTHER]);//Printing total number of other characters

System.out.println("");

System.out.println("Total Number of characters: "+total());//Total number of all characters

System.out.println("");

/*******************************************************************************************************************************************************************************/

int common = findcommon(VOWEL, CONSONANT); //calls method below to get largest common devisible number

System.out.println("Ratio of Vowels and Consonants: " +(count[VOWEL]/common) + ":" + (count[CONSONANT]/common)); //devide both by devisible number and print

/*******************************************************************************************************************************************************************************/

int common2 = findcommon(total(), WHITESPACE); //calls method below to get largest common devisible number

System.out.println("");

System.out.println("Ratio of Printing to Non- Printing Characters: " +(total()/common2) + ":" + (count[WHITESPACE]/common2)); //devide both by devisible number and print

}// close display method

/*******************************************************************************************************************************************************************************/

private int charCategory ( char ch ) { //open charCategory

if (Character.isDigit(ch))//If character is a digit return digit

return DIGIT;

else if (Character.isLetter(ch)){ //open character.isLetter method

if (Vowel(ch)){ //if vowel then record changes

count[VOWEL]++;

vowelcount(ch); // if its gonna be a vowel then send that char to the vowelcount method

}//close Vowel

else //else it'll be a consonant

count[CONSONANT]++;

return LETTER; //return letter anyway to keep track of letters counted

}//close character.isLetter method

else if (Character.isWhitespace(ch))//If theres whitespace return the amount of whitespace

return WHITESPACE;

else return OTHER;// else return other

} //close charCategory

/*********************************************************************************************************************/

// This method works out the ratio for the VOWELS and CONSONATS

// returns an int, takes in two ints

private static int findcommon(int VOW, int CON){//open findcommon method

// start from number two and work way down to 0

for (int x = CON; x > 0; x--){ //open if statement

//if both numbers devide by this number with no remainder then return it

if ( (VOW % x == 0) & (CON % x == 0) )

return x;

}//close the if statement

return 0; //only gets to this point if no number was found

}//close findcommon method

/**********************************************************************************************************************/

//This method works out the ratio for Non-Printing characters and Printing Characters

// returns an int, takes in two ints

private static int findcommon2(int TOT, int WHI) {//Open findcommon2 method

// start from number two and work way down to 0

for (int x = WHI; x > 0; x--){//open if statement

//if both numbers devide by this number with no remainder then return it

if ( (TOT % x == 0) & (WHI % x == 0) )

return x;

}//close if statement

return 0; //only gets to this point if no number was found

}//close findcommon2 method

/**********************************************************************************************************************/

private void vowelcount (char c) { //open vowelcount method

switch(c){ //open switch

//If character matches, then increment the relavent letter count and break away from the switch

case 'a': number[VOWELA]++; break;

case 'e': number[VOWELE]++; break;

case 'i': number[VOWELI]++; break;

case 'o': number[VOWELO]++; break;

case 'u': number[VOWELU]++; break;

}//close switch

}//close vowelcount Method

/********************************************************************************************************/

private static boolean Vowel ( char c ) {//If true return a Vowel

return c=='a' || c=='e' || c=='i' || c=='o' || c=='u';

}//close Vowel Method

}//close program

shenmue232a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 8
Change your ints to doubles; in Java using ints 1/3 = 0, and using doubles, 1/3 = 0.333....
ignignokt84a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 9
any idea what ints i would need to change
shenmue232a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 10
I just told you.Think.
ignignokt84a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 11
> any idea what ints i would need to changeLots more than can be covered here without too much involvement and hand-holding. It basically needs to be rewritten from scratch - it's a total mess.
warnerjaa at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 12
ok thanks for the help
shenmue232a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 13

ok i have cleaned the program up abit but still have the same problem heres the code

/*********************************************

Author: 0602471

Text Analyser Program

/*********************************************/

import java.util.Scanner;

import java.io.*;

public class TextAnalyse {

private int[] count = { 0, 0, 0, 0, 0, 0 , 0 , 0 , 0 , 0 , 0 };

private final int LETTER = 0,

DIGIT = 1,

WHITESPACE = 2,

OTHER = 3,

VOWEL = 4,

CONSONANT = 5,

VOWELA = 6,

VOWELE = 7,

VOWELI = 8,

VOWELO = 9,

VOWELU = 10;

public TextAnalyse ( String fileName ) {

String inStr;

try {

File file = new File(fileName);

Scanner fileScan = new Scanner(file);

while (fileScan.hasNextLine()) {

inStr = fileScan.nextLine();

inStr = inStr.toLowerCase();

for (int i = 0; i < inStr.length(); i++) {

char ch = inStr.charAt(i);

count[charCategory(ch)]++;

}

}

fileScan.close();

} catch (FileNotFoundException e) {

System.out.println("Text File was not found");

e.printStackTrace();

}

}

private int total(){

return (count[DIGIT]+count[VOWEL]+count[CONSONANT]

+count[WHITESPACE]+count[OTHER]);

}

private double percentage(){//THIS PART OF THE CODE IS GIVING ME THE PROBLEM

return (count[VOWELA] / count[VOWEL] * 100);//WORKS IF YOU CHANGE/TO%or +

}

public void display () {

System.out.println("Letters: "+count[LETTER]);

System.out.println("");

System.out.println("Digits: "+count[DIGIT]);

System.out.println("");

System.out.println("Vowels: "+count[VOWEL]);

System.out.println("");

System.out.println("Number of A: "+count[VOWELA]);

System.out.println("");

System.out.println("Number of E: "+count[VOWELE]);

System.out.println("");

System.out.println("Number of I: "+count[VOWELI]);

System.out.println("");

System.out.println("Number of O: "+count[VOWELO]);

System.out.println("");

System.out.println("Number of U: "+count[VOWELU]);

System.out.println("");

System.out.println("Consonants: "+count[CONSONANT]);

System.out.println("");

System.out.println("Whitespace: "+count[WHITESPACE]);

System.out.println("");

System.out.println("Other printing character: "+count[OTHER]);

System.out.println("");

System.out.println("Total Number of characters: "+total());

System.out.println("");

System.out.println("The ratio of vowels to consonants: " +count[VOWEL] +":" + count[CONSONANT]);

System.out.println("");

System.out.println("The ratio of Non-Printing to Printing characters " +total() + ":" + count[WHITESPACE]);

System.out.println("");

System.out.println("Percentage of vowel A: " + percentage() + " % ");

}

private int charCategory ( char ch ) {

if (Character.isDigit(ch))

return DIGIT;

else if (Character.isLetter(ch)){

if (Vowel(ch)){

count[VOWEL]++;

vowelcount(ch);

}

else

count[CONSONANT]++;

return LETTER;

}

else if (Character.isWhitespace(ch))

return WHITESPACE;

else return OTHER;

}

private void vowelcount (char c) {

switch(c){

case 'a': count[VOWELA]++; break;

case 'e': count[VOWELE]++; break;

case 'i': count[VOWELI]++; break;

case 'o': count[VOWELO]++; break;

case 'u': count[VOWELU]++; break;

}

}

private static boolean Vowel ( char c ) {

return c=='a' || c=='e' || c=='i' || c=='o' || c=='u';

}

}

shenmue232a at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 14

> private double percentage(){//THIS PART OF THE CODE IS GIVING ME THE PROBLEM

> return (count[VOWELA] / count[VOWEL] * 100);//WORKS IF YOU CHANGE / TO % or +

> }

What problem - still the division by zero problem, or the incorrect math problem, where integer division (such as 1 / 3 == 0) doesn't produce what you think it should? Don't make me guess what the current problem is.

warnerjaa at 2007-7-10 11:57:02 > top of Java-index,Java Essentials,Java Programming...
# 15
You are dividing a number by the value 0. That's forbidden.
jsalonena at 2007-7-21 19:11:03 > top of Java-index,Java Essentials,Java Programming...
# 16
well i made it output a double it gives me 0.0
shenmue232a at 2007-7-21 19:11:03 > top of Java-index,Java Essentials,Java Programming...
# 17

> well i made it output a double it gives me 0.0

Right, because:

count[VOWELA] is an integer.

count[VOWEL] is also an integer.

Let's assume count[VOWELA] == 1, and count[VOWEL] == 3.

You're still doing integer division, and 1 / 3 == 0 in integer division.

Try something like this:

return ((double) count[VOWELA]) / count[VOWEL]) * 100;

All you have to do is coerce either the divisor or dividend to a double type.

warnerjaa at 2007-7-21 19:11:03 > top of Java-index,Java Essentials,Java Programming...
# 18
Thanks that sorted the problem im not to good at java still learning :)Thanks
shenmue232a at 2007-7-21 19:11:03 > top of Java-index,Java Essentials,Java Programming...