-2

I'm supposed to read in a text file and print out one quoted string per line. The size of the quoted String has to be the same too.

This is the read file:

"check on","SKY yelow, blue ocean","","1598"
"6946","Jaming","Mountain range","GOOO, five, three!","912.3"

And this is the expected output:

check on
SKY yelow, blue ocean
1598
6946
jaming
Mountain range
GOOO, five, three!
912.3

I know how to read the file, but how would I get the output as shown above?

Thanks in advance!

HongHua
  • 1
  • 2

4 Answers4

2

Here the code for reading data from txt file.That will print as your wish and i mentioned the data which contain that txt file in below

"Viru","Sachin","Dravid","Ganguly","Rohit"

import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;



    public class logic {

        public static void main(String[] args)
        {
            BufferedReader br = null;

            try {

                String sCurrentLine;
                br = new BufferedReader(new FileReader("C:/Users/rajmohan.ravi/Desktop/test.txt"));

                while ((sCurrentLine = br.readLine()) != null) {
                    reArrange(sCurrentLine.split(","));
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        public static void reArrange(String[] dataContent)
        {
            for(String data : dataContent)
            {
                System.out.print(data);
                System.out.print("\r\n");
            }
        }


    }
takeoffjava
  • 504
  • 4
  • 15
0

Use Pattern and Matcher classes.

List<String> lst = new ArrayList();
Matcher m = Pattern.compile("\"([^\"]+)\"").matcher(string);
while(m.find())
{
lst.add(m.group(1));
}
System.out.println(lst);
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

You can do something like below

Scanner scanner = new Scanner(new File("path"));
        String input = scanner.next();
        Pattern p = Pattern.compile("\"([^\"]*)\"");
        Matcher m = p.matcher(input);
        while (m.find()) {
            System.out.println(m.group(1));
        }
Rishi Saraf
  • 1,644
  • 2
  • 14
  • 27
0

You can take help from this code :

String a = "\"abc\",\"xyzqr\",\"pqrst\",\"\"";   // Any string (of your specified type)
String an[] = a.split(",");
for (String b : an) {
      System.out.println(b.substring(1, b.length() - 1));
}

Read data line by line and use above code to print expected result.

Ashish Ani
  • 324
  • 1
  • 7
  • -- That wouldn't work because some of the quoted strings in my read file contains ",". If we split the "," it will split those words within the quoted string to separate. – HongHua Nov 24 '15 at 19:59