how can I store text information on a specific line
Hey,
I am trying to make a program that would first read a text file or a excel file and store two types of data, a line start and a line width. I than want the program to scan a new text file and using the data it stored from the other files to store the data. An example, like the file has line start of 5 and width of 4 I want the program to then go into the other text file and go to line 5 then copy everything from line 5 to line 9. Then once it copes all the data what was listed from the first file, I want it to create an output of all the data that it copied.
My question is, how can I go about making the program copy the content at line 5 to line 9, even if it is a space?
As of right now, I have some old program I used to analyze data that would scan the first 49 numbers and do some calc.
here is the class from the old code that I want to modify. My idea was to change it so that it would store the first text file or excel file into a hash table where the key would be line start and and data would the the width. Than it would scan the other file and spit out an output in a text file or something.
I have not done java programming in a year so I am a little rusty lol
Code :
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Scanner;
public class data {
static Scanner x;
double[] data = new double[50];
double sum;
public static Hashtable h = new Hashtable();
String file = "Data.txt";
int key = 1;
double xaxis;
double yaxis;
//setter
public data(double yyaxis){
this.yaxis=yyaxis;
}
//opens the data file
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() {
//reads the data in the text file and sets it to an array.
while(x.hasNext()){
for(int i=1; i<=49; i++){
if(x.hasNextDouble() == false){
System.out.println("error in data");
System.exit(0);
}
data[i] = x.nextDouble();
// Calculates the average for every 49 set of data.
//Stores that average to a hashtable
if(i == 49){
sum = 0;
for (int ii = 1; ii<=49; ii++){
sum = data[ii] + sum;
}
sum = sum / 49;
data av = new data(sum);
h.put(key, av.getHash());
key++;
}
}
}
//test to see the data its using to calculate
System.out.printf("the average is " + sum + "size "+ h.size() + " ave " + h.get(995) + " " + h.get(996)+ " data used for average: " );
}
// wrights the data to a external text file
public void wright(){
try {
PrintWriter outputStream = new PrintWriter(file);
for(int i=1; i<=49; i++){
System.out.print(" "+data[i] + " ");
outputStream.print (" " + data[i] + " ");
}
outputStream.println("");
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//getter for hashtable
public double getHash(){
return this.yaxis;
}
}
Re: how can I store text information on a specific line
Do you have some specific questions about the program you are trying to write?
Where are you having problems?
Quote:
how can I go about making the program copy the content at line 5 to line 9
Read over the lines up to line 5
read and copy the lines from line 5 up to line 9
Re: how can I store text information on a specific line
My mistake, I mean read over to column 5 then read and copy from column 5 to column 9. This old program would copy every new set of number, it would know its a new data point by the space between them. I am guessing data[i] = x.nextDouble(); stores every set of data point in a array, but instead of storing like the next data point by the blank space, it would store the data point between column 5 to column 9. Also the data points might be anything from a letter to just a blank space.
How can I do that?
Thank you
Re: how can I store text information on a specific line
Quote:
read over to column 5 then read and copy from column 5 to column 9.
Same logic:
Read and skip columns before 5, copy columns through 9
What is the boundary between columns? For example the "\n" character is the boundary between lines.
The String class's split() method may help you get all the "columns" in a String record/line.
Re: how can I store text information on a specific line
Hey,
Not really sure what you mean by boundary, but here is how the text file looks like. Has random length of white space between like this.
5412 1478ddp284 FAKE STREET MAT BRUNO HOUSE
But I think I have an idea, can I just make one of the text file into one big string then use substring() so it would create a string of
"5412 1478ddp284 FAKE STREET MAT BRUNO HOUSE" then use substring() with the data from the other text file, but not sure how to create one big string, ill try to google it.
Re: how can I store text information on a specific line
In a CSV file, a comma: ',' separates one column from the next. That is what I call a boundary. A character or String that separates one column from another.
If one or more spaces separate one column from the next, the String class's split() method could help you.
Re: how can I store text information on a specific line
ok thank you, ill try it out