Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Array prints line 2x

  1. #1
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Array prints line 2x

    My code works "almost" correctly. It accurately prints the line that appears more than once in its given array. However, I don't know how to stop it from printing the same number more than once. In the text file it reads from, the following numbers appear more than once: 147, 258, & 369 but my code make it display like this:
    147
    258
    369
    369
    258
    147

    I would certainly appreciate some help solving this problem. Thanks!

    Java Code:
        //check enrolled student ID's appearing > 1
        System.out.println("These enrolled student's Id # appear more than once:");
        
        for(int i=0; i < numEnrolled; i++)                    //cycle to next enrolled Id #
           {
            int studentId = enrollStdIds[i];                  //assign current Id # to interger
            int counter = 0;                                  //initialize counter
    
                //itterate over attending array
                for(int j=0; j < numEnrolled; j++)            //cycle to next enrolled Id #
                   {
                    if (enrollStdIds[j] == studentId)         //increase counter if matching
                       {
                        counter++;                            //accumulator
                       }
                    if (counter == 2)                         //execute if counter =2
                       {
                        System.out.println(studentId);        //display Id #
                        counter = 0;                          //reset counter
                       } 
                   }
             }

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Array prints line 2x

    so if i have understand correct you read a file with numbers on each line..
    and you want a program which will print all the numbers which appears more than once.. so you can create an ArrayList in which you will but all the numbers which appears more than once and befor you search if the next number in next line appear more than once you will first check if this number is already in ArrayList so you will void it ..

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,538
    Rep Power
    5

    Default Re: Array prints line 2x

    You could also sort the array. Then iterate thru the list, checking to see if the next number is the same as the current one, and then skipping all subsequent identical numbers.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Array prints line 2x

    Hmm. Why do you reset the counter after printing an ID that appears more than once?

  5. #5
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Array prints line 2x

    I attempted to place the duplicate numbers in their own array and print from that but, stuck again.....

    Java Code:
    //check enrolled student ID's appearing > 1
    System.out.println("These enrolled student's Id # appear more than once:");
     
    for(int i=0; i < numEnrolled; i++)                    //cycle to next enrolled Id #
       {
        int studentId = enrollStdIds[i];                  //assign current Id # to interger
        int counter = 0;                                  //initialize counter
        int[] twiceIDs;                                   //create array for duplicate ID#
     
            //itterate over enrolled array
            for(int j=0; j < numEnrolled; j++)            //cycle to next enrolled Id #
               {
                if (enrollStdIds[j] == studentId)         //increase counter if matching
                   {
                    counter++;                            //accumulator
                   }
                if (counter == 2 && studentId != 0)       //execute if counter = 2
                   {
                    twiceIDs = new int[numEnrolled];
                    Arrays.sort(twiceIDs);
                    System.out.println(twiceIDs);
                    counter = 0;                          //reset counter
                   } 
               }
         }

  6. #6
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Array prints line 2x

    what about trying something like that?

    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.Collections;
    
    class DoubleNumbers{
    	public static void main(String [] args){
    		Scanner inputReader = null;
    		try {
    			inputReader = new Scanner(new FileInputStream("myText.txt"));
    		}catch(FileNotFoundException e){
    			System.out.println("File Not Found!");
    			System.exit(0);
    		}
    		ArrayList<Integer> numbers = new ArrayList<Integer>();
    		while(inputReader.hasNext()){
    			String line = inputReader.nextLine();
    			numbers.add(Integer.parseInt(line));
    		}
    		Collections.sort(numbers);
    		int x ;
    		int y;
    		ArrayList<Integer> doubleNumbers = new ArrayList<Integer>();
    		for(int i = 0 ; i < numbers.size() -1; i ++){
    			x = numbers.get(i);
    			y = numbers.get(i+1);
    			if(x == y & !doubleNumbers.contains(x)){
    				doubleNumbers.add(x);
    			}
    		}
    		for(int i = 0 ; i< doubleNumbers.size() ; i ++){
    			System.out.println(doubleNumbers.get(i));
    		}
    	}
    }

    i make a file with name myText.txt with this inside :
    100
    500
    900
    600
    400
    100
    584
    600
    665
    584
    901
    584
    901
    584
    901
    600
    500

    and then i make an ArrayList<Integer> which i will put all the lines of the text above
    then i sort the ArrayList just to look like this :

    100
    100
    400
    500
    500
    584
    584
    584
    584
    600
    600
    600
    665
    900
    901
    901
    901

    then i have create an ArrayList doubleNumbers in which i will keep all numbers which exist more than once in
    the text file..

    last i just check if element in index i in numbers ArrayList is the same in index i + 1 and if the element in
    index i exist in doubleNumbers ArrayList.. because if already Exist i will no put it in DoubleNumbers ArrayList again..
    Last edited by ShadowWalker; 07-14-2013 at 03:24 PM.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array prints line 2x

    Arrays and all sorts of lists are just a clumsy way of thinking about this problem; use a Set instead because a Set only stores each element once.

    kind regards,

    Jos
    ShadowWalker likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Skipe line if text in array?
    By fpena06 in forum New To Java
    Replies: 7
    Last Post: 04-27-2012, 12:13 PM
  2. How to read a 2d array file line by line
    By jsoeung in forum New To Java
    Replies: 35
    Last Post: 04-18-2012, 03:23 PM
  3. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  4. Trying to write to a file but it only prints last line
    By beauti477 in forum New To Java
    Replies: 1
    Last Post: 07-22-2011, 01:19 AM
  5. command line input to array
    By minusten in forum New To Java
    Replies: 13
    Last Post: 05-09-2011, 01:56 AM

Posting Permissions

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