473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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\\src\\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.eof())
{
scores = ifile.readInt();


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

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

}

}
Mar 1 '07 #1
3 1479
r035198x
13,262 8TB
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\\src\\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.eof())
{
scores = ifile.readInt();


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

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

}

}
For opening the file use

Expand|Select|Wrap|Line Numbers
  1.  
  2. FileReader fr = new FileReader("C:\\Documents and Settings\\Owner\\Frequency\\src\\test.txt");
  3.  
  4. BufferedReader input = new BufferedReader(fr);
  5.  
For reading the file use
Expand|Select|Wrap|Line Numbers
  1.  int[] array = new int[100]; 
  2. int index = 0;
  3. String line = "";
  4. while((line =input.readLine()) != null) {
  5.   String[] values = line.split(" ");
  6.   for(String s : values) {
  7.       array[index++] = Integer.parseInt(s);
  8.   }
  9.  
Now you can sort your array and do all sorts of tricks on it.
Mar 1 '07 #2
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\\src\\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.eof())
{
scores = ifile.readInt();


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


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


}
Mar 1 '07 #3
r035198x
13,262 8TB
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\\src\\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.eof())
{
scores = ifile.readInt();


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


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


}
I don't know what inFile is or how it works so I can't say.
Mar 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
13
by: Ian Richardson | last post by:
At the moment I'm using a quicksort algorithm to sort a list of countries in alphabetic order. This worked wonderfully until someone came up with the Åland Islands... and this is at the end of the...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
3
by: Jim Adams | last post by:
I'm counting the frequency of word occurances and would like to return a key/value list sorted descending by frequency. So I need to quickly see if a word (key) is in the list, but later sort...
5
by: Paul | last post by:
Hi, I'm trying to create an array containing the entire contents of a text file and delimit the elements using vbCrLf. The text file is like this.... -text1 -text2 -text3
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
3
by: Lars Grobe | last post by:
Hi, first hello, I am new to the list, and I guess my question will show that clearly. I want to use some vector operations (at the moment altivec) in existing code. It is a raytracing-based...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.