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 7 of 7

Thread: how can I store text information on a specific line

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

    Default 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

    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;
     
     
     
    	}
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

    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
    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: 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how can I store text information on a specific line

    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.
    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: 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    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: how can I store text information on a specific line

    ok thank you, ill try it out

Similar Threads

  1. how to add information for specific data based on it's ID
    By banana in forum JDBC & Databases
    Replies: 0
    Last Post: August 23rd, 2012, 08:54 AM
  2. search for specific line in the file
    By Rajitha in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 16th, 2012, 09:18 AM
  3. Java, Best way to store information?
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 4
    Last Post: December 22nd, 2011, 05:46 PM
  4. Error using a structure to obtain information of an specific array
    By andresfelquintero in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2011, 06:51 PM
  5. Writing to a specific line in a text file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 09:11 PM