# Counting Loop and Outputting

• April 10th, 2013, 11:31 PM
Kristenw17
Counting Loop and Outputting
Alright so I made a program that will count the number of fractions in a file and then output the number of times each fraction appeared. I got
it to count the fraction and output. The problem is that I cannot figure out how to skip printing the same fractions count more than once. For example my code outputs:

5/5 has a count of 1
1/1 has a count of 2
1/10 has a count of 1
1/100 has a count of 1
1/1 has a count of 2

Code :

```import java.util.Scanner; import java.io.FileNotFoundException; import java.lang.String; import java.io.File; import java.io.FileInputStream;   public class Assignment1{   public static void main(String [] args){ //Reads file containing fractions Scanner inputFile = null; try { inputFile = new Scanner(new FileInputStream("fractions.txt")); } catch (FileNotFoundException e) { System.out.println("File not found or not opened."); System.exit(0); }   //variables String[]fractions = new String[100]; //will take in the fractions String[]split = new String[2]; //used to split the fractions int [] numerator = new int [100]; // store numerators int [] denominator = new int [100]; //store denominators int count = 0; //number of lines in file int z = 0; int same = 0; //number of fractions that are the same     //count the number of lines in the file, put each line into //the string[]fractions while(inputFile.hasNextLine()){ fractions[z]=inputFile.nextLine(); count++; //System.out.println(fractions[z]); z++; }   //split the fractions[] into two arrays: nummerator and denominator for(int i = 0; i<count; i++){ split = fractions[i].split("/"); numerator[i]=Integer.valueOf(split[0]); denominator[i]=Integer.valueOf(split[1]);   }   //used to compare specific numerator and denominator to the rest of the numbers int num; int den;   //start off by comparing denominator, and then compares the numerator //of like denominators for(int i = 0; i<=count; i++){ den = denominator[i]; num = numerator[i]; for(int a = 1; a<count; a++){ // if(den == denominator[a]){ //compare denominators if(num == numerator[a]){ // compare numerators same++; } } } //I am putting this in because I could not figure out why //the first and last fractions were printing a count of 0 if(same<=1){ System.out.println(num+ "/" + den + " has a count of 1"); }else{ System.out.println(num+ "/" + den + " has a count of "+ same); } same=0; }   //output the totals     } }```
• April 11th, 2013, 07:11 AM
Chris.Brown.SPE
Re: Counting Loop and Outputting
• April 12th, 2013, 01:31 AM
jps
Re: Counting Loop and Outputting
Quote:

Originally Posted by Chris.Brown.SPE