I am writing this program that takes a file of integers, sorts the integers, and prints the sorted list into a different output file. I have all of this working fine, I am stumped as to how to incorporate a counter.
I want to print the number of times a number appears that is less than 10.
here is my code at this point:

import java.io.*;
import java.util.Scanner;
/**
 *
 * @author Christie
 */
public class insertionSortMain {

    public static void main(String[] args)throws IOException {
        // TODO code application logic here       

            int nums[] = new int[20];            

            getInsertionSort(nums);
            showNums(nums);          
    }



    }

public static void getInsertionSort(int[]array){
    int temp, j;
     for (int i = 1; i < array.length; i++) {
    temp = array[i];
    j = i - 1;

    while ((j >= 0) && (array[j] > temp)) {
     array[j + 1] = array[j];
     j--;
    }     
    array[j + 1] = temp;
   }
}
}


I want the number to be the first number in the list of the output file

Recommended Answers

All 3 Replies

First, I would set up a Scanner object on the file. Which is strangely absent from your code.
o.O
Second, you're using an array with a fixed size. This is a no-no, but I'm guessing it suits your needs for the time being. ArrayLists are better and SAFER. You'll learn about them next semester. =)

I would setup a while loop (or a well-made for loop) whose condition is that the
Scanner hasNextInt. Within the loop, I'd grab the integers using the Scanner's getNextInt and put them into your array.
I'd also have a variable that keeps count of how many integers I'd parsed in and place them in the appropriate array index.
When the integer gets parsed in, I'd increment a second counter for each int less than ten.
Notice how I'm not writing any code. This looks a lot like homework.
Hope you make a good grade!
=D

Crazy, I realized by your response that all of my code did not show up, I must have erased it by mistake before posting.

package insertSort;
import java.io.*;
import java.util.Scanner;


public class InsertSort {

    public static void main(String[] args)throws IOException {
         int nums[] = new int[20];            
         getNums(nums);

         getInsertionSort(nums);
         showNums(nums);

 }

 public static void getNums(int[] array)throws IOException{
     Scanner inputFile = new Scanner(new File("input1.txt"));
     int count = 0;
      int x = inputFile.nextInt();
          while(inputFile.hasNextInt())
         {    
                for(int i = 1; i < array.length; i++ ){ 
                    array[i] = inputFile.nextInt(); 
                }          
         }            
         }

 public static void getCount(int[] array){
     int count = 0;
     int x = array[0];
     for(int i = 1; i < array.length; i++ ){ 
         if(array[i] <= x){
              count ++;
     }
     }
 }

 public static void showNums(int[] array)throws IOException{
      PrintWriter out = new PrintWriter(new FileWriter("output.txt"));

     for(int i=1; i< array.length; i++)
         out.println(array[i]);       
     out.close();
 }


public static void showCount(int[] array, int count, int x){
      for(int i=1; i< array.length; i++)
           if(array[i] <= x){
                        count++;
                    }
        // System.out.print(array[i] + " ");
System.out.print(count);
}

public static void getInsertionSort(int[]array){
 int temp, j;
  for (int i = 1; i < array.length; i++) {
 temp = array[i];
 j = i - 1;

 while ((j >= 0) && (array[j] > temp)) {
  array[j + 1] = array[j];
  j--;
 }     
 array[j + 1] = temp;
}
}
}

how embarrassing

OK, a few questions. Looking at the code as you posted it, I'm seeing some strange things.
First, Line 20: int x = inputFile.nextInt();
Why? You're sacrificing your first integer to the yawning Void of nothingness.

Then, Lines 23, 32, 42, 49: you have for(int i = 1; i < array.length; i++ )
Why? Again, sucks to be the first Int. In your Insertion Sort, I see you do the same thing, but there seems to be a good reason as j starts out as 1 less than i. Did you copy/paste and forget to change it back?

Also, GetCount does nothing. If anything, it should either change a static variable (less professional), or change its return type to int and return count (better).

And ShowCount which looks like it picks up where GetCount got abandoned isn't used, and frankly doesn't need to be, if you print out what you get back from using GetCount.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.