Results 1 to 11 of 11
Thread: Selecting every other array
- 10-08-2010, 02:10 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
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?!
- 10-08-2010, 03:17 AM #2
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.
- 10-08-2010, 01:33 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
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!
- 10-08-2010, 01:54 PM #4
pseudo code:I need every other
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
- 10-08-2010, 03:17 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-08-2010, 03:28 PM #6
- 10-08-2010, 03:31 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
- 10-10-2010, 05:26 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
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!
- 10-10-2010, 05:34 PM #9
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-11-2010, 10:05 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
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! ;)
- 10-11-2010, 10:39 PM #11
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Update GUI when selecting language
By ludberg in forum SWT / JFaceReplies: 0Last Post: 06-04-2010, 05:39 PM -
Selecting the greatest "double" in a string array
By gangsterooseven in forum New To JavaReplies: 6Last Post: 11-07-2009, 11:37 PM -
JFileChooser example (selecting a directory)
By Java Tip in forum Java TipReplies: 1Last Post: 02-03-2009, 01:25 PM -
Selecting parts of an image
By shaungoater in forum Java 2DReplies: 1Last Post: 12-15-2007, 10:06 PM -
selecting a record in database
By ramachandran in forum New To JavaReplies: 0Last Post: 10-25-2007, 07:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks