Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Question about reading numbers from external file

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Question about reading numbers from external file

    Hey,
    I am making a program that reads data from a radio telescope and then sums the data then plots it.
    I am having some problems about reading the data and have some questions.

    I at first had it so it was shown as a string. data = x.next();. The output showed everything in the text file. I then changed it into double and seems like its not reading all the numbers. Here's the code i used to read. Is the problem the text file containing the data? It's only numbers. well I attached the txt file data.txt

    My other question is how would I go about summing the data? I want to sum each row of data, but the output shows data on each line. Is there a way so the program reads one line and sums the data then reads next line sums that data ect? Or thinking might be easier to have it sum every 20 piece of data, since each row has the same amount of data.
    Thinking maybe storing that sum data in hash table or make an array of variables.




    while(x.hasNextDouble()){
     
    		//	if( i <= 17){
     
    		//	}
    		//	if(i >=67){
     
    		//	}
    			 data = x.nextDouble();
    			// String fa = x.next();
     
    			 System.out.println(data);
    		}
    		System.out.printf("done");
    	}



    Also I was trying to store the data back into a different text file, but it seems to only store the last number.

    try {
     
    			PrintWriter outputStream = new PrintWriter(file);
     
    			outputStream.print(data);
     
    		outputStream.println("");
    	//outputStream.println("\n" + data);
    	outputStream.flush();
     
    	} catch (FileNotFoundException e) {
     
    		e.printStackTrace();
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about reading numbers from external file

    seems like its not reading all the numbers
    What is printed out by the code when it reads the file and prints what it has read into the data variable?
    What data is missing?

    but it seems to only store the last number.
    A new file is created every time a new instance of PrintWriter is created.
    Create an instance before any writing is done and use that same instance for all the writing and close it when the code is finished writing to it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Question about reading numbers from external file

    The first number that is printed out is 1620.5, but it should be 1698.8 when it was string it did. Also when I had it as string it took way longer for the program to finish so I'm guess a good chunk of data is not shown....I just checked and only shows like the last 1/4 of the data
    It prints out like this
    1620.5
    1584.9
    1633.0
    1619.0
    1649.5
    .....

    Just did it in debug mode and seems like it does read it since it shows the first number ect, but wonder why it doesn't when I run the program normally. Is it to much data to show it all?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about reading numbers from external file

    The first number that is printed out is 1620.5, but it should be 1698.8
    Are you saying the first number in the file is 1698.8?
    Where is 1620.5 located in the file?
    Is there a pattern between what is printed and what is in the file? For example it only prints every third number.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Question about reading numbers from external file

    yes in the text file the first number is 1698.8. 1620.5 is located near the end and theirs no pattern that I see cause the number afters 1620.5 is what is in the data file. just seems like it skips 3/4 of the data and starts in the middle, and displays all the numbers afters 1620.5

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about reading numbers from external file

    displays all the numbers afters 1620.5
    Are there other parts of the code that read the file up to 1620.5?

    Can you make a small complete program that compiles, executes and shows the problem. There must be something else that isn't shown in the few lines of code that are posted.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Question about reading numbers from external file

    now, the output shows a different number, but still not the first one. Also the average is not what it should be for the last set of data. I am pretty sure I did the average correct. The average is very close to what I did in excel got 1628.271 in excel and in java 1616.112
    I am going to make the output set to a txt file and see if it writes all the data or shows same thing in the console.



    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Scanner;
     
     
    public class data {
     static Scanner x;
     double[] data = new double[49];
     double sum;
    	String file = "Data.txt";
    	int i =0;
     
    	public void open(){
    		try{
    			x = new Scanner(new File("C:/Users/chris/Desktop/data.txt"));
    		}
    		catch(Exception e){
    			System.out.print("no file");
    		}
    	}
     
     
    	public void read() {
     
    		while(x.hasNextDouble()){
     
    		//	if( i <= 17){
     
    		//	}
    		//	if(i >=67){
     
    		//	}
    		      for(int i=0; i<=48; i++){
    			 data[i] = x.nextDouble();
    			 System.out.println(data[i]);
    			 if(i == 48){
     
    				 for (int ii = 0; ii<=48; ii++){
    					sum = data[i] + sum;
    				 }
    				 sum = sum / 49;
    			 }
     
     
    		      }
    		}
    		System.out.printf("done" + sum);
    	}
     
    	public void wright(){
    		try {
     
    			PrintWriter outputStream = new PrintWriter(file);
     
    			outputStream.print(data);
     
    		outputStream.println("");
    	//outputStream.println("\n" + data);
    	outputStream.flush();
     
    	} catch (FileNotFoundException e) {
     
    		e.printStackTrace();
    	}	
     
     
     
    	}
     
    }


    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Scanner;
     
     
    public class astro {
     
    	public static void main(String[] args)  {
    		data d = new data();
    		d.open();
    		d.read();
    		d.wright();
    	}
     
     
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about reading numbers from external file

    What is in the input file that is read?

    Can you post the full contents of the programs output?


    A suggestion to make the code easier for testing.
    Instead of hard coding magic numbers like 48 and 49 in the code,
    define a variable that has the value of the number of lines to be read from the file and use that in the code in place of the numbers 48 and 49.
    For example:
      final int NbrLines = 5; // number of lines to read
     double[] data = new double[NbrLines];
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Question about reading numbers from external file

    ok thanks for the info, I posted the input file in my first post. It's the data.txt

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about reading numbers from external file

    I made my own data.txt file and then tried to change the program to have it work with the value of NbrLines that I posted above to make the testing easier. 49 lines is too many. 5 is enough for testing most parts of the code and is much easier to do arithmetic with.
    One thing that is weird is having the loop that does the summing be inside of the loop that is doing the reading of the data. Is there a requirement for it to be there? Normally summing the input can be done as the data is read or in a separate loop after the data has all been read.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Question about reading numbers from external file

    hey thank for the help, no the loop is not required there. I just did it that way.
    edit, O i know why I did it that way. I notice that after the for loop goes past to 48 it resets and starts from 0 and I need the average not sum I mean for every 48 bits of data.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about reading numbers from external file

    Another problem is the there is not a call to the hasNextDouble() method for each call to the nextDouble() method. They should be coded so the has... method is called before each call to next....
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Question about reading numbers from external file

    o ok thanks ill fix it
    ok found out why the average was wrong, used data[i] instead of data[ii]

    UPDATE: Still not getting correct average, but should be. I made a smaller files like you said with just four numbers and it calculated the correct average, but when I changed it back it's still off by a little. I checked few times and there are a total 49 set of numbers. The output shows the last 49 set and I checked them and all look correct.
    output
    the average is 1662.4298960349997 data used for average: 1653.0 1687.7 1594.7 1666.0 1613.4 1614.5 .......
    could be excel doing something wrong? Excel gave me 1628.271

  14. #14
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Question about reading numbers from external file

    Quote Originally Posted by leonne View Post
    ...
    could be excel doing something wrong? Excel gave me 1628.271
    The Excel sheet could be doing something wrong if it were not set up correctly, but I think it's probably OK.

    I mean, I get something like 1628.271429 for the average of the last 49 data elements in your file. Did you try it (with, say, a calculator)? I mean it takes a couple minutes for you to check the answer, which is probably less time than it takes to post and wait for a helpful response, right?

    Anyhow., given what you started with, here's my take on a method that can calculate averages of each 49 points in the file:

    Pseudo-code for data.read():
     
    Open input file.  If it caught an error, print a message and abort the program.
     
    Repeat the following loop while input file hasNextDouble():
    BEGIN LOOP
     
       Declare a double precision variable, sum.  Initialize it to zero
     
       Repeat the following loop, iterating i from zero through 48:
       BEGIN LOOP
            IF NOT input file hasNextDouble() THEN
                Print error message, close the input file, and exit the program.
            ELSE
                Get nextDouble from the input file.
                Add it to sum.
                Do whatever else you need to do with it (store in the data array, for example).
            END IF
        END LOOP // Inner loop added the 49 most recent data values
     
        Print the data array if you want to. This will contain the 49 most recent values.
        Print sum/49.  This is the average of the 49 most recent values.
     
    END LOOP // Outer loop read values from the file
     
    Close the input file.

    Cheers!

    Z

  15. The Following User Says Thank You to Zaphod_b For This Useful Post:

    leonne (January 15th, 2013)

  16. #15
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Question about reading numbers from external file

    thanks for the info, yeah was thinking using a calculator but didnt feel like typing 49 numbers lol but ill try it.
    Edit, I just created a smaller file with the last line and I get the same avg as excel. So its probably the loop. I am guess the error has something to do with NORMs last post.

Similar Threads

  1. Question about External Jars
    By Azurit3 in forum Java Theory & Questions
    Replies: 6
    Last Post: August 27th, 2012, 01:41 PM
  2. Build a database using Arrays and external file
    By simc47 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 19th, 2012, 02:40 PM
  3. Reading in numbers
    By Harlow777 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2012, 09:54 PM
  4. I/O stream for reading and editing a file full of numbers
    By Harry Blargle in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 5th, 2011, 09:00 AM
  5. reading from text file produces extra s p a c e s, even on numbers 1 0 1
    By Spidey1980 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 3rd, 2011, 09:18 PM