[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

How to parse a text file separated by spaces and commans?

Hi,
In Java, I want to parse a file. (myFile.txt)

The format of the file is like this:

020909-14:25 something 020909-14:25 999999,888888 020909-14:25

Open in new window


The general rule is:

The arguments in this line is seperated by space character. Take the 4th group in this line. If it is comma separated then assign it to a variable.

The variable in this example must include:

999999,888888

Open in new window


How can I do it in java?

Thanks,


0
Tolgar
Asked:
Tolgar
  • 19
  • 11
1 Solution
 
for_yanCommented:
String [] ss = line.split("\\s+");
should make it an array of your strings
0
 
for_yanCommented:
                 String line100 = "020909-14:25 something 020909-14:25 999999,888888 020909-14:25";
             String [] ss100 = line100.split("\\s+");
        for(String s10:ss100){

            System.out.println(s10);
        }

Open in new window


Output:
020909-14:25
something
020909-14:25
999999,888888
020909-14:25

Open in new window

0
 
TolgarAuthor Commented:
Would it be possible to give me the rest of the code.

By the way, I forgot to say:

This comma separated block does not have to be there. If it is not there, we can just skip.

Thanks for your help

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
for_yanCommented:
So you mean you don't care about the pieces which have comma inside ?
yes we can skip them when going through array
0
 
TolgarAuthor Commented:
IF:
020909-14:25 something 020909-14:25 999999,888888 020909-14:25

Open in new window


Return:
999999,888888

Open in new window


IF:
020909-14:25 something 020909-14:25 020909-14:25

Open in new window


Return:
empty string

Open in new window


I hope it is clear now.

Thanks for your help
0
 
for_yanCommented:
public class getMyString {
    public static void main(String[] args) {

        String myString = null;
        
          try{
        FileReader fr = new FileReader("myFile.txt");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
//System.out.println(s);




                     //String line100 = "020909-14:25 something 020909-14:25 999999,888888 020909-14:25";
             String [] ss100 = s.split("\\s+");
        for(String s10:ss100){
                           if(s10.indexOf(",")>-1)  {
          //  System.out.println(s10);
                               myString = s10;
                           }
            
        }
}
    fr.close();

              System.out.println("myString: " + myString);


    }catch(Exception ex){
              ex.printStackTrace();
          }
}
}

Open in new window


Output:
myString: 999999,888888

Open in new window

0
 
for_yanCommented:
If there are many lines in you file - maybe you want to collect the results into the ArrayList or something
0
 
for_yanCommented:
This will be the class wit static mnethod return ArrayList for many laines file

public class GetStrings {
  public static ArrayList <String> getMyString(String filename){
        ArrayList<String> ar = new ArrayList<String>();
        try{
         FileReader fr = new FileReader(filename);
 BufferedReader br = new BufferedReader(fr);
 String s;
 while((s = br.readLine()) != null) {






              String [] ss100 = s.split("\\s+");
         for(String s10:ss100){
                            if(s10.indexOf(",")>-1)  {

                               ar.add(s10);
                            }

         }
 }
     fr.close();

            


     }catch(Exception ex){
               ex.printStackTrace();
           }




        return ar;
    }




    public static void main(String[] args) {

       ArrayList<String> myList =  getMyString("myFile.txt");

        System.out.println(myList);
        
}
}

Open in new window

0
 
TolgarAuthor Commented:
Actually it is only one line like this.

So can I use the first code you sent me to do it?

Thanks,
0
 
for_yanCommented:
Sure, you can
0
 
TolgarAuthor Commented:
So if I only want to read the first line the code should be fine right?

String myString = null;      
try{
FileReader fr = new FileReader("myFile.txt");
BufferedReader br = new BufferedReader(fr);
String s;
s = br.readLine();
String [] ss100 = s.split("\\s+");
    for(String s10:ss100){
    if(s10ndexOf(",")>-1)  {
       myString = s10;
       }
    }
}
    fr.close();
    }catch(Exception ex){
              ex.printStackTrace();
          }

Thanks,

Open in new window

0
 
for_yanCommented:
I think it should be OK, but mayeb you have extra braces
0
 
for_yanCommented:
this works fine for me and reads first line
String myString = null;
try{
FileReader fr = new FileReader("myFile.txt");
BufferedReader br = new BufferedReader(fr);
String s;
s = br.readLine();
String [] ss100 = s.split("\\s+");
    for(String s10:ss100){
    if(s10.indexOf(",")>-1)  {
       myString = s10;
       }

}
    fr.close();
    }catch(Exception ex){
              ex.printStackTrace();
          }
              System.out.println("myString: " + myString);

Open in new window

0
 
TolgarAuthor Commented:
And, can I use a method like these?

inJob = new Scanner(new File(pathToJobfile));
					java.lang.String jobFile = null;
					while(inJob.hasNextLine()){
						jobFile = inJob.nextLine();

Open in new window


But again I just wanna read the first line for sure.


Thanks,

0
 
for_yanCommented:
I think you could do that eiither.
Just give it a try.
I think you need closing brace in your code.

Why would not use one way which works?
0
 
TolgarAuthor Commented:
My concern is the fr.close command in this code.

I am not sure what is the difference between these two codes in terms reading a file in the background.

My concern comes from here:
if some other system is trying to work with this file do something, my process should not stop it.

And our coding style forces me to use Scanner rather than FileReader.

Thanks,
0
 
for_yanCommented:
I don't think it makes any difference.
Use Scanner if you wish.
I think those are different ways of writing it in java -
the implementation will be actually system dependent and may work differently
on different systems.
You'll hardly guess what is the best way without experimenting.
In my experience it is very rare that I saw any clashes when raeading with java
and in practical work I actually nevber yused scanner (as it appeared in Java much later),
but go for it. Scanner is sure not worse than anything else.
0
 
TolgarAuthor Commented:
@for_yan: Can we say that FileReader does not lock the file while reading?

and in my code, Scanner scans the whole file at once I guess. But I only want the first line. So, do you know if nextLine will read the first line or not?

inJob = new Scanner(new File(pathToJobfile));
java.lang.String jobFile = null;
jobFile = inJob.nextLine();

Open in new window



Thanks,
0
 
for_yanCommented:
I'm pretty sure that FileReader will read only first line
Don't know about the scanner - it would be rather stupid implemnentation if it would be
scanning the whole file immediately - but I don't know.

Don't think FileReadr will lock the file.
But all these things are system depnendet - java does not  promise it - so JVM will delegate it to the
system. I think if you open with FileReader read fisrt line and close it - it should not
have any dramatic effects in most cases.

0
 
for_yanCommented:

I think normally when opening files java does not lock them - you need to do something special
(with FileLocks etc) to ensure that file is locked when you access it.
So I think it should not be an issue in your case.

I'd rather user FileReader, it has been in use for longer time, so I hope you'll not have any issues.
If you read about Scanner it is more cioncentrated on the parsing etc.
Still I hope it does not read in the whiol file at the start.

But FileReader defineitely will not read the whol file.
You can acvtually use buffered reader and specify exactly how
many bytes it will read - and it is rrecommended to use buffered reader
if you read a long file say through.
I guess in your case with one first line it doesnot matter.
0
 
TolgarAuthor Commented:
I have quick additional question to this.

I have this code:

if(splittedFirstLine[i].indexOf(",")>-1)

Open in new window


how can I change it to:

if(splittedFirstLine[i].indexOf(",")>-1) && numbersOnly in splittedFirstLine[i] 

Open in new window



because the pattern is 999999, 888888 and they arealways numbers but to be on the safe side I wanna add it to the code to make sure that we do NOT include anything like aaaa, bbb

Thanks,
0
 
for_yanCommented:
  if(splittedFirstLine[i].matches("^[\\d]+,[\\d]+$"))  {// keep it}
else {//throw away}

Open in new window

0
 
TolgarAuthor Commented:
@yan; Does this code include comma as well?

Can you please explain me how this works?

Thanks,
0
 
for_yanCommented:
Yes, it include comma - it is regular expression requiring any number (>0) of digits on the left, then comma then any number of digits (>0) on the right
0
 
TolgarAuthor Commented:
actually here is something

There may be only only one number or no number there.

So these are possible cases:

020909-14:25 something 020909-14:25 999999,888888 020909-14:25

Open in new window


Return: 999999,888888



020909-14:25 something 020909-14:25 999999 020909-14:25

Open in new window


Return: 999999


There are double space characters in the last gap.

093011-19:29 something 093011-19:01  093011-19:28

Open in new window


Return: nothing



The code should cover all three. We cover the first rule with your last post. But I think it does not cover the second one.

So the general rule can be like this:

Look at  the fourth group in the line which is separated by single spaces.

If  there is more than one space between them just ignore it.

If there are only single spaces, then take the fourth one as a whole if it is consists of numbers only.

Sorry for the change, but the other one  did not cover all the cases.

Thanks,




0
 
for_yanCommented:
No number of spaces is inconveninent. So you want either just only didgits 9999 or digits with comma - 999,999 - two such cases - correct ?
0
 
TolgarAuthor Commented:
Yes I want to capture only digits or digits with commas.

But we need to keep in mid the third scenario. Because if we ignore it then we can capture the fourth element even if there is two spaces before it.

So here is the edge case which I modified from the third scenario:

      
093011-19:29 something 093011-19:01  0930111928

Open in new window


This should not be accepted because there are two spaces there.

So the priority is on the number of spaces between them. They all have to single space. Then We can consider only digits and commas.

I hope it is clear now.

Thanks,
0
 
for_yanCommented:

  if(splittedFirstLine[i].matches("^[\\d]+,?[\\d]+$"))  {// keep it}
else {//throw away}

Open in new window


this would match 99999  or 9999,88888 but will not match 093011-19:28 or something
0
 
for_yanCommented:
then you can check the line in the very beiginning

String s= fr.readLine();
if(s.trim().indexOf("  ") > -1){//throw away}
if it does not have "  " then we go aheead and split it and match
then do everything and

 if(splittedFirstLine[i].matches("^[\\d]+,?[\\d]+$"))  {// keep it}
else {//throw away}

Open in new window

0
 
for_yanCommented:

This should do all of it:
String myString = null;

try{
FileReader fr = new FileReader("myFile.txt");
BufferedReader br = new BufferedReader(fr);
String s;
s = br.readLine();
if(s.trim().indexOf("  ") == -1){
String [] ss100 = s.split("\\s+");
    for(String s10:ss100){
    if(s10.matches("^[\\d]+,?[\\d]+$"))  {
       myString = s10;
       }
}
}
    fr.close();
    }catch(Exception ex){
              ex.printStackTrace();
          }
              System.out.println("myString0: " + myString);

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 19
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now