Results 1 to 5 of 5

Thread: Files

  1. #1
    Join Date
    Jun 2011
    Posts
    24
    Rep Power
    0

    Default Files

    Hello,

    I have a question regarding reading before line. For eg; I have a file with 20 lines. My 'if' condition satisfies line 6. Now I need to print line 5 and line 6.

    Can someone help me on this??

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Uh what? Can you post us the code you have in [code][/code] tags and further explain what you're trying to do?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Join Date
    Jun 2011
    Posts
    24
    Rep Power
    0

    Default

    here is my code. In this I am generating 10 random numbers and multiplying with total number of lines in the input file. The number which was obtained after multiplication, is used to retrieve that line from input file. If the number which was obtained after multiplication is even then I need to print even numberline and before line of even number. If the number which was obtained after multiplication is odd then I need to print odd numberline and next line of odd number. My code is working fine while printing odd numberline and next line of odd number. And the problem is with even numberline and before line of even number.


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Random;


    /**
    *
    * @author hi
    */


    public class test
    {
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
    BufferedReader br1= new BufferedReader(new FileReader("C://Users//hi//Desktop//random_generator_circos_input//inter.txt"));
    BufferedWriter bw1 =new BufferedWriter(new FileWriter("C://Users//hi//Desktop//random_generator_circos_input//interoutput.txt"));
    String s1=null;
    long line_count=0;
    while((s1=br1.readLine())!=null)
    {
    line_count++;
    }
    System.out.println("linecount is :"+line_count);
    double[] arr = new double[10];
    Random generator = new Random();
    int[] required_line_numbers = new int[10];
    for(int i=0;i<10;i++)
    {
    arr[i] = generator.nextDouble();
    required_line_numbers[i] = (int) (arr[i] * line_count);
    System.out.println("a:" + "\t" + +i + "\t" +arr[i] + "\t" +required_line_numbers[i]);
    br1= new BufferedReader(new FileReader("C://Users//hi//Desktop//random_generator_circos_input//inter.txt"));
    String s2=null;
    long input_file_line_number = 0;
    while((s2=br1.readLine())!=null)
    {
    if ( required_line_numbers[i] == (input_file_line_number))
    {
    if ( (input_file_line_number)%2 ==0)
    {
    String s3 = br1.readLine();
    System.out.println("s3 is :" +s3 + "\t" +"even" + "\t" + required_line_numbers[i]);
    System.out.println("s2 is :" +s2 + "\t" +"even" + "\t" + required_line_numbers[i] );
    System.out.println();
    }
    else
    {
    System.out.println("s2 is :" +s2 + "\t" +"odd" + "\t" + required_line_numbers[i]);
    String s3 = br1.readLine();
    System.out.println("s3 is :" +s3 + "\t" +"odd" + "\t" + required_line_numbers[i]);
    System.out.println();
    }
    }
    input_file_line_number++;
    }
    }
    br1.close();
    bw1.close();
    }
    }
    Last edited by lakshmibvaraprasad; 07-19-2011 at 04:42 PM.

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm, well try putting your code in tags for us to preserve formatting. Its pretty hard reading code without formatting.

    However, if you say that the odd one is working but the even one is not. Try looking at that section. Is there anything different in this section of code? This is the entire loop of where you are getting your line and displaying it. I'm not really sure why you are declaring a the same variable 10 times and why you put the while loop in the for loop.

    Java Code:
    for(int i=0;i<10;i++)
    {
    arr[i] = generator.nextDouble();
    required_line_numbers[i] = (int) (arr[i] * line_count);
    System.out.println("a:" + "\t" + +i + "\t" +arr[i] + "\t" +required_line_numbers[i]);
    br1= new BufferedReader(new FileReader("C://Users//hi//Desktop//random_generator_circos_input//inter.txt"));
    String s2=null;
    long input_file_line_number = 0;
    while((s2=br1.readLine())!=null)
    {
    if ( required_line_numbers[i] == (input_file_line_number))
    {
    if ( (input_file_line_number)%2 ==0)
    {
    String s3 = br1.readLine();
    System.out.println("s3 is :" +s3 + "\t" +"even" + "\t" + required_line_numbers[i]);
    System.out.println("s2 is :" +s2 + "\t" +"even" + "\t" + required_line_numbers[i] );
    System.out.println();
    }
    else
    {
    System.out.println("s2 is :" +s2 + "\t" +"odd" + "\t" + required_line_numbers[i]);
    String s3 = br1.readLine();
    System.out.println("s3 is :" +s3 + "\t" +"odd" + "\t" + required_line_numbers[i]);
    System.out.println();
    }
    }
    input_file_line_number++;
    }
    }
    Try putting in System.out.println's in your code to show you where the program is and what is the output of your variables. This will help you figure out if your variables are what you expect them to be at that time in your code. Does your code ever print even numbers? If it does, does it print two lines? If it prints two lines, are they identical lines? What are the variables entering your print statements at the time of printing?
    Last edited by Dark; 07-19-2011 at 05:05 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

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

    Default

    Check that you are only reading lines from the file at one spot in the code. If you read lines at more than one spot it will be harder keeping track of the lines

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  2. Replies: 1
    Last Post: 05-18-2011, 12:34 PM
  3. Creating Jar Files with functioning input files
    By appleLove in forum NetBeans
    Replies: 1
    Last Post: 04-10-2011, 10:37 PM
  4. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  5. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM

Posting Permissions

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