Array Sort and occurances

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • unknown32
    New Member
    • Mar 2007
    • 2

    #1

    Array Sort and occurances

    I am currently working on a program that will read ints from a file (no more than 100 on file)sorts them in decending order and counts how many times each number occurs. I'm still a little new to this so be gentle :).
    My input file looks as follows:
    75 85 90 100
    60 90 100 85
    75 35 60 90
    100 90 90 90
    60 50 70 85
    75 90 90 70

    What the program needs to output is:
    100:3
    90:8
    85:3
    75:3
    70:2
    60:3
    50:1
    35:1

    Here is my code so far it isnt much but I have been testing to see if my array works. Just showing my main because my other methods arent working such as my sort. Any help would be greatly appreciated, thank you.

    public static void main(String[] args)
    {
    InputFile ifile;
    ifile =new InputFile("C:\\ Documents and Settings\\Owner \\Frequency\\sr c\\test.txt");

    Keyboard kbd;
    kbd = new Keyboard();


    int [] a;
    a=new int[100];

    int scores=0;
    int x=-1;
    int y=0;
    int z=0;
    while(!ifile.eo f())
    {
    scores = ifile.readInt() ;


    a[x+1]=scores;
    x++;
    y++;
    }

    while (z!=y)
    {
    System.out.prin tln(a[z]);
    z++;

    }

    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by unknown32
    I am currently working on a program that will read ints from a file (no more than 100 on file)sorts them in decending order and counts how many times each number occurs. I'm still a little new to this so be gentle :).
    My input file looks as follows:
    75 85 90 100
    60 90 100 85
    75 35 60 90
    100 90 90 90
    60 50 70 85
    75 90 90 70

    What the program needs to output is:
    100:3
    90:8
    85:3
    75:3
    70:2
    60:3
    50:1
    35:1

    Here is my code so far it isnt much but I have been testing to see if my array works. Just showing my main because my other methods arent working such as my sort. Any help would be greatly appreciated, thank you.

    public static void main(String[] args)
    {
    InputFile ifile;
    ifile =new InputFile("C:\\ Documents and Settings\\Owner \\Frequency\\sr c\\test.txt");

    Keyboard kbd;
    kbd = new Keyboard();


    int [] a;
    a=new int[100];

    int scores=0;
    int x=-1;
    int y=0;
    int z=0;
    while(!ifile.eo f())
    {
    scores = ifile.readInt() ;


    a[x+1]=scores;
    x++;
    y++;
    }

    while (z!=y)
    {
    System.out.prin tln(a[z]);
    z++;

    }

    }
    For opening the file use

    Code:
     
    FileReader fr = new FileReader("C:\\Documents and Settings\\Owner\\Frequency\\src\\test.txt");
      
    BufferedReader input = new BufferedReader(fr);
    For reading the file use
    Code:
     int[] array = new int[100]; 
    int index = 0;
    String line = "";
    while((line =input.readLine()) != null) {
      String[] values = line.split(" ");
      for(String s : values) {
    	  array[index++] = Integer.parseInt(s);
      }
    }
    Now you can sort your array and do all sorts of tricks on it.

    Comment

    • unknown32
      New Member
      • Mar 2007
      • 2

      #3
      Ok I havnt started using that code yet. But I have a quick question. Look at my code. For some reason my array is storing zeros? Why is that?

      public static void main(String[] args)
      {
      InputFile ifile;
      ifile =new InputFile("C:\\ Documents and Settings\\Owner \\Frequency\\sr c\\test.txt");

      Keyboard kbd;
      kbd = new Keyboard();


      int [] a;
      a=new int[100];

      int scores=0;
      int x=-1;
      int y=0;
      int z=0;
      while(!ifile.eo f())
      {
      scores = ifile.readInt() ;


      a[x+1]=scores;
      x++;
      y++;


      }
      Arrays.sort(a);
      for (int j=0; j<y; j++)
      System.out.prin tln(a[j]);


      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by unknown32
        Ok I havnt started using that code yet. But I have a quick question. Look at my code. For some reason my array is storing zeros? Why is that?

        public static void main(String[] args)
        {
        InputFile ifile;
        ifile =new InputFile("C:\\ Documents and Settings\\Owner \\Frequency\\sr c\\test.txt");

        Keyboard kbd;
        kbd = new Keyboard();


        int [] a;
        a=new int[100];

        int scores=0;
        int x=-1;
        int y=0;
        int z=0;
        while(!ifile.eo f())
        {
        scores = ifile.readInt() ;


        a[x+1]=scores;
        x++;
        y++;


        }
        Arrays.sort(a);
        for (int j=0; j<y; j++)
        System.out.prin tln(a[j]);


        }
        I don't know what inFile is or how it works so I can't say.

        Comment

        Working...