This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Pipeline as Code and have Mohamed Labouardy on-line!
See this thread for details.
Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Error in printing lines from textfile

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question here.I need to open a text file and the print the output according the the pattern i want.I used regex pattern method to get the desired pattern.I wan remove the curly braces,commas and brackets.I managed to do so but i dont know why my code doesn't print out the last line..

The data in text file would be like this



My Code As Follows
I should get output like this if use above code

8

11

19

21

34 20 33 26 17 35 36 49 4 48 39 1 10 41 14 7 16 46


But my question here is why I cannot get the output like above?The final line is missing.I could only get output like this.

8

11

19

21


is there any wrong with the method i used? Hope someone could help me..Tq
 
Marshal
Posts: 72443
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You should be doing several things which ought to be in several methods rather than one method:-
  • 1: Opening the reader (and closing it which I presume is in code you missed out).
  • 2: Reading the lines in the file. You appear only to read one line in the code you provided which presumably explains why you get the short output.
  • 3: Manipulating the input.
  • 4: Printing the results.
  •  
    Arsh Boy
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I found the answer ready..i used arraylist and then store the string in the arraylist.It works!

    package exercises;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import javax.swing.JFileChooser;

    /**
    *

    */
    public class ClusterLine {

    private static File f;

    public static void main(String[] args) throws FileNotFoundException, IOException {


    String sentence = "";
    try {


    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    f = chooser.getSelectedFile();
    String filename = f.getAbsolutePath();
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);

    List<String> lines = Files.readAllLines(Paths.get(f.getAbsolutePath()), Charset.defaultCharset());

    sentence= lines.toString();

    Pattern p = Pattern.compile("\\{(.*?)\\}");

    Matcher m = p.matcher(sentence);

    while (m.find()) {
    System.out.println(m.group(0).replace(",", "").replaceAll("\\{", "").replaceAll("\\}", ""));

    }
    } catch (FileNotFoundException ex) {

    }


    }
    }

    Thanks a lot for your suggestions..
     
    It will give me the powers of the gods. Not bad for a tiny ad:
    SKIP - a book about connecting industrious people with elderly land owners
    https://coderanch.com/t/skip-book
    reply
      Bookmark Topic Watch Topic
    • New Topic