# Another Array Problem

• 09-09-2004, 08:59 PM
RPBLEA
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.

import java.io.*;

public class temp {

public static PrintWriter outFile;

public static void main(String[] args) throws IOException {
int tempcount;
int k;

outFile = new PrintWriter(new PrintWriter("Out.txt"));

int [] count = new int [200];

for (k = 0; k < 200; k ++) {

}

}
}
• 09-10-2004, 04:48 AM
Rezrovs
Try reading each number into an array. Then loop through the array and count the number of times that the specified number appears.
• 09-13-2004, 04:44 PM
RPBLEA
Help
How do I get the results from the File and Put it into the array, And then how do I count how many times it went through the loop?
• 09-14-2004, 02:41 AM
Rezrovs
First comment, you shouldn't code with throwing Exceptions like that. Rather put the proper code in try/catch blocks.

OK, so you said that you need to read numbers from a txt file.
So you were correct to have the BufferedReader. But rather than reading to the integer straight away, do this...

Code:

``` String tempString = inFile.readLine(); int intCounter = 0; while(tempString!=null){         tempCount = Integer.parseInt(tempString);         intCounter++;                 //process the integer here*         tempString = inFile.readLine();        }```
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.

Cheers,
Rachel
• 09-14-2004, 09:06 AM
cjard
rachel,

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

so you can loop through the whole array printing out:

System.out.println(i+" degrees occurred" + temps[i] + " times");

-

does this help?
• 09-14-2004, 09:08 PM
RPBLEA
is this something that would work
import java.io.*;

public class temp {
public static PrintWriter outFile;
public static void main(String[] args) throws IOException {
outFile = new PrintWriter(new PrintWriter("Out.txt"));

int intCounter = 0;

while(tempString != null) {
intCounter = Integer.parseInt(tempString);
int [] temps = new int[200];
temps[intCounter] ++;
outFile.println(temps + "degrees occurred" + temps[intCounter] + "times");
}

inFile.close();
outFile.close();
}
}

I don't know if this is correct because my java compiler isn't working at the time, but can you guys correct this if it can be corrected?
• 09-15-2004, 03:05 AM
Rezrovs
Well then it would rather be better to fix your compiler and test it and then let us know how to help.

The first thing - you still aren't throwing the exceptions correctly.

Second, your println isn't going to work like you want it.

outFile.println(temps + "degrees occurred" + temps[intCounter] + "times");

If you do this in the loop, you are going to print out your whole array every time you read a number. Rather wait for the loop to finish and then run another loop to read them into the outFile.

Plus in the loop you are declaring a new array every time. You need to declare this array outside the loop.
• 09-15-2004, 11:10 AM
RPBLEA
Almost Got it
import java.io.*;

public class temp {
public static PrintWriter outFile;
public static void main(String[] args) throws IOException {
outFile = new PrintWriter(new PrintWriter("Out.txt"));

char starChar = '*';
int intCounter = 0;
intCounter = Integer.parseInt(tempString);

while(tempString != null) {
int [] temps = new int[200];

temps[intCounter] ++;
outFile.println(temps + " degrees occurred " + temps[intCounter] + " times " + temps[starChar]);
}

inFile.close();
outFile.close();
}
}

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?
• 09-15-2004, 11:42 AM
Rezrovs
Code:

```public class temp {         public static BufferedReader inFile;         public static PrintWriter outFile;         public static void main(String[] args) throws IOException {                 inFile = new BufferedReader(new FileReader("File.txt"));                 outFile = new PrintWriter(new PrintWriter("Out.txt"));                 String tempString = inFile.readLine();                 int [] temps = new int[200]; //you need to declare it out of the loop                 int theNumber;                                         for(int i=0; i<temps.length; i++){                         temps[i] = 0; //you need to initialise every holder in the array so that you can use increment later on                 }                 while(tempString != null) {                                                 theNumber = Integer.parseInt(tempString);                                                temps[theNumber] ++;                         tempString = inFile.readLine();                 }//this is reading the whole file into the array - note that this works if the ints are stored one on every line                 for(int i=0 ;i<temps.length; i++){                         outFile.println("The degree "+i"+ occurred "+temps[i]+" times";                 }//this prints the array and it's contents to the file                 inFile.close();                 outFile.close();         } }```
• 09-16-2004, 08:12 AM
cjard
rezrovs; you dont need to initialize an array in java; it will automatically filled with zeroes

ps; it's up to you what you post, but consider if youre doing someone's homework for them, and what they will actually learn if you give them a full solution..
• 09-16-2004, 08:35 AM
Rezrovs
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.