Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Counting Loop and Outputting

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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

    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
     
     
        }
    }


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Counting Loop and Outputting

    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Counting Loop and Outputting

    Quote Originally Posted by Chris.Brown.SPE View Post
    ...Thread closed

Similar Threads

  1. Hi, I need help outputting a 2d array
    By MrMasticater in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 15th, 2013, 08:41 PM
  2. Customer counting program - while loop
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2012, 02:56 PM
  3. Counting Words in a File with a Loop
    By bengregg in forum Loops & Control Statements
    Replies: 17
    Last Post: February 11th, 2011, 10:11 AM
  4. Need help outputting the array to a .csv
    By arpanetguru in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 23rd, 2010, 05:27 PM
  5. Need help.. Counting Prime #'s up to 50 w/while loop
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 6th, 2010, 05:40 PM