Another Array Problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Another Array Problem

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Question 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 BufferedReader inFile;
    public static PrintWriter outFile;

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

    inFile = new BufferedReader(new FileReader("File.txt"));
    outFile = new PrintWriter(new PrintWriter("Out.txt"));

    tempcount = Integer.parseInt(inFile.readLine());

    int [] count = new int [200];

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


    }



    }
    }

  2. #2
    Join Date
    Sep 2004
    Posts
    26
    Try reading each number into an array. Then loop through the array and count the number of times that the specified number appears.

  3. #3
    Join Date
    Sep 2004
    Posts
    103

    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?

  4. #4
    Join Date
    Sep 2004
    Posts
    26
    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

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    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?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Sep 2004
    Posts
    103

    is this something that would work

    import java.io.*;


    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 intCounter = 0;

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


    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?

  7. #7
    Join Date
    Sep 2004
    Posts
    26
    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.

  8. #8
    Join Date
    Sep 2004
    Posts
    103

    Question Almost Got it

    import java.io.*;


    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();
    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]);
    tempString = inFile.readLine();
    }


    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?

  9. #9
    Join Date
    Sep 2004
    Posts
    26
    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();
    	}
    }

  10. #10
    Join Date
    Feb 2004
    Posts
    808
    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..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  11. #11
    Join Date
    Sep 2004
    Posts
    26
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center