Results 1 to 11 of 11
  1. #1
    Rivy2112 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Unhappy Selecting every other array

    I have a text file from notebook that has 160800 values. I have successfully imported them into Java. What I need to do is cut the 160800 in half and I thought Java could do it easier and faster than me manually deleting half.

    Here is my code so far:

    package lab2;

    import java.io.*;
    import java.util.Scanner;

    public class Temp
    {
    public static void main(String[] args)throws FileNotFoundException
    {
    int i=0;

    Scanner inFile=new Scanner(new FileReader("file location.txt"));
    int t=inFile.nextInt();
    double [] temp=new double[t];
    while(inFile.hasNext())
    {
    temp[i]=inFile.nextDouble();
    System.out.printf("%d, %.3f %n",i, temp[i]);
    }
    }
    }


    Here is what the output looks like for the last two:

    160798, 151.122
    160799, 151.645

    And it only displays numbers 156355 to 160799. I am assuming that it will only display 'x' amount of values?

    Can someone PLEASE help me figure out the code for only outputting half the values instead of all 160800?!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Your while loop will read until the end of the file. If you want to stop reading the file before the end of the file, there are a couple of choices:
    Use a for loop that ends after the number of lines you want to read.
    Add a counter inside the while loop and break out of the loop after reading the number you want to read.

  3. #3
    Rivy2112 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    That would be the way to go if I only wanted the first half or the second half. I need every other array. The arrays are from measurements I took in an experiment and they are temperatures that are increasing from ~0 to ~150 C. If I were to take just the first or second half of the data set (arrays) I would only have half of the information that I need. Being that there are 160800 sets/arrays, taking out every other array will not change my measurements that I need to take while cutting my total arrays down to 80400.

    I need this number to be less than 130000 so I can import it into Origin to do analysis.

    Sorry about the confusion on the original post, I was staring at my code for over 4+ hours and I was mentally drained!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I need every other
    pseudo code:
    loop until end of file
    read a line
    if odd number line
    save the line
    if even number line
    ignore the line
    end loop

    Test if odd or even by using modulus 2 (% 2) == 1
    Modulus would also work if you wanted every nth line (%n) == 1
    To see how it works, write a small test program with a for loop and print out every nth line using modulus to select the line

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    A couple of years ago I wrote a little article for another forum and I think the subject of the article can apply here. Here it is.

    kind regards,

    Jos

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    A couple of years ago I wrote a little article for another forum and I think the subject of the article can apply here. Here it is.
    Fixed the link (http:// was repeated)

    db

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Fixed the link (http:// was repeated)
    Stupid fingers of mine ... thanks for the fix (I won't edit my first reply ;-)

    kind regards,

    Jos

  8. #8
    Rivy2112 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Norm I appreciate your help but I am very green when it comes to Java so I don't know what you mean by pseudo code. And when you use modulus and other things like that I don't know the appropriate import names or how it should look in my code.

    Sorry for the confusion and frustration!

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    pseudo means not real or actual code
    The modulus operator (%) returns the remainder after division. 10 % 3 = 1
    You wanted to select every nth record. Use the modulus operator on the record number to see if its the nth record:
    if((recNbr % 3) == 1) would be true for every 3rd record. The values returned by % would be: 0,1,2,0,1,2,0,1,2,....
    To see how it works, write a very small program with a loop and print out the loop index every nth iteration of the loop using the modulus operator to select which lines to print.

  10. #10
    Rivy2112 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    I got it to output every other array. Now I am curious how to have it output it into an excel spreadsheet (.xlsx)?

    Here is my revised code, all I had to do was copy temp[t]=inFile.nextDouble(); after the print and it would only print out every other one.


    package lab2;

    import java.io.*;
    import java.util.Scanner;

    public class Temp
    {
    public static void main(String[] args)throws FileNotFoundException
    {
    int t=0;

    //To Read in:
    Scanner inFile=new Scanner(new FileReader("C:\\Users\\Rivy\\Desktop\\School\\'10 - '11 Fall\\A-Lab\\L2\\T.txt"));
    int tr=inFile.nextInt(); //npts= The first value of the list from values.txt
    double [] temp=new double[tr]; //Declares array of size npts

    while(inFile.hasNext()) //This loop reads in all values of values.txt document
    {
    temp[t]=inFile.nextDouble();
    System.out.printf("%d, %.3f %n",t, temp[t]); //Prints out each value of the array
    temp[t]=inFile.nextDouble();
    t++;
    }
    }
    }


    It is very frustrating how I spent how many hours trying to figure this out and all I had to do was copy one code over!!

    Please help on the exporting of the output. Sure hope it is less confusing than previous issues! ;)

  11. #11
    Rivy2112 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Nevermind I figured out how to export it to a text file. Thank you everyone who helped me out!! I really appreciate it!!:D

Similar Threads

  1. Update GUI when selecting language
    By ludberg in forum SWT / JFace
    Replies: 0
    Last Post: 06-04-2010, 05:39 PM
  2. Selecting the greatest "double" in a string array
    By gangsterooseven in forum New To Java
    Replies: 6
    Last Post: 11-07-2009, 11:37 PM
  3. JFileChooser example (selecting a directory)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-03-2009, 01:25 PM
  4. Selecting parts of an image
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 12-15-2007, 10:06 PM
  5. selecting a record in database
    By ramachandran in forum New To Java
    Replies: 0
    Last Post: 10-25-2007, 07:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •