If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Another Array Problem

Okay I'm kinda stuck on this problem, I'm trying to get a java program to read data from a file. This data are random numbers representing degrees in temperature. I can't figure out how to count each one, to say that I have this many days at 100 degrees, this many days at 50 degreees. Can anyone help, here is the little code I have.

Then when this block has executed, you will have the number of integers that you read from the file.

Next you want to put them into an array.
Because you don't know how many integers there are in the file, you can't put it directly into the array. There might be a better way to this, but I suggest putting the integers into a vector where the * is in the code(because that doesn't have a fixed size). Then when you have them in the Vector, you can declare an array with the capacity of the intCounter and then loop through the Vector and take the integers from the vector and put them into the array. Then null the Vector when you are finished with it.

it is likely the OP has been throwing exceptions because he hasnt learned how to catch them yet..

try to discourage use of vector in preference for arraylist, unless synchronized behaviour is explicitly required..

RPBLEA:

the solution is quite simple;

declare an array big enough to hold all the range of temperatures you expect.suppose your file has 0 to 220 degrees F in it, in varying numbers, you make an array at least big enough to hold all these:

int[] temps = new int[221];

remember that arrays run from 0, so an array of size 221 runs from 0 to 220 (our temp range)

then read each temp in from the file (i assume you can do this) convert it to an integer (rachel gave code) and then increment that array position by one

so assume we read in "56" , we convert to number, then increment the array in position 56, by one:

temps[56]++;

naturally, you will be reading your number into a variable, maybe called theTemp, so you will do:

temps[theTemp]++;

at the end of the file you will have an array with a load of numbers in, maybe some will be 0.. if there were ten occurrents of 89 degrees in the file, then temps[89] will = 10

alright it almost works but the array is not pulling out the numbers from the File, It's giving me gibberish in the first print out statement temps, Is there some way I can fix this and the second thing is i'm trying to add it to look like a histogram graph and print out stars for how many of the same degrees have been counted. But I think if i can get the array to print out from the file it would work anyone help?

Yes, it is a possibility that this is a homework question. But I have been watching this thread for over a week and at least they are trying with different pieces of code. Even if the code that they do post does seem to just be taking them in circles.

I remember learning and if I got too stuck on some code I would give up the hope of learning it. Sometimes we just need the solution and we can then dissect it to see why and how it works.